[英]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.我正在尝试将我的浏览器扩展(我希望在 Chrome 和 Firefox 上运行)从清单 v2 迁移到 v3。
However, I am getting conflicting information about the background
section.但是,我收到有关
background
部分的相互矛盾的信息。 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 ).在我找到的最好的来源中,很多地方都提到它应该迁移到
service_worker
( 示例),但似乎 Firefox 应该仍然使用scripts
( 来源)。
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:来自
parcel
的构建命令很高兴(似乎在下面使用了parcel/transformer-webextension
),但是web-ext
灾难性地失败了:
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.即使我提供了应该解决这个问题的标志
--firefox-preview
。
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).这是有道理的, 这个写得很好的教程声称 Firefox 一直在使用 V3 的
scripts
,只是弃用了persisent
标志(您可以将其删除或设置为 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 .事实上,这正是官方 Firefox 文档声称 V3 应该支持的方式。
But parcel complains that ^^^ Missing property service\_worker
:但是包裹抱怨
^^^ 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
:但是包裹不满意,带有
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.因此,无论我尝试什么,我都无法同时让
parcel
和web-ext
都满意。
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.似乎 Chrome 想要一件事,而 Firefox 想要另一件事,但无论我尝试什么,我什至无法在这两个浏览器上运行我的扩展程序。 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).因此,不仅看起来我不能为两种浏览器都提供 V3 扩展——如果我想同时使用
parcel
和web-ext
(这是不可或缺的 afaik),我根本无法拥有 V3 扩展。
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.我特别担心,因为根据官方 chrome 来源,Manifest V3 从现在开始是“特色徽章的先决条件”,而 V2 将在 6 月删除。
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?因此,如果我已经因为现在不使用 V3 而受到惩罚,并且只有不到 6 个月的时间来弄清楚如何去做,而似乎没有可用的工具支持这个版本呢? I must be missing something...
我肯定错过了什么...
Notes: these are the commands I am using to run parcel and web-ext:注意:这些是我用来运行 parcel 和 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这些是我正在使用的版本,都是 NPM 上的最新版本
"parcel": "^2.8.2",
"web-ext": "^7.4.0",
Just faced the same problem:刚遇到同样的问题:
background.scripts
and insists on using background.service_worker
background.scripts
不满意,坚持使用background.service_worker
background.service_worker
and wants background.scripts
background.service_worker
需要background.scripts
Manifest v3 is developed by Google, so looks like Firefox team haven't fully implemented it yet. Manifest v3 是由谷歌开发的,所以看起来 Firefox 团队还没有完全实现它。 Firefox 109 is the first version to "support" manifest v3 (released on January 17).
Firefox 109 是第一个“支持”manifest v3(1 月 17 日发布)的版本。
I was able to quickly find these tickets 1 and 2 on bugzilla.我能够在 bugzilla 上快速找到这些票证1和2 。 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.更糟糕的是,Chrome 不再接受带有 manifest v2 的商店的新扩展。 This could be the reason why Firefox team decided to enable Manifest v3 extensions even without service workers support.
这可能是 Firefox 团队决定在没有服务人员支持的情况下启用 Manifest v3 扩展的原因。
web-ext
is also developed by Mozilla, just having something like this: web-ext
也是由 Mozilla 开发的,只是有这样的东西:
"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
)应该允许你(没有跨浏览器兼容性!)将它发布到 Firefox 市场并使用
web-ext
工具(它还有非常冗长的内置 lint 子命令./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.我查看了 parcel-bundler 更改历史记录,并在 2022 年 4 月添加了 Manifest V3 支持,然后Mozilla 提供了他们的 Manifest V3 实现的详细信息,其中不包括 Chrome 添加的 Service Workers 以替换后台页面。
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.时至今日,Mozilla 只提供了关于他们对 Manifest v3 愿景的高级细节,这使得 Firefox 的实现只完成了一半,并且(在这篇文章中值得注意)在包含后台脚本时与 Chrome 的实现不同。
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.这给 Parcel 带来了几个问题,因为他们的捆绑器依赖于一致的清单标准来生成可以跨浏览器使用的单一可分发文件。 The issue you raised is valid and Parcel only supports Chrome's version of Manifest v3.
您提出的问题是有效的,Parcel 仅支持 Chrome 版本的 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' .
更大的问题是 Firefox 在可预见的未来将需要与其他浏览器不同的清单文件,而 parcel-bundler 仅支持必须命名为 '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.我在 parcel-bundler repo 中打开了一个错误来记录与 Firefox Manifest V3 的不兼容性,但目前无法为 Firefox 构建包含使用 Parcel 的后台脚本的 Manifest V3 扩展。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.