This commit is contained in:
gorhill 2014-12-31 23:09:22 -05:00
parent 6c8b4cf201
commit baa0fee0f3
1 changed files with 27 additions and 13 deletions

View File

@ -48,7 +48,7 @@ var messager = vAPI.messaging.channel('contentscript-end.js');
} }
var queriedSelectors = {}; var queriedSelectors = {};
var injectedSelectors = vAPI.hideCosmeticFilters || {}; var injectedSelectors = {};
var classSelectors = null; var classSelectors = null;
var idSelectors = null; var idSelectors = null;
var highGenerics = null; var highGenerics = null;
@ -56,28 +56,36 @@ var messager = vAPI.messaging.channel('contentscript-end.js');
var nullArray = { push: function(){} }; var nullArray = { push: function(){} };
var domLoaded = function() { var domLoaded = function() {
idsFromNodeList(document.querySelectorAll('[id]'));
classesFromNodeList(document.querySelectorAll('[class]'));
retrieveGenericSelectors();
};
// https://github.com/gorhill/uBlock/issues/452
// This needs to be executed *after* the response from our query is
// received, not at `DOMContentLoaded` time, or else there is a good
// likeliness to outrun contentscript-start.js, which may still be waiting
// on a response from its own query.
var firstRunHandler = function() {
// https://github.com/gorhill/uBlock/issues/158 // https://github.com/gorhill/uBlock/issues/158
// Ensure injected styles are enforced // Ensure injected styles are enforced
// rhill 2014-11-16: not sure this is needed anymore. Test case in // rhill 2014-11-16: not sure this is needed anymore. Test case in
// above issue was fine without the line below.. // above issue was fine without the line below..
var selectors = vAPI.hideCosmeticFilters; var selectors = vAPI.hideCosmeticFilters;
if ( typeof selectors === 'object' ) { if ( typeof selectors === 'object' ) {
injectedSelectors = selectors;
hideElements(Object.keys(selectors).join(',')); hideElements(Object.keys(selectors).join(','));
} }
// Add exception filters into injected filters collection, in order // Add exception filters into injected filters collection, in order
// to force them to be seen as "already injected". // to force them to be seen as "already injected".
selectors = vAPI.donthideCosmeticFilters; selectors = vAPI.donthideCosmeticFilters;
if ( typeof selectors === 'object' ) { if ( typeof selectors === 'object' ) {
for ( var selector in selectors ) { for ( selector in selectors ) {
if ( selectors.hasOwnProperty(selector) ) { if ( selectors.hasOwnProperty(selector) ) {
injectedSelectors[selector] = true; injectedSelectors[selector] = true;
} }
} }
} }
// Now scan content of page
idsFromNodeList(document.querySelectorAll('[id]'));
classesFromNodeList(document.querySelectorAll('[class]'));
retrieveGenericSelectors();
}; };
var retrieveGenericSelectors = function() { var retrieveGenericSelectors = function() {
@ -103,6 +111,12 @@ var messager = vAPI.messaging.channel('contentscript-end.js');
}; };
var retrieveHandler = function(selectors) { var retrieveHandler = function(selectors) {
// https://github.com/gorhill/uBlock/issues/452
// See above.
if ( typeof firstRunHandler === 'function' ) {
firstRunHandler();
firstRunHandler = undefined;
}
//console.debug('µBlock> contextNodes = %o', contextNodes); //console.debug('µBlock> contextNodes = %o', contextNodes);
if ( selectors && selectors.highGenerics ) { if ( selectors && selectors.highGenerics ) {
highGenerics = selectors.highGenerics; highGenerics = selectors.highGenerics;
@ -216,7 +230,7 @@ var messager = vAPI.messaging.channel('contentscript-end.js');
var selector; var selector;
while ( i-- ) { while ( i-- ) {
selector = generics[i]; selector = generics[i];
if ( injectedSelectors[selector] !== undefined ) { if ( injectedSelectors.hasOwnProperty(selector) ) {
continue; continue;
} }
injectedSelectors[selector] = true; injectedSelectors[selector] = true;
@ -241,14 +255,14 @@ var messager = vAPI.messaging.channel('contentscript-end.js');
if ( !attrValue ) { continue; } if ( !attrValue ) { continue; }
selector = '[' + attr + '="' + attrValue + '"]'; selector = '[' + attr + '="' + attrValue + '"]';
if ( generics[selector] ) { if ( generics[selector] ) {
if ( injectedSelectors[selector] === undefined ) { if ( injectedSelectors.hasOwnProperty(selector) === false ) {
injectedSelectors[selector] = true; injectedSelectors[selector] = true;
out.push(selector); out.push(selector);
} }
} }
selector = node.tagName.toLowerCase() + selector; selector = node.tagName.toLowerCase() + selector;
if ( generics[selector] ) { if ( generics[selector] ) {
if ( injectedSelectors[selector] === undefined ) { if ( injectedSelectors.hasOwnProperty(selector) === false ) {
injectedSelectors[selector] = true; injectedSelectors[selector] = true;
out.push(selector); out.push(selector);
} }
@ -277,7 +291,7 @@ var messager = vAPI.messaging.channel('contentscript-end.js');
iSelector = selectors.length; iSelector = selectors.length;
while ( iSelector-- ) { while ( iSelector-- ) {
selector = selectors[iSelector]; selector = selectors[iSelector];
if ( injectedSelectors[selector] === undefined ) { if ( injectedSelectors.hasOwnProperty(selector) === false ) {
injectedSelectors[selector] = true; injectedSelectors[selector] = true;
out.push(selector); out.push(selector);
} }
@ -293,7 +307,7 @@ var messager = vAPI.messaging.channel('contentscript-end.js');
var processHighHighGenerics = function() { var processHighHighGenerics = function() {
processHighHighGenericsTimer = null; processHighHighGenericsTimer = null;
if ( injectedSelectors['{{highHighGenerics}}'] !== undefined ) { return; } if ( injectedSelectors.hasOwnProperty('{{highHighGenerics}}') ) { return; }
if ( document.querySelector(highGenerics.hideHigh) === null ) { return; } if ( document.querySelector(highGenerics.hideHigh) === null ) { return; }
injectedSelectors['{{highHighGenerics}}'] = true; injectedSelectors['{{highHighGenerics}}'] = true;
// We need to filter out possible exception cosmetic filters from // We need to filter out possible exception cosmetic filters from
@ -443,10 +457,10 @@ var messager = vAPI.messaging.channel('contentscript-end.js');
} }
} }
if ( addedNodeListsTimer === null ) { if ( addedNodeListsTimer === null ) {
// I arbitrarily chose 50 ms for now: // I arbitrarily chose 100 ms for now:
// I have to compromise between the overhead of processing too few // I have to compromise between the overhead of processing too few
// nodes too often and the delay of many nodes less often. // nodes too often and the delay of many nodes less often.
addedNodeListsTimer = setTimeout(mutationObservedHandler, 75); addedNodeListsTimer = setTimeout(mutationObservedHandler, 100);
} }
}; };