From 6af4494946db68ef389ac8ed3f004001e78c5f6a Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Sat, 21 Oct 2023 07:11:12 -0400 Subject: [PATCH] [mv3] Honor scriptlets' target world in Firefox When the target world of a scriptlet is the ISOLATED one, skip Blob-based injection in Firefox, as the current world is always the ISOLATED one. This should make ISOLATED world-based scriptlets more reliable (i.e. execute sooner) in Firefox. --- platform/mv3/make-scriptlets.js | 3 +++ platform/mv3/scriptlets/scriptlet.template.js | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/platform/mv3/make-scriptlets.js b/platform/mv3/make-scriptlets.js index 9e9559e17..2a135d2e7 100644 --- a/platform/mv3/make-scriptlets.js +++ b/platform/mv3/make-scriptlets.js @@ -59,6 +59,7 @@ export function init() { const entry = { name: fn.name, code: fn.toString(), + world: scriptlet.world || 'MAIN', dependencies: scriptlet.dependencies, requiresTrust: scriptlet.requiresTrust === true, }; @@ -96,6 +97,7 @@ export function compile(details) { scriptletFiles.set(scriptletToken, { name: resourceEntry.name, code: createScriptletCoreCode(scriptletToken), + world: resourceEntry.world, args: new Map(), hostnames: new Map(), entities: new Map(), @@ -165,6 +167,7 @@ export async function commit(rulesetId, path, writeFn) { ); content = safeReplace(content, /\$rulesetId\$/, rulesetId, 0); content = safeReplace(content, /\$scriptletName\$/, details.name, 0); + content = safeReplace(content, '$world$', details.world); content = safeReplace(content, 'self.$argsList$', JSON.stringify(Array.from(details.args.keys()).map(a => JSON.parse(a))) diff --git a/platform/mv3/scriptlets/scriptlet.template.js b/platform/mv3/scriptlets/scriptlet.template.js index 841aa40cc..503d85ed1 100644 --- a/platform/mv3/scriptlets/scriptlet.template.js +++ b/platform/mv3/scriptlets/scriptlet.template.js @@ -132,8 +132,10 @@ argsList.length = 0; // 'MAIN' world not yet supported in Firefox, so we inject the code into // 'MAIN' ourself when environment in Firefox. +const targetWorld = '$world$'; + // Not Firefox -if ( typeof wrappedJSObject !== 'object' ) { +if ( typeof wrappedJSObject !== 'object' || targetWorld === 'ISOLATED' ) { return uBOL_$scriptletName$(); }