Skip to content

Commit

Permalink
Dynamically reload 3p css when noop-ing "3rd-party" cell
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
gorhill committed Dec 12, 2020
1 parent 64571a3 commit b779f1f
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const µBlock = (( ) => { // jshint ignore:line
autoUpdateDelayAfterLaunch: 180,
autoUpdatePeriod: 7,
benchmarkDatasetURL: 'unset',
blockingProfiles: '11111/#F00 11011/#C0F 11001/#00F 00001',
blockingProfiles: '11111/#F00 11010/#C0F 11001/#00F 00001',
cacheStorageAPI: 'unset',
cacheStorageCompression: true,
cacheControlForFirefox1376932: 'no-cache, no-store, must-revalidate',
Expand Down
5 changes: 4 additions & 1 deletion src/js/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ const relaxBlockingMode = (( ) => {
// TODO: Reset to original blocking profile?
if ( newProfileBits === undefined ) { return; }

const noReload = (newProfileBits & 0b00000001) === 0;

if (
(curProfileBits & 0b00000010) !== 0 &&
(newProfileBits & 0b00000010) === 0
Expand All @@ -104,6 +106,7 @@ const relaxBlockingMode = (( ) => {
(newProfileBits & 0b00000100) === 0
) {
µb.toggleFirewallRule({
tabId: noReload ? tab.id : undefined,
srcHostname: hn,
desHostname: '*',
requestType: '3p',
Expand Down Expand Up @@ -135,7 +138,7 @@ const relaxBlockingMode = (( ) => {
}

// Reload the target tab?
if ( (newProfileBits & 0b00000001) === 0 ) { return; }
if ( noReload ) { return; }

// Reload: use a timer to coalesce bursts of reload commands.
let timer = reloadTimers.get(tab.id);
Expand Down
70 changes: 70 additions & 0 deletions src/js/scriptlets/load-3p-css.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*******************************************************************************
uBlock Origin - a browser extension to block requests.
Copyright (C) 2020-present Raymond Hill
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see {http://www.gnu.org/licenses/}.
Home: https://github.com/gorhill/uBlock
*/

'use strict';

/******************************************************************************/

(( ) => {
if ( typeof vAPI !== 'object' ) { return; }

if ( vAPI.load3rdPartyCSS ) { return; }
vAPI.load3rdPartyCSS = true;

const links = document.querySelectorAll('link[rel="stylesheet"]');
if ( links.length === 0 ) { return; }

const toLoadMaybe = new Map(Array.from(links).map(a => [ a.href, a ]));

for ( const sheet of Array.from(document.styleSheets) ) {
let loaded = false;
try {
loaded = sheet.rules.length !== 0;
} catch(ex) {
}
if ( loaded ) { continue; }
const link = toLoadMaybe.get(sheet.href);
if ( link === undefined ) { continue; }
toLoadMaybe.delete(sheet.href);
const clone = link.cloneNode(true);
link.replaceWith(clone);
}
})();








/*******************************************************************************
DO NOT:
- Remove the following code
- Add code beyond the following code
Reason:
- https://github.com/gorhill/uBlock/pull/3721
- uBO never uses the return value from injected content scripts
**/

void 0;
10 changes: 10 additions & 0 deletions src/js/ublock.js
Original file line number Diff line number Diff line change
Expand Up @@ -509,9 +509,19 @@ const matchBucket = function(url, hostname, bucket, start) {
// https://github.com/chrisaljoudi/uBlock/issues/420
this.cosmeticFilteringEngine.removeFromSelectorCache(srcHostname, 'net');

if ( details.tabId === undefined ) { return; }

if ( requestType.startsWith('3p') ) {
this.updateToolbarIcon(details.tabId, 0b100);
}

if ( requestType === '3p' && action === 3 ) {
vAPI.tabs.executeScript(details.tabId, {
file: '/js/scriptlets/load-3p-css.js',
allFrames: true,
runAt: 'document_idle',
});
}
};

/******************************************************************************/
Expand Down

3 comments on commit b779f1f

@gwarser
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you thought about images? Or it will be too much?

@gorhill
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, and all the rest too.

@gorhill
Copy link
Owner Author

@gorhill gorhill commented on b779f1f Dec 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I call "the rest" is not trivial changes, so this will have to wait for next dev cycle. It's not a given images should be loaded, it feels like a personal preference to me, so this might need its own bit.

For frames, I started work on this then thew it away when I realized this was going to demand more work, which I don't want to start in this dev cycle. For scripts, I will need to experiment so also too late for current dev cycle. Probably both will need their specific bit to dictate whether dynamic reload is preferred or not.

Please sign in to comment.