From 3b07db6a0370205f48ed7df694ac5c837c63ac7b Mon Sep 17 00:00:00 2001 From: David Walsh Date: Wed, 3 Aug 2022 10:32:22 -0500 Subject: [PATCH] Fix #12345 - Inject provider for MV3 via app-init --- app/manifest/v3/_base.json | 2 ++ app/scripts/app-init.js | 15 +++++++++++++++ app/scripts/contentscript.js | 11 ++++------- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/app/manifest/v3/_base.json b/app/manifest/v3/_base.json index 38494de8fda9..7b28cd2edb0a 100644 --- a/app/manifest/v3/_base.json +++ b/app/manifest/v3/_base.json @@ -47,6 +47,7 @@ ], "default_locale": "en", "description": "__MSG_appDescription__", + "host_permissions": ["file://*/*", "http://*/*", "https://*/*"], "icons": { "16": "images/icon-16.png", "19": "images/icon-19.png", @@ -61,6 +62,7 @@ "name": "__MSG_appName__", "permissions": [ "storage", + "scripting", "unlimitedStorage", "clipboardWrite", "http://localhost:8545/", diff --git a/app/scripts/app-init.js b/app/scripts/app-init.js index 8117566729be..4ae247c5ba3a 100644 --- a/app/scripts/app-init.js +++ b/app/scripts/app-init.js @@ -126,3 +126,18 @@ chrome.runtime.onMessage.addListener(() => { importAllScripts(); return false; }); + +/* + * This content script is injected programmatically because + * MAIN world injection does not work properly via manifest + * https://bugs.chromium.org/p/chromium/issues/detail?id=634381 + */ +chrome.scripting.registerContentScripts([ + { + id: 'inpage', + matches: ['file://*/*', 'http://*/*', 'https://*/*'], + js: ['inpage.js'], + runAt: 'document_start', + world: 'MAIN', + }, +]); diff --git a/app/scripts/contentscript.js b/app/scripts/contentscript.js index 952bffb3fc57..e76237bf2202 100644 --- a/app/scripts/contentscript.js +++ b/app/scripts/contentscript.js @@ -41,7 +41,9 @@ if ( ) { setupPhishingStream(); } else if (shouldInjectProvider()) { - injectScript(inpageBundle); + if (!isManifestV3()) { + injectScript(inpageBundle); + } setupStreams(); } @@ -55,12 +57,7 @@ function injectScript(content) { const container = document.head || document.documentElement; const scriptTag = document.createElement('script'); scriptTag.setAttribute('async', 'false'); - // Inline scripts do not work in MV3 due to more strict security policy - if (isManifestV3()) { - scriptTag.setAttribute('src', browser.runtime.getURL('inpage.js')); - } else { - scriptTag.textContent = content; - } + scriptTag.textContent = content; container.insertBefore(scriptTag, container.children[0]); container.removeChild(scriptTag); } catch (error) {