diff --git a/src/js/messaging.js b/src/js/messaging.js index c8f5808af..1aa59195b 100644 --- a/src/js/messaging.js +++ b/src/js/messaging.js @@ -345,6 +345,7 @@ const onMessage = function(request, sender, callback) { case 'setWhitelist': µb.netWhitelist = µb.whitelistFromString(request.whitelist); µb.saveWhitelist(); + µb.filteringBehaviorChanged(); break; case 'toggleHostnameSwitch': diff --git a/src/js/scriptlet-filtering.js b/src/js/scriptlet-filtering.js index 595ed911b..037f399e0 100644 --- a/src/js/scriptlet-filtering.js +++ b/src/js/scriptlet-filtering.js @@ -67,7 +67,12 @@ const contentScriptRegisterer = new (class { constructor() { this.hostnameToDetails = new Map(); if ( browser.contentScripts === undefined ) { return; } - µb.onEvent('filteringBehaviorChanged', ( ) => { + µb.onEvent('filteringBehaviorChanged', ev => { + const details = ev.detail; + if ( details instanceof Object ) { + if ( details.direction > 0 ) { return; } + if ( details.hostname ) { return this.flush(details.hostname); } + } this.reset(); }); } @@ -100,6 +105,15 @@ const contentScriptRegisterer = new (class { this.hostnameToDetails.delete(hostname); this.unregisterHandle(details.handle); } + flush(hostname) { + if ( hostname === '*' ) { return this.reset(); } + for ( const hn of this.hostnameToDetails.keys() ) { + if ( hn.endsWith(hostname) === false ) { continue; } + const pos = hn.length - hostname.length; + if ( pos !== 0 && hn.charCodeAt(pos-1) !== 0x2E /* . */ ) { continue; } + this.unregister(hn); + } + } reset() { if ( this.hostnameToDetails.size === 0 ) { return; } for ( const details of this.hostnameToDetails.values() ) { diff --git a/src/js/storage.js b/src/js/storage.js index 2b1a81594..a8e0a7c81 100644 --- a/src/js/storage.js +++ b/src/js/storage.js @@ -364,7 +364,6 @@ import { netWhitelist: this.arrayFromWhitelist(this.netWhitelist) }); this.netWhitelistModifyTime = Date.now(); - µb.filteringBehaviorChanged(); }; /******************************************************************************/ diff --git a/src/js/ublock.js b/src/js/ublock.js index a02ee6f1b..7f902af38 100644 --- a/src/js/ublock.js +++ b/src/js/ublock.js @@ -147,6 +147,7 @@ const matchBucket = function(url, hostname, bucket, start) { } bucket.push(directive); this.saveWhitelist(); + µb.filteringBehaviorChanged({ hostname: targetHostname }); return true; } @@ -187,6 +188,7 @@ const matchBucket = function(url, hostname, bucket, start) { } } this.saveWhitelist(); + µb.filteringBehaviorChanged({ direction: 1 }); return true; }; @@ -465,7 +467,8 @@ const matchBucket = function(url, hostname, bucket, start) { // (but not really) redundant rules led to this issue. µb.toggleFirewallRule = function(details) { - let { srcHostname, desHostname, requestType, action } = details; + const { desHostname, requestType, action } = details; + let { srcHostname } = details; if ( action !== 0 ) { sessionFirewall.setCell( @@ -495,8 +498,7 @@ const matchBucket = function(url, hostname, bucket, start) { permanentFirewall.unsetCell( srcHostname, desHostname, - requestType, - action + requestType ); } this.savePermanentFirewallRules(); @@ -521,8 +523,11 @@ const matchBucket = function(url, hostname, bucket, start) { // https://github.com/chrisaljoudi/uBlock/issues/420 cosmeticFilteringEngine.removeFromSelectorCache(srcHostname, 'net'); - // Flush memory cache - µb.filteringBehaviorChanged(); + // Flush caches + µb.filteringBehaviorChanged({ + direction: action === 1 ? 1 : 0, + hostname: srcHostname, + }); if ( details.tabId === undefined ) { return; } @@ -603,12 +608,15 @@ const matchBucket = function(url, hostname, bucket, start) { break; } - // Flush memory cache if needed + // Flush caches if needed if ( newState ) { switch ( details.name ) { case 'no-scripting': case 'no-remote-fonts': - µb.filteringBehaviorChanged(); + µb.filteringBehaviorChanged({ + direction: details.state ? 1 : 0, + hostname: details.hostname, + }); break; default: break; diff --git a/src/js/utils.js b/src/js/utils.js index 09e54eb15..9f94aeb25 100644 --- a/src/js/utils.js +++ b/src/js/utils.js @@ -134,13 +134,13 @@ import µb from './background.js'; /******************************************************************************/ -µb.fireEvent = function(name) { +µb.fireEvent = function(name, details = undefined) { if ( self instanceof Object && self.dispatchEvent instanceof Function && self.CustomEvent instanceof Function ) { - self.dispatchEvent(new CustomEvent(name)); + self.dispatchEvent(new CustomEvent(name, { detail: details })); } }; @@ -155,9 +155,11 @@ import µb from './background.js'; /******************************************************************************/ -µb.filteringBehaviorChanged = function() { - vAPI.net.handlerBehaviorChanged(); - this.fireEvent('filteringBehaviorChanged'); +µb.filteringBehaviorChanged = function(details = {}) { + if ( typeof details.direction !== 'number' || details.direction >= 0 ) { + vAPI.net.handlerBehaviorChanged(); + } + this.fireEvent('filteringBehaviorChanged', details); }; /******************************************************************************/