Skip to content

Failing to set correct background script paths #301

@mgonline

Description

@mgonline

Description:

I am using extension.js to enable cross-browser compatibility, but I am encountering issues with the handling of background.scripts in the generated manifest.js file.

What I have tried:

  1. Using Browser Prefixes:
    I initially tried using the browser prefixes for service_worker and scripts to ensure compatibility across different browsers, as referenced in this issue comment.

    Input:

    {
         ...
        "background": {
            "firefox:scripts": ["./src/background/main.ts"],
            "chromium:service_worker": "./src/background/main.ts"
        },
         ...
    }

    Output:

    {
         ...
        "background": {},
         ...
    }

    The background object is empty in the output, which is not the expected behavior.

  2. Without Browser Prefixes:
    Next, I removed the browser-specific prefixes and specified both scripts and service_worker keys without any prefixes.

    Input:

    {
         ...
        "background": {
            "scripts": ["./src/background/main.ts"],
            "service_worker": "./src/background/main.ts"
        },
         ...
    }

    Output:

    {
         ...
        "background": {
            "scripts": ["./src/background/main.ts"],
            "service_worker": "background/service_worker.js"
        },
         ...
    }

    This transformation works for Chrome, but it does not function as expected in Firefox as the path in the scripts is incorrect (seems just copied exactly same as what is mentioned in the orignal manifest).

  3. Only service_worker:
    I then attempted to specify only the service_worker key, which obviously wouldn't work on Firefox due to the known issue I mentioned earlier.

    Input:

    {
         ...
        "background": {
            "service_worker": "./src/background/main.ts"
        },
         ...
    }

    Output:

    {
         ...
        "background": {
            "service_worker": "background/service_worker.js"
        },
         ...
    }

    However, note that the transformation of the service_worker path is correct. It would work on Chrome, but not on Firefox.

  4. Only scripts:
    Finally, I tested specifying only the scripts key, which would obviously break compatibility with Chrome, as Chrome only supports scripts in manifest v2.

    Input:

    {
         ...
        "background": {
            "scripts": ["./src/background/main.ts"]
        },
         ...
    }

    Output:

    {
         ...
        "background": {
            "scripts": ["background/scripts.js"]
        },
         ...
    }

    Note that, again, the scripts path was correctly transformed! While this transformation works for Firefox, but it does not ensure cross-browser compatibility.

Reference:

  • Earlier issue that raised similar concern but not quite the same.
  • Inconsistencies across browsers for scripts, page, and service_worker properties of background is detailed in the MDN documentation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions