1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-11-02 00:42:45 +01:00
This commit is contained in:
Raymond Hill 2014-11-16 17:06:29 -02:00
parent f88d1849d0
commit c4838581fd
2 changed files with 22 additions and 55 deletions

View File

@ -20,6 +20,7 @@
*/ */
/* global vAPI */ /* global vAPI */
'use strict'; 'use strict';
/******************************************************************************/ /******************************************************************************/
@ -42,7 +43,7 @@ var messager = vAPI.messaging.channel('contentscript-end.js');
(function() { (function() {
var queriedSelectors = {}; var queriedSelectors = {};
var injectedSelectors = {}; var injectedSelectors = vAPI.injectedCosmeticFilters || {};
var classSelectors = null; var classSelectors = null;
var idSelectors = null; var idSelectors = null;
var highGenerics = null; var highGenerics = null;
@ -50,57 +51,18 @@ var messager = vAPI.messaging.channel('contentscript-end.js');
var nullArray = { push: function(){} }; var nullArray = { push: function(){} };
var domLoaded = function() { var domLoaded = function() {
var style = document.getElementById('ublock-preload-1ae7a5f130fc79b4fdb8a4272d9426b5'); // https://github.com/gorhill/uBlock/issues/158
if ( style ) { // Ensure injected styles are enforced
// https://github.com/gorhill/uBlock/issues/14 // rhill 2014-11-16: not sure this is needed anymore. Test case in
// Treat any existing domain-specific exception selectors as if // above issue was fine without the line below..
// they had been injected already. if ( vAPI.injectedCosmeticFilters ) {
var selectors, i; hideElements(Object.keys(vAPI.injectedCosmeticFilters).join(','));
var exceptions = style.getAttribute('data-ublock-exceptions');
if ( exceptions ) {
selectors = JSON.parse(exceptions);
i = selectors.length;
while ( i-- ) {
injectedSelectors[selectors[i]] = true;
}
}
// Avoid re-injecting already injected CSS rules.
selectors = selectorsFromStyles(style);
i = selectors.length;
while ( i-- ) {
injectedSelectors[selectors[i]] = true;
}
// https://github.com/gorhill/uBlock/issues/158
// Ensure injected styles are enforced
hideElements(selectors.join(','));
} }
idsFromNodeList(document.querySelectorAll('[id]')); idsFromNodeList(document.querySelectorAll('[id]'));
classesFromNodeList(document.querySelectorAll('[class]')); classesFromNodeList(document.querySelectorAll('[class]'));
retrieveGenericSelectors(); retrieveGenericSelectors();
}; };
var selectorsFromStyles = function(styleRef) {
var selectors = [];
var styles = typeof styleRef === 'string' ?
document.querySelectorAll(styleRef):
[styleRef];
var i = styles.length;
var style, subset, lastSelector, pos;
while ( i-- ) {
style = styles[i];
subset = style.textContent.split(',\n');
lastSelector = subset.pop();
if ( lastSelector ) {
pos = lastSelector.indexOf('\n');
if ( pos !== -1 ) {
subset.push(lastSelector.slice(0, pos));
}
}
selectors = selectors.concat(subset);
}
return selectors;
};
var retrieveGenericSelectors = function() { var retrieveGenericSelectors = function() {
var selectors = classSelectors !== null ? Object.keys(classSelectors) : []; var selectors = classSelectors !== null ? Object.keys(classSelectors) : [];
if ( idSelectors !== null ) { if ( idSelectors !== null ) {

View File

@ -21,6 +21,7 @@
/* jshint multistr: true */ /* jshint multistr: true */
/* global vAPI */ /* global vAPI */
'use strict'; 'use strict';
/******************************************************************************/ /******************************************************************************/
@ -54,6 +55,7 @@ var cosmeticFilters = function(details) {
if ( style !== null ) { if ( style !== null ) {
return; return;
} }
var injectedCosmeticFilters = {};
style = document.createElement('style'); style = document.createElement('style');
style.setAttribute('id', 'ublock-preload-1ae7a5f130fc79b4fdb8a4272d9426b5'); style.setAttribute('id', 'ublock-preload-1ae7a5f130fc79b4fdb8a4272d9426b5');
var donthide = details.cosmeticDonthide; var donthide = details.cosmeticDonthide;
@ -61,20 +63,22 @@ var cosmeticFilters = function(details) {
if ( donthide.length !== 0 ) { if ( donthide.length !== 0 ) {
donthide = donthide.length !== 1 ? donthide.join(',\n') : donthide[0]; donthide = donthide.length !== 1 ? donthide.join(',\n') : donthide[0];
donthide = donthide.split(',\n'); donthide = donthide.split(',\n');
style.setAttribute('data-ublock-exceptions', JSON.stringify(donthide)); var i = donthide.length;
while ( i-- ) {
injectedCosmeticFilters[donthide[i]] = true;
}
// https://github.com/gorhill/uBlock/issues/143 // https://github.com/gorhill/uBlock/issues/143
if ( hide.length !== 0 ) { if ( hide.length !== 0 ) {
// I chose to use Array.indexOf() instead of converting the array to
// a map, then deleting whitelisted selectors, and then converting
// back the map into an array, because there are typically very few
// exception filters, if any.
hide = hide.length !== 1 ? hide.join(',\n') : hide[0]; hide = hide.length !== 1 ? hide.join(',\n') : hide[0];
hide = hide.split(',\n'); hide = hide.split(',\n');
var i = donthide.length, j; i = hide.length;
var selector;
while ( i-- ) { while ( i-- ) {
j = hide.indexOf(donthide[i]); selector = hide[i];
if ( j !== -1 ) { if ( injectedCosmeticFilters[selector] ) {
hide.splice(j, 1); hide.splice(i, 1);
} else {
injectedCosmeticFilters[selector] = true;
} }
} }
} }
@ -90,6 +94,7 @@ var cosmeticFilters = function(details) {
if ( parent ) { if ( parent ) {
parent.appendChild(style); parent.appendChild(style);
} }
vAPI.injectedCosmeticFilters = injectedCosmeticFilters;
}; };
var netFilters = function(details) { var netFilters = function(details) {