From cf94ca55760128e544dc14471ef13b0c17fd5e6a Mon Sep 17 00:00:00 2001 From: gorhill Date: Fri, 10 Apr 2015 13:00:34 -0400 Subject: [PATCH 1/2] whitelist directives must override strict blocking --- src/js/traffic.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/js/traffic.js b/src/js/traffic.js index f9175fe0e..2f89bff94 100644 --- a/src/js/traffic.js +++ b/src/js/traffic.js @@ -199,6 +199,11 @@ var onBeforeRootFrameRequest = function(details) { var result = ''; + // If the site is whitelisted, disregard strict blocking + if ( µb.getNetFilteringSwitch(requestURL) === false ) { + result = 'ua:whitelisted'; + } + // Permanently unrestricted? if ( result === '' && µb.hnSwitches.evaluateZ('dontBlockDoc', requestHostname) ) { result = 'ua:dontBlockDoc true'; @@ -217,7 +222,7 @@ var onBeforeRootFrameRequest = function(details) { } // Filtering - if ( result === '' && µb.getNetFilteringSwitch(requestURL) ) { + if ( result === '' ) { result = µb.staticNetFilteringEngine.matchString(context); // https://github.com/chrisaljoudi/uBlock/issues/1128 // Do not block if the match begins after the hostname. From 741c145e90f9e93fd9fc31c6305362ae94c77ee5 Mon Sep 17 00:00:00 2001 From: gorhill Date: Fri, 10 Apr 2015 14:08:43 -0400 Subject: [PATCH 2/2] this fixes https://github.com/gorhill/uMatrix/issues/144 --- platform/chromium/vapi-client.js | 3 ++ src/js/contentscript-end.js | 91 ++++++++++++++++++++++++++------ src/js/messaging.js | 17 ++++-- 3 files changed, 92 insertions(+), 19 deletions(-) diff --git a/platform/chromium/vapi-client.js b/platform/chromium/vapi-client.js index 616d4e2aa..f3898fc8a 100644 --- a/platform/chromium/vapi-client.js +++ b/platform/chromium/vapi-client.js @@ -150,6 +150,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 d90d08898..e5df12d4d 100644 --- a/src/js/contentscript-end.js +++ b/src/js/contentscript-end.js @@ -56,10 +56,36 @@ if ( vAPI.contentscriptEndInjected ) { } vAPI.contentscriptEndInjected = true; +/******************************************************************************/ +/******************************************************************************/ + +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 @@ -119,7 +145,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; } @@ -314,24 +347,32 @@ 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); } } nextRetrieveHandler(response); }; - var nextRetrieveHandler = function(selectors) { + 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 ( selectors && selectors.hide.length ) { - processLowGenerics(selectors.hide, hideSelectors); + if ( result && result.hide.length ) { + processLowGenerics(result.hide, hideSelectors); } if ( highGenerics ) { if ( highGenerics.hideLowCount ) { @@ -688,6 +729,14 @@ var uBlockCollapser = (function() { childList: true, subtree: true }); + + // https://github.com/gorhill/uMatrix/issues/144 + shutdownJobs.add(function() { + treeObserver.disconnect(); + if ( addedNodeListsTimer !== null ) { + clearTimeout(addedNodeListsTimer); + } + }); })(); /******************************************************************************/ @@ -700,12 +749,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); + }); +})(); /******************************************************************************/ /******************************************************************************/ @@ -764,6 +820,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/messaging.js b/src/js/messaging.js index 3a0b7d010..4e526cab7 100644 --- a/src/js/messaging.js +++ b/src/js/messaging.js @@ -496,9 +496,12 @@ var onMessage = function(details, sender, callback) { switch ( details.what ) { case 'retrieveGenericCosmeticSelectors': - if ( pageStore && pageStore.getGenericCosmeticFilteringSwitch() - && (!frameStore || frameStore.getNetFilteringSwitch()) ) { - response = µb.cosmeticFilteringEngine.retrieveGenericSelectors(details); + response = { + shutdown: !pageStore || !pageStore.getNetFilteringSwitch() || (frameStore && !frameStore.getNetFilteringSwitch()), + result: null + }; + if(pageStore && pageStore.getGenericCosmeticFilteringSwitch()) { + response.result = µb.cosmeticFilteringEngine.retrieveGenericSelectors(details); } break; @@ -508,7 +511,13 @@ var onMessage = function(details, sender, callback) { // Evaluate many requests case 'filterRequests': - response = filterRequests(pageStore, details); + response = { + shutdown: !pageStore || !pageStore.getNetFilteringSwitch(), + result: null + }; + if(pageStore) { + response.result = filterRequests(pageStore, details); + } break; default: