Skip to content

Commit

Permalink
Coalesce tab reloads in burst "relax blocking mode" ops
Browse files Browse the repository at this point in the history
Quickly firing "Relax blocking mode" commands will
cause the tab to reload only once.
  • Loading branch information
gorhill committed Sep 8, 2019
1 parent ad0315a commit 4792e0e
Showing 1 changed file with 78 additions and 60 deletions.
138 changes: 78 additions & 60 deletions src/js/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

// 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
) {
µb.toggleFirewallRule({
srcHostname: hn,
desHostname: '*',
requestType: '3p',
action: 3,
});
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; }

if (
(curProfileBits & 0b00001000) !== 0 &&
(newProfileBits & 0b00001000) === 0
(curProfileBits & 0b00000010) !== 0 &&
(newProfileBits & 0b00000010) === 0
) {
µb.toggleFirewallRule({
srcHostname: hn,
desHostname: '*',
requestType: '3p-script',
action: 3,
µb.toggleHostnameSwitch({
name: 'no-scripting',
hostname: hn,
state: false,
});
}
if (
(curProfileBits & 0b00010000) !== 0 &&
(newProfileBits & 0b00010000) === 0
) {
µb.toggleFirewallRule({
srcHostname: hn,
desHostname: '*',
requestType: '3p-frame',
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 ( 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;
Expand Down

0 comments on commit 4792e0e

Please sign in to comment.