Since the latest Firefox supports ES Modules without flags as well as Chrome, I would like to use import
/export
for my web extension (add-on). Pop up, background and option pages were OK by simply using <script type="module">
.
How do you make modules work in content script?
I tried and saw followings:
(1) Just write import
in a acript declared at content_scripts.js
in manifest.json
import Foo from './foo.js';
(Chrome) Uncaught SyntaxError: Unexpected identifier
Firefox doesn't work with no errors.
(2) Run through tabs.executeScript()
browser.tabs.executeScript(undefined, {
file: "foo.js",
});
(Chrome) Uncaught SyntaxError: Unexpected identifier
(Firefox) Error: import declarations may only appear at top level of a module
(3) Insert a script element created with type="module"
const el = document.createElement("script");
el.src = browser.extension.getURL("foo.js");
el.type = "module";
document.body.appendChild(el);
(Chrome) Uncaught (in promise) ReferenceError: browser is not defined
(Firefox) ReferenceError: browser is not defined
Do you have any other idea?