1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-10-06 09:37:12 +02:00

Coalesce tab reloads in burst "relax blocking mode" ops

Quickly firing "Relax blocking mode" commands will
cause the tab to reload only once.
This commit is contained in:
Raymond Hill 2019-09-08 12:52:28 -04:00
parent ad0315a4cd
commit 4792e0e291
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2

View File

@ -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;