Skip to content

Commit b779f1f

Browse files
committed
Dynamically reload 3p css when noop-ing "3rd-party" cell
This should improve usability of uBO's hard-mode and "relax blocking mode" operations. This is the new default behavior. The previous behavior of forcing a reload of the page can be re-enabled by simply setting the `3p` bit of the advanced setting `blockingProfiles` to 1.
1 parent 64571a3 commit b779f1f

File tree

4 files changed

+85
-2
lines changed

4 files changed

+85
-2
lines changed

src/js/background.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ const µBlock = (( ) => { // jshint ignore:line
4343
autoUpdateDelayAfterLaunch: 180,
4444
autoUpdatePeriod: 7,
4545
benchmarkDatasetURL: 'unset',
46-
blockingProfiles: '11111/#F00 11011/#C0F 11001/#00F 00001',
46+
blockingProfiles: '11111/#F00 11010/#C0F 11001/#00F 00001',
4747
cacheStorageAPI: 'unset',
4848
cacheStorageCompression: true,
4949
cacheControlForFirefox1376932: 'no-cache, no-store, must-revalidate',

src/js/commands.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ const relaxBlockingMode = (( ) => {
8888
// TODO: Reset to original blocking profile?
8989
if ( newProfileBits === undefined ) { return; }
9090

91+
const noReload = (newProfileBits & 0b00000001) === 0;
92+
9193
if (
9294
(curProfileBits & 0b00000010) !== 0 &&
9395
(newProfileBits & 0b00000010) === 0
@@ -104,6 +106,7 @@ const relaxBlockingMode = (( ) => {
104106
(newProfileBits & 0b00000100) === 0
105107
) {
106108
µb.toggleFirewallRule({
109+
tabId: noReload ? tab.id : undefined,
107110
srcHostname: hn,
108111
desHostname: '*',
109112
requestType: '3p',
@@ -135,7 +138,7 @@ const relaxBlockingMode = (( ) => {
135138
}
136139

137140
// Reload the target tab?
138-
if ( (newProfileBits & 0b00000001) === 0 ) { return; }
141+
if ( noReload ) { return; }
139142

140143
// Reload: use a timer to coalesce bursts of reload commands.
141144
let timer = reloadTimers.get(tab.id);

src/js/scriptlets/load-3p-css.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*******************************************************************************
2+
3+
uBlock Origin - a browser extension to block requests.
4+
Copyright (C) 2020-present Raymond Hill
5+
6+
This program is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
This program is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with this program. If not, see {http://www.gnu.org/licenses/}.
18+
19+
Home: https://github.com/gorhill/uBlock
20+
*/
21+
22+
'use strict';
23+
24+
/******************************************************************************/
25+
26+
(( ) => {
27+
if ( typeof vAPI !== 'object' ) { return; }
28+
29+
if ( vAPI.load3rdPartyCSS ) { return; }
30+
vAPI.load3rdPartyCSS = true;
31+
32+
const links = document.querySelectorAll('link[rel="stylesheet"]');
33+
if ( links.length === 0 ) { return; }
34+
35+
const toLoadMaybe = new Map(Array.from(links).map(a => [ a.href, a ]));
36+
37+
for ( const sheet of Array.from(document.styleSheets) ) {
38+
let loaded = false;
39+
try {
40+
loaded = sheet.rules.length !== 0;
41+
} catch(ex) {
42+
}
43+
if ( loaded ) { continue; }
44+
const link = toLoadMaybe.get(sheet.href);
45+
if ( link === undefined ) { continue; }
46+
toLoadMaybe.delete(sheet.href);
47+
const clone = link.cloneNode(true);
48+
link.replaceWith(clone);
49+
}
50+
})();
51+
52+
53+
54+
55+
56+
57+
58+
59+
/*******************************************************************************
60+
61+
DO NOT:
62+
- Remove the following code
63+
- Add code beyond the following code
64+
Reason:
65+
- https://github.com/gorhill/uBlock/pull/3721
66+
- uBO never uses the return value from injected content scripts
67+
68+
**/
69+
70+
void 0;

src/js/ublock.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,9 +509,19 @@ const matchBucket = function(url, hostname, bucket, start) {
509509
// https://github.com/chrisaljoudi/uBlock/issues/420
510510
this.cosmeticFilteringEngine.removeFromSelectorCache(srcHostname, 'net');
511511

512+
if ( details.tabId === undefined ) { return; }
513+
512514
if ( requestType.startsWith('3p') ) {
513515
this.updateToolbarIcon(details.tabId, 0b100);
514516
}
517+
518+
if ( requestType === '3p' && action === 3 ) {
519+
vAPI.tabs.executeScript(details.tabId, {
520+
file: '/js/scriptlets/load-3p-css.js',
521+
allFrames: true,
522+
runAt: 'document_idle',
523+
});
524+
}
515525
};
516526

517527
/******************************************************************************/

0 commit comments

Comments
 (0)