From d589cb3f255d2022fe6c518b4f9d48ba5f9ec611 Mon Sep 17 00:00:00 2001 From: Krzysztof Modras Date: Tue, 19 Nov 2024 15:28:26 +0100 Subject: [PATCH] rename content script api --- src/background/adblocker.js | 68 ++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 32 deletions(-) diff --git a/src/background/adblocker.js b/src/background/adblocker.js index 3ce4e9fda..c8de6280f 100644 --- a/src/background/adblocker.js +++ b/src/background/adblocker.js @@ -87,7 +87,7 @@ export async function reloadMainEngine() { console.info('[adblocker] Main engine reloaded with no filters'); } if (__PLATFORM__ === 'firefox') { - contentScripts.clear(); + contentScripts.unregisterAll(); } } @@ -173,23 +173,40 @@ export const setup = asyncSetup([ const contentScripts = (() => { const map = new Map(); return { - set(key, value) { - this.delete(key); - map.set(key, value); + async register(hostname, code) { + this.unregister(hostname); + try { + const contentScript = await browser.contentScripts.register({ + js: [ + { + code, + }, + ], + allFrames: true, + matches: [`https://*.${hostname}/*`, `http://*.${hostname}/*`], + matchAboutBlank: true, + matchOriginAsFallback: true, + runAt: 'document_start', + }); + map.set(hostname, contentScript); + } catch (e) { + console.warn(e); + contentScripts.unregister(hostname); + } }, - has(key) { - return map.has(key); + isRegistered(hostname) { + return map.has(hostname); }, - delete(key) { - const contentScript = map.get(key); + unregister(hostname) { + const contentScript = map.get(hostname); if (contentScript) { contentScript.unregister(); - map.delete(key); + map.delete(hostname); } }, - clear() { - for (const key of map.keys()) { - this.delete(key); + unregisterAll() { + for (const hostname of map.keys()) { + this.unregister(hostname); } }, }; @@ -226,27 +243,14 @@ async function injectScriptlets(scripts, tabId, frameId, hostname) { if (__PLATFORM__ === 'firefox') { if (scripts.length === 0) { - contentScripts.delete(hostname); - } else if (!contentScripts.has(hostname)) { - try { - const contentScript = await browser.contentScripts.register({ - js: [ - { - code: `(${scriptletInjector.toString()})("${encodeURIComponent(scriptlets)}")`, - }, - ], - allFrames: true, - matches: [`https://*.${hostname}/*`, `http://*.${hostname}/*`], - matchAboutBlank: true, - matchOriginAsFallback: true, - runAt: 'document_start', - }); - contentScripts.set(hostname, contentScript); - } catch (e) { - console.warn(e); - contentScripts.delete(hostname); - } + contentScripts.unregister(hostname); + } else if (!contentScripts.isRegistered(hostname)) { + await contentScripts.register( + hostname, + `(${scriptletInjector.toString()})("${encodeURIComponent(scriptlets)}")`, + ); } + // do nothing if already registered } else { if (scripts.length === 0) return;