From f25a437fd1050e554ed9ab24c5e36b093a29a406 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Fri, 11 Apr 2025 18:20:51 -0400 Subject: [PATCH] Code review re. scriptlets lookup Possibly fixes a race condition at browser launch causing empty scriptlets to be injected (and cached). --- src/js/redirect-engine.js | 1 + src/js/scriptlet-filtering-core.js | 22 ++++++++++++---------- src/js/scriptlet-filtering.js | 1 + src/js/static-ext-filtering-db.js | 10 +++++----- 4 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/js/redirect-engine.js b/src/js/redirect-engine.js index 5e7a55884..14df52200 100644 --- a/src/js/redirect-engine.js +++ b/src/js/redirect-engine.js @@ -460,6 +460,7 @@ class RedirectEngine { for ( const [ token, entry ] of this.resources ) { this.resources.set(token, RedirectEntry.fromDetails(entry)); } + this.modifyTime = Date.now(); return true; } diff --git a/src/js/scriptlet-filtering-core.js b/src/js/scriptlet-filtering-core.js index eda887634..5c56423c2 100644 --- a/src/js/scriptlet-filtering-core.js +++ b/src/js/scriptlet-filtering-core.js @@ -25,11 +25,6 @@ import { redirectEngine as reng } from './redirect-engine.js'; /******************************************************************************/ -const $mainWorldMap = new Map(); -const $isolatedWorldMap = new Map(); - -/******************************************************************************/ - // For debugging convenience: all the top function calls will appear // at the bottom of a generated content script const codeSorter = (a, b) => { @@ -243,18 +238,27 @@ export class ScriptletFilteringEngine { } } + const mainWorldMap = new Map(); + const isolatedWorldMap = new Map(); + for ( const token of scriptlets ) { - lookupScriptlet(token, $mainWorldMap, $isolatedWorldMap, options.debug); + lookupScriptlet(token, mainWorldMap, isolatedWorldMap, options.debug); + } + + if ( scriptlets.size !== 0 ) { + if ( mainWorldMap.size === 0 ) { + if ( isolatedWorldMap.size === 0 ) { return; } + } } const mainWorldCode = []; - for ( const js of $mainWorldMap.values() ) { + for ( const js of mainWorldMap.values() ) { mainWorldCode.push(js); } mainWorldCode.sort(codeSorter); const isolatedWorldCode = []; - for ( const js of $isolatedWorldMap.values() ) { + for ( const js of isolatedWorldMap.values() ) { isolatedWorldCode.push(js); } isolatedWorldCode.sort(codeSorter); @@ -267,8 +271,6 @@ export class ScriptletFilteringEngine { ...Array.from(exceptions).map(a => decompile(a, true)), ], }; - $mainWorldMap.clear(); - $isolatedWorldMap.clear(); const scriptletGlobals = options.scriptletGlobals || {}; diff --git a/src/js/scriptlet-filtering.js b/src/js/scriptlet-filtering.js index f71b07c2f..c61444bb6 100644 --- a/src/js/scriptlet-filtering.js +++ b/src/js/scriptlet-filtering.js @@ -328,6 +328,7 @@ export class ScriptletFilteringEngineEx extends ScriptletFilteringEngine { if ( typeof details.frameId !== 'number' ) { return; } const hostname = hostnameFromURI(details.url); + if ( hostname === '' ) { return; } const domain = domainFromHostname(hostname); const scriptletDetails = this.retrieve({ diff --git a/src/js/static-ext-filtering-db.js b/src/js/static-ext-filtering-db.js index 2e2347491..6d69fd634 100644 --- a/src/js/static-ext-filtering-db.js +++ b/src/js/static-ext-filtering-db.js @@ -92,9 +92,9 @@ export class StaticExtFilteringHostnameDB { if ( target.includes('/') ) { return this.#storeMatcher(target, iStr); } - const iList = this.#hostnameToStringListMap.get(target); + const iList = this.#hostnameToStringListMap.get(target) ?? 0; this.#hostnameToStringListMap.set(target, this.#linkedLists.length); - this.#linkedLists.push(iStr, iList !== undefined ? iList : 0); + this.#linkedLists.push(iStr, iList); } #storeMatcher(target, iStr) { @@ -105,11 +105,11 @@ export class StaticExtFilteringHostnameDB { this.#matcherSlots.push({ isRegex, hn, pn, iList: 0 }); this.#matcherMap.set(target, iMatcher); if ( isRegex === false ) { - const iMatcherList = this.#hostnameToMatcherListMap.get(hn) || 0; + const iMatcherList = this.#hostnameToMatcherListMap.get(hn) ?? 0; this.#hostnameToMatcherListMap.set(hn, this.#linkedLists.length); this.#linkedLists.push(iMatcher, iMatcherList); } else { - const iMatcherList = this.#hostnameToMatcherListMap.get('') || 0; + const iMatcherList = this.#hostnameToMatcherListMap.get('') ?? 0; this.#hostnameToMatcherListMap.set('', this.#linkedLists.length); this.#linkedLists.push(iMatcher, iMatcherList); } @@ -176,7 +176,7 @@ export class StaticExtFilteringHostnameDB { } #retrieveSpecificsByRegex(hn, out, hostname, pathname) { - let iMatchList = this.#hostnameToMatcherListMap.get(hn) || 0; + let iMatchList = this.#hostnameToMatcherListMap.get(hn) ?? 0; while ( iMatchList !== 0 ) { const iMatchSlot = this.#linkedLists[iMatchList+0]; const matcher = this.#matcherSlots[iMatchSlot];