Skip to content

Commit 4792e0e

Browse files
committed
Coalesce tab reloads in burst "relax blocking mode" ops
Quickly firing "Relax blocking mode" commands will cause the tab to reload only once.
1 parent ad0315a commit 4792e0e

File tree

1 file changed

+78
-60
lines changed

1 file changed

+78
-60
lines changed

src/js/commands.js

Lines changed: 78 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -38,77 +38,95 @@
3838

3939
if ( µBlock.canUseShortcuts === false ) { return; }
4040

41-
const relaxBlockingMode = function(tab) {
42-
if ( tab instanceof Object === false || tab.id <= 0 ) { return; }
41+
const relaxBlockingMode = (( ) => {
42+
const reloadTimers = new Map();
4343

44-
const µb = µBlock;
45-
const normalURL = µb.normalizePageURL(tab.id, tab.url);
44+
return function(tab) {
45+
if ( tab instanceof Object === false || tab.id <= 0 ) { return; }
4646

47-
if ( µb.getNetFilteringSwitch(normalURL) === false ) { return; }
47+
const µb = µBlock;
48+
const normalURL = µb.normalizePageURL(tab.id, tab.url);
4849

49-
const hn = µb.URI.hostnameFromURI(normalURL);
50-
const curProfileBits = µb.blockingModeFromHostname(hn);
51-
let newProfileBits;
52-
for ( const profile of µb.liveBlockingProfiles ) {
53-
if ( (curProfileBits & profile.bits & ~1) !== curProfileBits ) {
54-
newProfileBits = profile.bits;
55-
break;
56-
}
57-
}
50+
if ( µb.getNetFilteringSwitch(normalURL) === false ) { return; }
5851

59-
// TODO: Reset to original blocking profile?
60-
if ( newProfileBits === undefined ) { return; }
61-
62-
if (
63-
(curProfileBits & 0b00000010) !== 0 &&
64-
(newProfileBits & 0b00000010) === 0
65-
) {
66-
µb.toggleHostnameSwitch({
67-
name: 'no-scripting',
68-
hostname: hn,
69-
state: false,
70-
});
71-
}
72-
if ( µb.userSettings.advancedUserEnabled ) {
73-
if (
74-
(curProfileBits & 0b00000100) !== 0 &&
75-
(newProfileBits & 0b00000100) === 0
76-
) {
77-
µb.toggleFirewallRule({
78-
srcHostname: hn,
79-
desHostname: '*',
80-
requestType: '3p',
81-
action: 3,
82-
});
52+
const hn = µb.URI.hostnameFromURI(normalURL);
53+
const curProfileBits = µb.blockingModeFromHostname(hn);
54+
let newProfileBits;
55+
for ( const profile of µb.liveBlockingProfiles ) {
56+
if ( (curProfileBits & profile.bits & ~1) !== curProfileBits ) {
57+
newProfileBits = profile.bits;
58+
break;
59+
}
8360
}
61+
62+
// TODO: Reset to original blocking profile?
63+
if ( newProfileBits === undefined ) { return; }
64+
8465
if (
85-
(curProfileBits & 0b00001000) !== 0 &&
86-
(newProfileBits & 0b00001000) === 0
66+
(curProfileBits & 0b00000010) !== 0 &&
67+
(newProfileBits & 0b00000010) === 0
8768
) {
88-
µb.toggleFirewallRule({
89-
srcHostname: hn,
90-
desHostname: '*',
91-
requestType: '3p-script',
92-
action: 3,
69+
µb.toggleHostnameSwitch({
70+
name: 'no-scripting',
71+
hostname: hn,
72+
state: false,
9373
});
9474
}
95-
if (
96-
(curProfileBits & 0b00010000) !== 0 &&
97-
(newProfileBits & 0b00010000) === 0
98-
) {
99-
µb.toggleFirewallRule({
100-
srcHostname: hn,
101-
desHostname: '*',
102-
requestType: '3p-frame',
103-
action: 3,
104-
});
75+
if ( µb.userSettings.advancedUserEnabled ) {
76+
if (
77+
(curProfileBits & 0b00000100) !== 0 &&
78+
(newProfileBits & 0b00000100) === 0
79+
) {
80+
µb.toggleFirewallRule({
81+
srcHostname: hn,
82+
desHostname: '*',
83+
requestType: '3p',
84+
action: 3,
85+
});
86+
}
87+
if (
88+
(curProfileBits & 0b00001000) !== 0 &&
89+
(newProfileBits & 0b00001000) === 0
90+
) {
91+
µb.toggleFirewallRule({
92+
srcHostname: hn,
93+
desHostname: '*',
94+
requestType: '3p-script',
95+
action: 3,
96+
});
97+
}
98+
if (
99+
(curProfileBits & 0b00010000) !== 0 &&
100+
(newProfileBits & 0b00010000) === 0
101+
) {
102+
µb.toggleFirewallRule({
103+
srcHostname: hn,
104+
desHostname: '*',
105+
requestType: '3p-frame',
106+
action: 3,
107+
});
108+
}
105109
}
106-
}
107110

108-
if ( newProfileBits & 0b00000001 ) {
109-
vAPI.tabs.reload(tab.id);
110-
}
111-
};
111+
// Reload the target tab?
112+
if ( (newProfileBits & 0b00000001) === 0 ) { return; }
113+
114+
// Reload: use a timer to coalesce bursts of reload commands.
115+
let timer = reloadTimers.get(tab.id);
116+
if ( timer !== undefined ) {
117+
clearTimeout(timer);
118+
}
119+
timer = vAPI.setTimeout(
120+
tabId => {
121+
reloadTimers.delete(tabId);
122+
vAPI.tabs.reload(tabId);
123+
},
124+
547,
125+
tab.id
126+
);
127+
reloadTimers.set(tab.id, timer);
128+
};
129+
})();
112130

113131
vAPI.commands.onCommand.addListener(command => {
114132
const µb = µBlock;

0 commit comments

Comments
 (0)