mirror of
https://github.com/gorhill/uBlock.git
synced 2024-11-05 18:32:30 +01: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:
parent
ad0315a4cd
commit
4792e0e291
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user