5

Inside the background script of my WebExtension I want to use functions/constants from another JavaScript file. But as simple as it sounds, I cannot get it to work.

I'm using the following four files as a minimal example for my problem:

  1. manifest.json:

    {
        "manifest_version": 2,
        "name": "MinimalCompleteReproducibleExample",
        "version": "0.0.0.0",
    
        "background": {
            "page": "background.html"
        }
    }
    

    It basically just loads background.html as the background script/page.

  2. background.html:

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
        </head>
        <body>
            <script type="module" src="/mylib.js"></script>
            <script src="background.js"></script>
        </body>
    </html>
    

    It loads the "external" JavaScript file mylib.js that contains the functions/constants I want to reuse and the actual background script background.js.

  3. mylib.js:

    export const foo = 42;
    

    It just exports the constant foo.

  4. background.js:

    console.log(foo);
    

    It tries to use the content of the "external" JavaScript file mylib.js.

When I load this extension, I get the following error message in the debugger:

Uncaught ReferenceError: foo is not defined

That seems to indicate that the content of mylib.js is not available.

Before that, I did load the background.js directly in manifest.json and added the following line to background.js:

import { foo } from "/mylib.js";

But that does not seem to be allowed in WebExtensions and also does not work:

Uncaught SyntaxError: import declarations may only appear at top level of a module

So can someone tell me, how to make another JavaScript file available inside a background script?

3
  • 1
    Add type="module" to your background script declaration as well. Commented Aug 27, 2020 at 16:04
  • 1
    Please note that using a remote script in a Firefox extension will result in rejection of the extension by AMO. Commented Aug 28, 2020 at 4:57
  • 1
    @erosman Thanks for the advice. Fortunateley, I don't plan to pull it in dynamically. I will ship the library within the extension. Commented Aug 28, 2020 at 8:18

2 Answers 2

2

wOxxOm's comment helped me solving it. Two changes are necessary:

  • Addition of type="module" to <script src="background.js"></script> in background.html
  • Addition of import { foo } from "/mylib.js"; to background.js

The line <script type="module" src="/mylib.js"></script> in background.html can then be omitted.

Complete working example:

  1. manifest.json:
    {
        "manifest_version": 2,
        "name": "MinimalCompleteReproducibleExample",
        "version": "0.0.0.0",
    
        "background": {
            "page": "background.html"
        }
    }
    
  2. background.html:
    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
        </head>
        <body>
            <script type="module" src="/background.js"></script>
        </body>
    </html>
    
  3. mylib.js:
    export const foo = 42;
    
  4. background.js:
    import { foo } from "/mylib.js";
    console.log(foo);
    
Sign up to request clarification or add additional context in comments.

Comments

0

As I was looking around how to fix the problem for quiet a while and did not find my solution anywhere:

There is also the option to set "type": "module" for a background script as described here: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/background

so in manifest.json it would be as follows:

"background": {
        "scripts": ["background.js"],
        "type": "module"
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.