From b779f1f7c9deea32970b93e11a8d33ef7efe5db8 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Sat, 12 Dec 2020 14:33:49 -0500 Subject: [PATCH] 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. --- src/js/background.js | 2 +- src/js/commands.js | 5 ++- src/js/scriptlets/load-3p-css.js | 70 ++++++++++++++++++++++++++++++++ src/js/ublock.js | 10 +++++ 4 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 src/js/scriptlets/load-3p-css.js diff --git a/src/js/background.js b/src/js/background.js index d73551cd9..1c9cd5205 100644 --- a/src/js/background.js +++ b/src/js/background.js @@ -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', diff --git a/src/js/commands.js b/src/js/commands.js index c30b8627b..a695427fb 100644 --- a/src/js/commands.js +++ b/src/js/commands.js @@ -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 @@ -104,6 +106,7 @@ const relaxBlockingMode = (( ) => { (newProfileBits & 0b00000100) === 0 ) { µb.toggleFirewallRule({ + tabId: noReload ? tab.id : undefined, srcHostname: hn, desHostname: '*', requestType: '3p', @@ -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); diff --git a/src/js/scriptlets/load-3p-css.js b/src/js/scriptlets/load-3p-css.js new file mode 100644 index 000000000..1d0df772c --- /dev/null +++ b/src/js/scriptlets/load-3p-css.js @@ -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; diff --git a/src/js/ublock.js b/src/js/ublock.js index bc765036f..9076f3da3 100644 --- a/src/js/ublock.js +++ b/src/js/ublock.js @@ -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', + }); + } }; /******************************************************************************/