Skip to content

Commit

Permalink
no need for mutation observer if no scripting on the page
Browse files Browse the repository at this point in the history
  • Loading branch information
gorhill committed Jul 30, 2014
1 parent 297df56 commit e1b253f
Showing 1 changed file with 28 additions and 34 deletions.
62 changes: 28 additions & 34 deletions js/contentscript-end.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,7 @@ var messaging = (function(name){

// ABP cosmetic filters

var cosmeticFiltering = (function() {

(function() {
var queriedSelectors = {};
var injectedSelectors = {};
var classSelectors = null;
Expand Down Expand Up @@ -391,9 +390,34 @@ var cosmeticFiltering = (function() {

domLoaded();

return {
processNodeLists: processNodeLists
// Observe changes in the DOM only if...
// - there is a document.body
// - there is at least one `script` tag
if ( !document.body || !document.querySelector('script') ) {
return;
}

var mutationObservedHandler = function(mutations) {
var iMutation = mutations.length;
var nodeLists = [], nodeList;
while ( iMutation-- ) {
nodeList = mutations[iMutation].addedNodes;
if ( nodeList && nodeList.length ) {
nodeLists.push(nodeList);
}
}
if ( nodeLists.length ) {
processNodeLists(nodeLists);
}
};
// https://github.com/gorhill/httpswitchboard/issues/176
var observer = new MutationObserver(mutationObservedHandler);
observer.observe(document.body, {
attributes: false,
childList: true,
characterData: false,
subtree: true
});
})();

/******************************************************************************/
Expand Down Expand Up @@ -455,34 +479,4 @@ var cosmeticFiltering = (function() {

/******************************************************************************/

// Observe changes in the DOM

var mutationObservedHandler = function(mutations) {
var iMutation = mutations.length;
var nodeLists = [], nodeList;
while ( iMutation-- ) {
nodeList = mutations[iMutation].addedNodes;
if ( nodeList && nodeList.length ) {
nodeLists.push(nodeList);
}
}
if ( nodeLists.length ) {
cosmeticFiltering.processNodeLists(nodeLists);
}
};

// This fixes http://acid3.acidtests.org/
if ( document.body ) {
// https://github.com/gorhill/httpswitchboard/issues/176
var observer = new MutationObserver(mutationObservedHandler);
observer.observe(document.body, {
attributes: false,
childList: true,
characterData: false,
subtree: true
});
}

/******************************************************************************/

})();

0 comments on commit e1b253f

Please sign in to comment.