From e1b253fc19583b7a32ac9d27ecb0837ae42f34e7 Mon Sep 17 00:00:00 2001 From: gorhill Date: Wed, 30 Jul 2014 08:05:00 -0400 Subject: [PATCH] no need for mutation observer if no scripting on the page --- js/contentscript-end.js | 62 +++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 34 deletions(-) diff --git a/js/contentscript-end.js b/js/contentscript-end.js index 9aff7ecb9..ed4d191e5 100644 --- a/js/contentscript-end.js +++ b/js/contentscript-end.js @@ -126,8 +126,7 @@ var messaging = (function(name){ // ABP cosmetic filters -var cosmeticFiltering = (function() { - +(function() { var queriedSelectors = {}; var injectedSelectors = {}; var classSelectors = null; @@ -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 + }); })(); /******************************************************************************/ @@ -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 - }); -} - -/******************************************************************************/ - })();