Skip to content

Commit

Permalink
Fix #12345 - Inject provider for MV3 via app-init
Browse files Browse the repository at this point in the history
  • Loading branch information
darkwing committed Aug 3, 2022
1 parent ce901f8 commit 76abd30
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
2 changes: 2 additions & 0 deletions app/manifest/v3/_base.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -61,6 +62,7 @@
"name": "__MSG_appName__",
"permissions": [
"storage",
"scripting",
"unlimitedStorage",
"clipboardWrite",
"http://localhost:8545/",
Expand Down
15 changes: 15 additions & 0 deletions app/scripts/app-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
]);
11 changes: 4 additions & 7 deletions app/scripts/contentscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ if (
) {
setupPhishingStream();
} else if (shouldInjectProvider()) {
injectScript(inpageBundle);
if (!isManifestV3()) {
injectScript(inpageBundle);
}
setupStreams();
}

Expand All @@ -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) {
Expand Down

0 comments on commit 76abd30

Please sign in to comment.