P粉7390793182023-08-28 14:20:34
import()< /code>
function. Unlike the unsafe workaround using elements, this workaround runs in the same secure JS environment (isolated world of content scripts) and your imported modules can still access global variables As well as functions for the initial content script, including the built-in
chrome
API, such as chrome.runtime.sendMessage
.
In content_script.js
it looks like
(async () => { const src = chrome.runtime.getURL("your/content_main.js"); const contentMain = await import(src); contentMain.main(); })();
You also need to declare the imported script in the manifest's Web Accessible Resources:
// ManifestV3
"web_accessible_resources": [{ "matches": ["<all_urls>"], "resources": ["your/content_main.js"] }],
// ManifestV2
"web_accessible_resources": [ "your/content_main.js" ]
Learn more details:
chrome.runtime.getURL
李>
Hope it helps.
P粉7970046442023-08-28 11:07:40
First, it’s important to note that as of January 2018, content scripts do not support modules. This workaround works around the restriction to your extension by embedding the module script
tag into the returned page.
Web scripts (or other extensions) can exploit your code and extract/spoof data by using setters/getters, proxies on Object.prototype
and other prototypes functions and /or the global object, because the code inside the script
element runs in the JS context of the page, rather than in the secure isolated JS environment that runs content scripts by default.
A safe workaround is the dynamic import() shown in another answer
here.
This is my manifest.json
:
"content_scripts": [ { "js": [ "content.js" ] }], "web_accessible_resources": [ "main.js", "my-script.js" ]
Please note that I have two scripts in web_accessible_resources
.
This is my content.js
:
'use strict'; const script = document.createElement('script'); script.setAttribute("type", "module"); script.setAttribute("src", chrome.extension.getURL('main.js')); const head = document.head || document.getElementsByTagName("head")[0] || document.documentElement; head.insertBefore(script, head.lastChild);
This will insert main.js
as a module script into the web page.
All my business logic is now in main.js
.
For this method to work, main.js
(and all the scripts I import
into) must be located in web_accessible_resources in the manifest
.
my-script.js
'use strict'; const injectFunction = () => window.alert('hello world'); export {injectFunction};
In main.js
, this is an example of the import script:
'use strict'; import {injectFunction} from './my-script.js'; injectFunction();
This works! No errors were thrown and I was happy. :)