简体   繁体   中英

Manifest v3 background scripts/service worker on Firefox

I'm trying to migrate my browser extension (that I expect to work on Chrome and Firefox) from manifest v2 to v3.

However, I am getting conflicting information about the background section. I did lots of research on google and stack overflow, and no one seems to agree on anything. Also, most information seems to be outdated. Of the best sources I found, lots of places mention that it should be migrated to service_worker ( example ), but it seems that Firefox should still use scripts instead ( source ).

But no matter what I do, I am getting errors. If I only use service worker:

"background": {
    "type": "module", // tried both with and without this option
    "service_worker": "background.ts"
}

The the build command from parcel is happy (seems to use parcel/transformer-webextension underneath), but web-ext fails catastrophically:

WebExtError: installTemporaryAddon: Error:
Error: Could not install add-on at '...':
Error: background.service_worker is currently disabled

Even if I provide the flag --firefox-preview which was supposed to fix this.

Which kinda makes sense, this well-written tutorial claims that Firefox is keeping using scripts for V3, just deprecating the persisent flag (which you can remove or set to false). That is fine, as I wasn't using that anyway.

"background": {
    "scripts": ["background.ts"],
    "persistent": false // `persistent` must either be false or omited; I tried both
}

In fact that is exactly how the official Firefox docs claim V3 should support .

But parcel complains that ^^^ Missing property service\_worker :

包裹错误截图

Ok, so let's try both:

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

But parcel is not happy, with Invalid Web Extension manifest :

包裹错误截图

So no matter what I try, I can't get both parcel and web-ext to be happy at the same time.

It seems that Chrome wants one thing, and Firefox wants another, but no matter what I try, I can't even run my extension on neither browser. So not only it seems I can't have a V3 extension for both browsers - I cannot have a V3 extension at all if I want to use both parcel and web-ext (which is indispensable afaik).

I am particularly concerned because according to official chrome sources , Manifest V3 is a "prerequisite for the Featured badge" starting of now , and V2 will be removed by June.

So if I am already penalized for not using V3 now, and have less than 6 months to figure out how to, while none of the available tooling seems to support this version yet? I must be missing something...


Notes: these are the commands I am using to run parcel and web-ext:

        "watch": "parcel watch src/manifest.json --dist-dir distribution --no-cache --no-hmr",
        "start": "web-ext run --firefox-preview"

And these are the versions I am using, both the latest on NPM

        "parcel": "^2.8.2",
        "web-ext": "^7.4.0",

Just faced the same problem:

  • Chrome is not happy with background.scripts and insists on using background.service_worker
  • Firefox doesn't support background.service_worker and wants background.scripts

Manifest v3 is developed by Google, so looks like Firefox team haven't fully implemented it yet. Firefox 109 is the first version to "support" manifest v3 (released on January 17).

I was able to quickly find these tickets 1 and 2 on bugzilla. Doesn't look like it will be fixed any time soon!

What makes matters even worse, Chrome doesn't accept new extensions to its store with manifest v2 any more. This could be the reason why Firefox team decided to enable Manifest v3 extensions even without service workers support.

web-ext is also developed by Mozilla, just having something like this:

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

should allow you (no cross-browser compatibility!) to publish it to Firefox marketplace and use web-ext tool (it also has very verbose inbuilt lint subcommand ./node_modules/.bin/web-ext lint )

I looked through the parcel-bundler change history and Manifest V3 support was added in April, 2022 before Mozilla provided details of their Manifest V3 implementation, which does not include Chrome's addition of Service Workers to replace Background Pages.

Still today, Mozilla has only provided high level details on their vision for Manifest v3, which leaves Firefox's implementation half complete, and (notable to this post) distinct from Chrome's implementation when a background script is included.

This presents several issues for Parcel because their bundler relies on a consistent manifest standard to produce a single distributable that can be used across browsers. The issue you raised is valid and Parcel only supports Chrome's version of Manifest v3. The bigger problem is that Firefox will require a distinct manifest file from other browsers for the foreseeable future, and parcel-bundler only supports a single manifest file that must be named 'manifest.json' .

There is an open pull request to add support for multiple manifest files using distinct names, but it has not been merged yet (at the time of writing). I'm not sure how suitable this solution will be since the changes do not solve for this specific issue.

I opened a bug in the parcel-bundler repo to document the incompatibility with Firefox Manifest V3, but at the moment it is not possible to build a Manifest V3 extension for Firefox that includes background scripts using Parcel.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM