From bccd5a02649fb6f5fe748b78539b1d574fbb48b4 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Thu, 27 Oct 2022 14:16:21 -0400 Subject: [PATCH] Harden scriptlet injections This commit make it so scriptlet injections will occur at the earliest possible time on all platform. This should also fix the case reported at: - https://www.reddit.com/r/uBlockOrigin/comments/ye6abt/ Which is caused by the fact that there is no webNavigation events being fired by the browser. In such case, the changes here will make it so that uBO will detect that the scriptlet were not injected and will inject them at main content script injection time. --- src/js/background.js | 3 ++- src/js/contentscript.js | 3 +++ src/js/messaging.js | 5 +---- src/js/scriptlet-filtering.js | 1 + src/js/tab.js | 7 +------ 5 files changed, 8 insertions(+), 11 deletions(-) diff --git a/src/js/background.js b/src/js/background.js index 4ac91efc3..cbc12529a 100644 --- a/src/js/background.js +++ b/src/js/background.js @@ -19,6 +19,8 @@ Home: https://github.com/gorhill/uBlock */ +/* globals browser */ + 'use strict'; /******************************************************************************/ @@ -149,7 +151,6 @@ const µBlock = { // jshint ignore:line privacySettingsSupported: vAPI.browserSettings instanceof Object, cloudStorageSupported: vAPI.cloud instanceof Object, canFilterResponseData: typeof browser.webRequest.filterResponseData === 'function', - canInjectScriptletsNow: vAPI.webextFlavor.soup.has('chromium'), // https://github.com/chrisaljoudi/uBlock/issues/180 // Whitelist directives need to be loaded once the PSL is available diff --git a/src/js/contentscript.js b/src/js/contentscript.js index cb75f8cc3..59045fd0f 100644 --- a/src/js/contentscript.js +++ b/src/js/contentscript.js @@ -1362,6 +1362,9 @@ vAPI.DOMFilterer = class { vAPI.messaging.send('contentscript', { what: 'retrieveContentScriptParameters', url: vAPI.effectiveSelf.location.href, + scriptletsInjected: + typeof self.uBO_scriptletsInjected === 'boolean' && + self.uBO_scriptletsInjected, }).then(response => { bootstrapPhase1(response); }); diff --git a/src/js/messaging.js b/src/js/messaging.js index 9de7ba6da..c920db9e4 100644 --- a/src/js/messaging.js +++ b/src/js/messaging.js @@ -773,10 +773,7 @@ const retrieveContentScriptParameters = async function(sender, request) { // https://github.com/uBlockOrigin/uBlock-issues/issues/688#issuecomment-748179731 // For non-network URIs, scriptlet injection is deferred to here. The // effective URL is available here in `request.url`. - if ( - µb.canInjectScriptletsNow === false || - isNetworkURI(sender.frameURL) === false - ) { + if ( request.scriptletsInjected !== true ) { scriptletFilteringEngine.injectNow(request); } diff --git a/src/js/scriptlet-filtering.js b/src/js/scriptlet-filtering.js index 1e6c3b12d..8c2be9ba9 100644 --- a/src/js/scriptlet-filtering.js +++ b/src/js/scriptlet-filtering.js @@ -95,6 +95,7 @@ const contentscriptCode = (( ) => { ) { return; } + self.uBO_scriptletsInjected = true; const injectScriptlets = function(d) { let script; try { diff --git a/src/js/tab.js b/src/js/tab.js index b28e7c6ad..48dd9e32b 100644 --- a/src/js/tab.js +++ b/src/js/tab.js @@ -41,7 +41,6 @@ import { import { domainFromHostname, hostnameFromURI, - isNetworkURI, originFromURI, } from './uri-utils.js'; @@ -930,11 +929,7 @@ vAPI.Tabs = class extends vAPI.Tabs { const pageStore = µb.pageStoreFromTabId(tabId); if ( pageStore === null ) { return; } pageStore.setFrameURL(details); - if ( - µb.canInjectScriptletsNow && - isNetworkURI(url) && - pageStore.getNetFilteringSwitch() - ) { + if ( pageStore.getNetFilteringSwitch() ) { scriptletFilteringEngine.injectNow(details); } }