From baa0fee0f3e5f09bc8e216f5a56879b6850a2f51 Mon Sep 17 00:00:00 2001 From: gorhill Date: Wed, 31 Dec 2014 23:09:22 -0500 Subject: [PATCH] this fixes #452 --- src/js/contentscript-end.js | 40 +++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/src/js/contentscript-end.js b/src/js/contentscript-end.js index 1d8cf7f68..7f8f1b7ad 100644 --- a/src/js/contentscript-end.js +++ b/src/js/contentscript-end.js @@ -48,7 +48,7 @@ var messager = vAPI.messaging.channel('contentscript-end.js'); } var queriedSelectors = {}; - var injectedSelectors = vAPI.hideCosmeticFilters || {}; + var injectedSelectors = {}; var classSelectors = null; var idSelectors = null; var highGenerics = null; @@ -56,28 +56,36 @@ var messager = vAPI.messaging.channel('contentscript-end.js'); var nullArray = { push: 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 // Ensure injected styles are enforced // rhill 2014-11-16: not sure this is needed anymore. Test case in // above issue was fine without the line below.. var selectors = vAPI.hideCosmeticFilters; if ( typeof selectors === 'object' ) { + injectedSelectors = selectors; hideElements(Object.keys(selectors).join(',')); } // Add exception filters into injected filters collection, in order // to force them to be seen as "already injected". selectors = vAPI.donthideCosmeticFilters; if ( typeof selectors === 'object' ) { - for ( var selector in selectors ) { + for ( selector in selectors ) { if ( selectors.hasOwnProperty(selector) ) { injectedSelectors[selector] = true; } } } - // Now scan content of page - idsFromNodeList(document.querySelectorAll('[id]')); - classesFromNodeList(document.querySelectorAll('[class]')); - retrieveGenericSelectors(); }; var retrieveGenericSelectors = function() { @@ -103,6 +111,12 @@ var messager = vAPI.messaging.channel('contentscript-end.js'); }; 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); if ( selectors && selectors.highGenerics ) { highGenerics = selectors.highGenerics; @@ -216,7 +230,7 @@ var messager = vAPI.messaging.channel('contentscript-end.js'); var selector; while ( i-- ) { selector = generics[i]; - if ( injectedSelectors[selector] !== undefined ) { + if ( injectedSelectors.hasOwnProperty(selector) ) { continue; } injectedSelectors[selector] = true; @@ -241,14 +255,14 @@ var messager = vAPI.messaging.channel('contentscript-end.js'); if ( !attrValue ) { continue; } selector = '[' + attr + '="' + attrValue + '"]'; if ( generics[selector] ) { - if ( injectedSelectors[selector] === undefined ) { + if ( injectedSelectors.hasOwnProperty(selector) === false ) { injectedSelectors[selector] = true; out.push(selector); } } selector = node.tagName.toLowerCase() + selector; if ( generics[selector] ) { - if ( injectedSelectors[selector] === undefined ) { + if ( injectedSelectors.hasOwnProperty(selector) === false ) { injectedSelectors[selector] = true; out.push(selector); } @@ -277,7 +291,7 @@ var messager = vAPI.messaging.channel('contentscript-end.js'); iSelector = selectors.length; while ( iSelector-- ) { selector = selectors[iSelector]; - if ( injectedSelectors[selector] === undefined ) { + if ( injectedSelectors.hasOwnProperty(selector) === false ) { injectedSelectors[selector] = true; out.push(selector); } @@ -293,7 +307,7 @@ var messager = vAPI.messaging.channel('contentscript-end.js'); var processHighHighGenerics = function() { processHighHighGenericsTimer = null; - if ( injectedSelectors['{{highHighGenerics}}'] !== undefined ) { return; } + if ( injectedSelectors.hasOwnProperty('{{highHighGenerics}}') ) { return; } if ( document.querySelector(highGenerics.hideHigh) === null ) { return; } injectedSelectors['{{highHighGenerics}}'] = true; // 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 ) { - // 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 // nodes too often and the delay of many nodes less often. - addedNodeListsTimer = setTimeout(mutationObservedHandler, 75); + addedNodeListsTimer = setTimeout(mutationObservedHandler, 100); } };