[英]chrome extension - Service worker registration failed - manifest V3
I am trying to build a simple extension, but I get " Service worker registration failed " error on the background.js line.我正在尝试构建一个简单的扩展,但在 background.js 行上出现“服务工作者注册失败”错误。 I read some other posts using a wrapper file to load the background.js but I am using chrome version 105 and shouldn't need to use the wrapper fix.
我阅读了其他一些使用包装文件加载 background.js 的帖子,但我使用的是 chrome 版本 105,不需要使用包装修复。
manifest.jason:清单.杰森:
{
"name": "Test",
"description" : "Test",
"version": "1.0",
"manifest_version": 3,
"background": {
"service_worker": "background.js"
}
}
background.js:背景.js:
chrome.action.onClicked.addListener(tab => {
chrome.tabs.update({url: 'http://example.com'});
});
If you click Errors
in chrome://extensions page you'll see:如果您单击 chrome://extensions 页面中的
Errors
,您将看到:
Uncaught TypeError: Cannot read properties of undefined (reading 'onClicked')
Click it and you'll see the source:点击它,你会看到源代码:
chrome.action.onClicked.addListener(tab => {
It means that chrome.action
is undefined.这意味着
chrome.action
未定义。
Lots of API inside chrome
are present only if your extension's manifest.json lists its associated permission name in "permissions"
or in a special key - this is indicated at the beginning of the official documentation page for an API.仅当您的扩展程序的清单时,
chrome
中的许多 API 才会出现。json 在"permissions"
或特殊键中列出其关联的权限名称 - 这在 ZDB974238714CA8DE634A7CE1D8 的官方文档页面的开头指出
Add "action": {}
to manifest.json.将
"action": {}
添加到 manifest.json。
You can also specify a title and an icon inside, see the documentation , but be careful with default_popup
- it disables onClicked listener when specified ie if you want to use default_popup to show html in this standard popup you'll need to move the inside of your onClicked listener from background.js to the beginning of the popup.js script for your popup.html, which will run every time the popup is shown.您还可以在内部指定标题和图标,请参阅 文档,但请注意
default_popup
- 它在指定时禁用 onClicked 侦听器,即如果您想使用 default_popup 在此标准弹出窗口中显示 html 您需要移动内部您的 onClicked 侦听器从 background.js 到 popup.html 的 popup.js 脚本的开头,每次显示弹出窗口时都会运行。 In your case it's chrome.tabs.update({url: 'http://example.com'});
在您的情况下,它是
chrome.tabs.update({url: 'http://example.com'});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.