我正在构建一个浏览器扩展。我想在用户单击扩展程序的图标时触发通知。当我尝试在 Chrome 中加载扩展程序时,出现此错误:Service Worker 注册失败
作为错误的来源,它在清单中突出显示了“background.js”(您可以在下面找到)。没有提供更多信息。然而,只有当我在background.ts中有第一个片段时才会出现问题,如评论所示:
import { browser } from "webextension-polyfill-ts";
// This code prevents the compiled extension to be loaded into Chrome
browser.browserAction.onClicked.addListener(() => {
console.log('browserAction.onClicked');
});
// I also have this code in the file, which works just fine.
browser.runtime.onMessage.addListener(() => {
console.log('browser.runtime.onMessage');
});
(注意,这当然是编译成Javascript的)
当然我怀疑是权限问题,但找不到任何可疑的地方。这是完整的清单:
{
"name": "Test",
"description": "Test",
"version": "0.0.1",
"manifest_version": 3,
"permissions": ["storage", "activeTab", "scripting"],
"background": {
"service_worker": "background.js"
},
"action": {
"default_title": "Click to show an alert"
},
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": [
"js/content-script.js"
]
}
],
"content_security_policy": {}
}
有什么想法吗?谢谢!
将此包含在您的清单中的背景键下
"background": {
"service_worker": "background.js"
"type": "module"
}