diff --git a/src/js/commands.js b/src/js/commands.js index 70f73fdfb..dff8a7772 100644 --- a/src/js/commands.js +++ b/src/js/commands.js @@ -38,77 +38,95 @@ if ( µBlock.canUseShortcuts === false ) { return; } -const relaxBlockingMode = function(tab) { - if ( tab instanceof Object === false || tab.id <= 0 ) { return; } +const relaxBlockingMode = (( ) => { + const reloadTimers = new Map(); - const µb = µBlock; - const normalURL = µb.normalizePageURL(tab.id, tab.url); + return function(tab) { + if ( tab instanceof Object === false || tab.id <= 0 ) { return; } - if ( µb.getNetFilteringSwitch(normalURL) === false ) { return; } + const µb = µBlock; + const normalURL = µb.normalizePageURL(tab.id, tab.url); - const hn = µb.URI.hostnameFromURI(normalURL); - const curProfileBits = µb.blockingModeFromHostname(hn); - let newProfileBits; - for ( const profile of µb.liveBlockingProfiles ) { - if ( (curProfileBits & profile.bits & ~1) !== curProfileBits ) { - newProfileBits = profile.bits; - break; + if ( µb.getNetFilteringSwitch(normalURL) === false ) { return; } + + const hn = µb.URI.hostnameFromURI(normalURL); + const curProfileBits = µb.blockingModeFromHostname(hn); + let newProfileBits; + for ( const profile of µb.liveBlockingProfiles ) { + if ( (curProfileBits & profile.bits & ~1) !== curProfileBits ) { + newProfileBits = profile.bits; + break; + } } - } - // TODO: Reset to original blocking profile? - if ( newProfileBits === undefined ) { return; } + // TODO: Reset to original blocking profile? + if ( newProfileBits === undefined ) { return; } - if ( - (curProfileBits & 0b00000010) !== 0 && - (newProfileBits & 0b00000010) === 0 - ) { - µb.toggleHostnameSwitch({ - name: 'no-scripting', - hostname: hn, - state: false, - }); - } - if ( µb.userSettings.advancedUserEnabled ) { if ( - (curProfileBits & 0b00000100) !== 0 && - (newProfileBits & 0b00000100) === 0 + (curProfileBits & 0b00000010) !== 0 && + (newProfileBits & 0b00000010) === 0 ) { - µb.toggleFirewallRule({ - srcHostname: hn, - desHostname: '*', - requestType: '3p', - action: 3, + µb.toggleHostnameSwitch({ + name: 'no-scripting', + hostname: hn, + state: false, }); } - if ( - (curProfileBits & 0b00001000) !== 0 && - (newProfileBits & 0b00001000) === 0 - ) { - µb.toggleFirewallRule({ - srcHostname: hn, - desHostname: '*', - requestType: '3p-script', - action: 3, - }); + if ( µb.userSettings.advancedUserEnabled ) { + if ( + (curProfileBits & 0b00000100) !== 0 && + (newProfileBits & 0b00000100) === 0 + ) { + µb.toggleFirewallRule({ + srcHostname: hn, + desHostname: '*', + requestType: '3p', + action: 3, + }); + } + if ( + (curProfileBits & 0b00001000) !== 0 && + (newProfileBits & 0b00001000) === 0 + ) { + µb.toggleFirewallRule({ + srcHostname: hn, + desHostname: '*', + requestType: '3p-script', + action: 3, + }); + } + if ( + (curProfileBits & 0b00010000) !== 0 && + (newProfileBits & 0b00010000) === 0 + ) { + µb.toggleFirewallRule({ + srcHostname: hn, + desHostname: '*', + requestType: '3p-frame', + action: 3, + }); + } } - if ( - (curProfileBits & 0b00010000) !== 0 && - (newProfileBits & 0b00010000) === 0 - ) { - µb.toggleFirewallRule({ - srcHostname: hn, - desHostname: '*', - requestType: '3p-frame', - action: 3, - }); - } - } - if ( newProfileBits & 0b00000001 ) { - vAPI.tabs.reload(tab.id); - } -}; + // Reload the target tab? + if ( (newProfileBits & 0b00000001) === 0 ) { return; } + + // Reload: use a timer to coalesce bursts of reload commands. + let timer = reloadTimers.get(tab.id); + if ( timer !== undefined ) { + clearTimeout(timer); + } + timer = vAPI.setTimeout( + tabId => { + reloadTimers.delete(tabId); + vAPI.tabs.reload(tabId); + }, + 547, + tab.id + ); + reloadTimers.set(tab.id, timer); + }; +})(); vAPI.commands.onCommand.addListener(command => { const µb = µBlock;