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 */
'use strict';
/******************************************************************************/
@ -42,7 +43,7 @@ var messager = vAPI.messaging.channel('contentscript-end.js');
(function() {
var queriedSelectors = {};
var injectedSelectors = {};
var injectedSelectors = vAPI.injectedCosmeticFilters || {};
var classSelectors = null;
var idSelectors = null;
var highGenerics = null;
@ -50,57 +51,18 @@ var messager = vAPI.messaging.channel('contentscript-end.js');
var nullArray = { push: function(){} };
var domLoaded = function() {
var style = document.getElementById('ublock-preload-1ae7a5f130fc79b4fdb8a4272d9426b5');
if ( style ) {
// https://github.com/gorhill/uBlock/issues/14
// Treat any existing domain-specific exception selectors as if
// they had been injected already.
var selectors, i;
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(','));
// rhill 2014-11-16: not sure this is needed anymore. Test case in
// above issue was fine without the line below..
if ( vAPI.injectedCosmeticFilters ) {
hideElements(Object.keys(vAPI.injectedCosmeticFilters).join(','));
}
idsFromNodeList(document.querySelectorAll('[id]'));
classesFromNodeList(document.querySelectorAll('[class]'));
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 selectors = classSelectors !== null ? Object.keys(classSelectors) : [];
if ( idSelectors !== null ) {

View File

@ -21,6 +21,7 @@
/* jshint multistr: true */
/* global vAPI */
'use strict';
/******************************************************************************/
@ -54,6 +55,7 @@ var cosmeticFilters = function(details) {
if ( style !== null ) {
return;
}
var injectedCosmeticFilters = {};
style = document.createElement('style');
style.setAttribute('id', 'ublock-preload-1ae7a5f130fc79b4fdb8a4272d9426b5');
var donthide = details.cosmeticDonthide;
@ -61,20 +63,22 @@ var cosmeticFilters = function(details) {
if ( donthide.length !== 0 ) {
donthide = donthide.length !== 1 ? donthide.join(',\n') : donthide[0];
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
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.split(',\n');
var i = donthide.length, j;
i = hide.length;
var selector;
while ( i-- ) {
j = hide.indexOf(donthide[i]);
if ( j !== -1 ) {
hide.splice(j, 1);
selector = hide[i];
if ( injectedCosmeticFilters[selector] ) {
hide.splice(i, 1);
} else {
injectedCosmeticFilters[selector] = true;
}
}
}
@ -90,6 +94,7 @@ var cosmeticFilters = function(details) {
if ( parent ) {
parent.appendChild(style);
}
vAPI.injectedCosmeticFilters = injectedCosmeticFilters;
};
var netFilters = function(details) {