From 4aa61fcf8996aadb8242ca87dde3f3bb9ed352ca Mon Sep 17 00:00:00 2001 From: gorhill Date: Tue, 7 Apr 2015 19:10:03 -0400 Subject: [PATCH] this fixes https://github.com/gorhill/uMatrix/issues/144 --- platform/chromium/vapi-client.js | 5 +- src/js/contentscript-end.js | 100 ++++++++++++++++++++++++++----- src/js/contentscript-start.js | 4 +- src/js/cosmetic-count.js | 6 +- src/js/cosmetic-off.js | 6 +- src/js/cosmetic-on.js | 6 +- src/js/messaging.js | 16 ++++- 7 files changed, 111 insertions(+), 32 deletions(-) diff --git a/platform/chromium/vapi-client.js b/platform/chromium/vapi-client.js index 1d7d0594d..86b9797de 100644 --- a/platform/chromium/vapi-client.js +++ b/platform/chromium/vapi-client.js @@ -21,8 +21,6 @@ // For non background pages -/* global self */ - /******************************************************************************/ (function(self) { @@ -136,6 +134,9 @@ vAPI.messaging = { }, close: function() { delete vAPI.messaging.channels[this.channelName]; + if ( Object.keys(vAPI.messaging.channels).length === 0 ) { + vAPI.messaging.close(); + } } }; diff --git a/src/js/contentscript-end.js b/src/js/contentscript-end.js index b65fc319f..9dc9340c1 100644 --- a/src/js/contentscript-end.js +++ b/src/js/contentscript-end.js @@ -37,7 +37,8 @@ if ( document instanceof HTMLDocument === false ) { return false; } -if ( !vAPI ) { +// This can happen +if ( !vAPI || !vAPI.messaging ) { //console.debug('contentscript-end.js > vAPI not found'); return; } @@ -57,10 +58,37 @@ if ( vAPI.contentscriptEndInjected ) { vAPI.contentscriptEndInjected = true; vAPI.styles = vAPI.styles || []; +/******************************************************************************/ +/******************************************************************************/ + +var shutdownJobs = (function() { + var jobs = []; + + return { + add: function(job) { + jobs.push(job); + }, + exec: function() { + //console.debug('Shutting down...'); + var job; + while ( job = jobs.pop() ) { + job(); + } + } + }; +})(); + +/******************************************************************************/ /******************************************************************************/ var messager = vAPI.messaging.channel('contentscript-end.js'); +// https://github.com/gorhill/uMatrix/issues/144 +shutdownJobs.add(function() { + messager.close(); +}); + +/******************************************************************************/ /******************************************************************************/ // https://github.com/chrisaljoudi/uBlock/issues/789 @@ -120,7 +148,14 @@ var uBlockCollapser = (function() { this.collapse = false; }; - var onProcessed = function(requests) { + var onProcessed = function(response) { + // https://github.com/gorhill/uMatrix/issues/144 + if ( response.shutdown ) { + shutdownJobs.exec(); + return; + } + + var requests = response.result; if ( requests === null || Array.isArray(requests) === false ) { return; } @@ -253,10 +288,12 @@ var uBlockCollapser = (function() { (function() { if ( vAPI.skipCosmeticFiltering ) { - // console.debug('Abort cosmetic filtering'); + //console.debug('Abort cosmetic filtering'); return; } + //console.debug('Starts cosmetic filtering'); + //var timer = window.performance || Date; //var tStart = timer.now(); @@ -315,12 +352,13 @@ var uBlockCollapser = (function() { firstRetrieveHandler = null; // These are sent only once - if ( response ) { - if ( response.highGenerics ) { - highGenerics = response.highGenerics; + var result = response && response.result; + if ( result ) { + if ( result.highGenerics ) { + highGenerics = result.highGenerics; } - if ( response.donthide ) { - processLowGenerics(response.donthide, nullArray); + if ( result.donthide ) { + processLowGenerics(result.donthide, nullArray); } } @@ -328,11 +366,19 @@ var uBlockCollapser = (function() { }; var nextRetrieveHandler = function(response) { + // https://github.com/gorhill/uMatrix/issues/144 + if ( response && response.shutdown ) { + shutdownJobs.exec(); + return; + } + //var tStart = timer.now(); //console.debug('µBlock> contextNodes = %o', contextNodes); + var result = response && response.result; var hideSelectors = []; - if ( response && response.hide.length ) { - processLowGenerics(response.hide, hideSelectors); + + if ( result && result.hide.length ) { + processLowGenerics(result.hide, hideSelectors); } if ( highGenerics ) { if ( highGenerics.hideLowCount ) { @@ -684,12 +730,22 @@ var uBlockCollapser = (function() { } }; + //console.debug('Starts cosmetic filtering\'s mutations observer'); + // https://github.com/gorhill/httpswitchboard/issues/176 var treeObserver = new MutationObserver(treeMutationObservedHandlerAsync); treeObserver.observe(document.body, { childList: true, subtree: true }); + + // https://github.com/gorhill/uMatrix/issues/144 + shutdownJobs.add(function() { + treeObserver.disconnect(); + if ( addedNodeListsTimer !== null ) { + clearTimeout(addedNodeListsTimer); + } + }); })(); /******************************************************************************/ @@ -702,12 +758,19 @@ var uBlockCollapser = (function() { // - Elements dynamically added to the page // - Elements which resource URL changes -var onResourceFailed = function(ev) { - //console.debug('onResourceFailed(%o)', ev); - uBlockCollapser.add(ev.target); - uBlockCollapser.process(); -}; -document.addEventListener('error', onResourceFailed, true); +(function() { + var onResourceFailed = function(ev) { + //console.debug('onResourceFailed(%o)', ev); + uBlockCollapser.add(ev.target); + uBlockCollapser.process(); + }; + document.addEventListener('error', onResourceFailed, true); + + // https://github.com/gorhill/uMatrix/issues/144 + shutdownJobs.add(function() { + document.removeEventListener('error', onResourceFailed, true); + }); +})(); /******************************************************************************/ /******************************************************************************/ @@ -766,6 +829,11 @@ document.addEventListener('error', onResourceFailed, true); }; window.addEventListener('contextmenu', onContextMenu, true); + + // https://github.com/gorhill/uMatrix/issues/144 + shutdownJobs.add(function() { + document.removeEventListener('contextmenu', onContextMenu, true); + }); })(); /******************************************************************************/ diff --git a/src/js/contentscript-start.js b/src/js/contentscript-start.js index 1c0670e2b..1a40187ff 100644 --- a/src/js/contentscript-start.js +++ b/src/js/contentscript-start.js @@ -40,8 +40,8 @@ if ( document instanceof HTMLDocument === false ) { return false; } -// Because in case -if ( !vAPI ) { +// This can happen +if ( !vAPI || !vAPI.messaging ) { //console.debug('contentscript-start.js > vAPI not found'); return; } diff --git a/src/js/cosmetic-count.js b/src/js/cosmetic-count.js index bdf51b559..080404a03 100644 --- a/src/js/cosmetic-count.js +++ b/src/js/cosmetic-count.js @@ -35,9 +35,9 @@ if ( document instanceof HTMLDocument === false ) { return; } -// Because in case -if ( !vAPI ) { - //console.debug('cosmetic-on.js > vAPI not found'); +// This can happen +if ( !vAPI || !vAPI.messaging ) { + //console.debug('cosmetic-count.js > no vAPI'); return; } diff --git a/src/js/cosmetic-off.js b/src/js/cosmetic-off.js index 7ca962fe0..14a19ab9c 100644 --- a/src/js/cosmetic-off.js +++ b/src/js/cosmetic-off.js @@ -35,9 +35,9 @@ if ( document instanceof HTMLDocument === false ) { return; } -// Because in case -if ( !vAPI ) { - //console.debug('cosmetic-off.js > vAPI not found'); +// This can happen +if ( !vAPI || !vAPI.messaging ) { + //console.debug('cosmetic-off.js > no vAPI'); return; } diff --git a/src/js/cosmetic-on.js b/src/js/cosmetic-on.js index 8cb80dccf..b1a2ad9dd 100644 --- a/src/js/cosmetic-on.js +++ b/src/js/cosmetic-on.js @@ -35,9 +35,9 @@ if ( document instanceof HTMLDocument === false ) { return; } -// Because in case -if ( !vAPI ) { - //console.debug('cosmetic-on.js > vAPI not found'); +// This can happen +if ( !vAPI || !vAPI.messaging ) { + //console.debug('cosmetic-on.js > no vAPI'); return; } diff --git a/src/js/messaging.js b/src/js/messaging.js index f06137076..0bef40a23 100644 --- a/src/js/messaging.js +++ b/src/js/messaging.js @@ -516,8 +516,12 @@ var onMessage = function(details, sender, callback) { switch ( details.what ) { case 'retrieveGenericCosmeticSelectors': - if ( pageStore && pageStore.getGenericCosmeticFilteringSwitch() ) { - response = µb.cosmeticFilteringEngine.retrieveGenericSelectors(details); + response = { + shutdown: !pageStore || pageStore.getGenericCosmeticFilteringSwitch() === false, + result: null + }; + if ( response.shutdown === false ) { + response.result = µb.cosmeticFilteringEngine.retrieveGenericSelectors(details); } break; @@ -527,7 +531,13 @@ var onMessage = function(details, sender, callback) { // Evaluate many requests case 'filterRequests': - response = filterRequests(pageStore, details); + response = { + shutdown: !pageStore || pageStore.getGenericCosmeticFilteringSwitch() === false, + result: null + }; + if ( response.shutdown === false ) { + response.result = filterRequests(pageStore, details); + } break; default: