简体   繁体   English

chrome 扩展 - 服务工作者注册失败 - 清单 V3

[英]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'});
});

How to investigate service worker registration error如何调查 Service Worker 注册错误

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未定义。

Problem问题

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 的官方文档页面的开头指出

Solution解决方案

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.

相关问题 带有 React 的 Chrome 扩展清单 v3 - Chrome extension manifest v3 with React Chrome 89.0.4389.114 扩展权限(Manifest V3) - Chrome 89.0.4389.114 Extension Permissions (Manifest V3) Chrome 扩展 - 从清单 v2 迁移到 v3 - Chrome extension - migrate from manifest v2 to v3 Firefox 上的 Manifest v3 后台脚本/service worker - Manifest v3 background scripts/service worker on Firefox 在 Manifest v3 中 chrome.alarms 过去后,无法从选项卡中的 service_worker 注入 CSS 样式 - Unable to inject CSS style from service_worker in tab after chrome.alarms has elapsed in Manifest v3 将我的 Chrome 扩展程序迁移到 Manifest v3 时出错 - Error with migrating my Chrome Extension to Manifest v3 在清单 v3 扩展中正确使用 chrome.tabCapture - Properly using chrome.tabCapture in a manifest v3 extension 使 chrome 扩展仅在某些站点上工作(清单 v3) - Make chrome extension work only on certain sites (manifest v3) Chrome manifest v3 - 是否有可行的解决方法在 Chrome 扩展中使用 Google 的文件选择器? - Chrome manifest v3 - is there a viable workaround to use Google's File Picker in a Chrome extension? chrome manifest v3 上的基本代理身份验证 - Basic Proxy Authentication on chrome manifest v3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM