Service Worker 注册失败.Chrome 扩展程序 [英] Service worker registration failed. Chrome extension
问题描述
我不明白如何在 Service Worker 中从 manifest v2 迁移到 v3.发生错误 Service Worker 注册失败
//manifest.json背景":{service_worker":/script/background/background.js";},
//background.jschrome.runtime.onInstalled.addListener(onInstalled);
请在下面找到您的具体问题的原因以及在控制台日志中未获取失败详细信息的原因.
在 Chrome 93 之前,service worker 文件必须在根路径中manifest.json 在哪里.
这是 Service Worker 规范的限制,对 自 Chrome 93 起 的扩展放宽了.>
如果出于某种原因,您想让您的扩展程序在旧版 Chrome 中使用,则正确的 manifest.json 应如下所示:
背景":{service_worker":background.js";},
相反,要使用任意路径,您需要阻止在旧版 Chrome 中安装:
minimum_chrome_version":93,背景":{service_worker":js/bg/worker-loader.js",类型":模块";},
Type
module
是可选的,从 Chrome 92 开始支持.你现在可以静态导入 ES 模块.对动态导入的支持正在开发.在任何 Chrome 版本中,您都可以使用
importScripts('/path/foo.js', '/path/bar.js');
从其他目录导入脚本.如果 worker 脚本在安装时抛出错误,worker 将不会被注册,并且您将不会在控制台中获得由 Service Worker 触发的错误信息,但只有得到服务工作者注册失败".此行为是由于早于 Chrome 93 的 Chrome 版本中的错误错误造成的.
解决方案:
- 使用 Chrome 93 或更新版本.
有限的解决方法:
- 将所有内容包装在 try/catch 中,如其他答案所示或 使用加载程序"脚本.
典型原因:
- 访问未声明的变量
- 语法错误,如未闭合的括号
- 访问
chrome
API 而不在 manifest.json 的permissions
字段中声明它 - 工作进程崩溃
I don't understand how to migrate from manifest v2 to v3 in part of Service Worker. Occurs error Service worker registration failed
// manifest.json
"background": {
"service_worker": "/script/background/background.js"
},
// background.js
chrome.runtime.onInstalled.addListener(onInstalled);
Please find below the cause for your specific issue and the cause for not getting the details of failure in the console log.
Before Chrome 93, the service worker file must be in the root path where manifest.json is.
This is a limitation of Service Worker specification, relaxed for extensions since Chrome 93.
If, for whatever reason, you want to allow your extension to be used in older Chrome, the correct manifest.json should look like this:
"background": { "service_worker": "background.js" },
Conversely, to use an arbitrary path you need to prevent installation in older Chrome:
"minimum_chrome_version": 93, "background": { "service_worker": "js/bg/worker-loader.js", "type": "module" },
Type
module
is optional and is supported since Chrome 92. You can import ES modules statically for now. The support for dynamic imports is in development.In any Chrome version you can use
importScripts('/path/foo.js', '/path/bar.js');
to import scripts from other directories.If the worker script throws an error at installation, the worker won't be registered and you will not be getting the error information triggered by your service worker in the console, but only get "Service worker registration failed". This behavior is due to a bug bug in Chrome versions earlier than Chrome 93.
Solution:
- use Chrome 93 or newer.
Limited workaround:
- wrap everything in try/catch as shown in the other answer or use a "loader" script.
Typical causes:
- accessing an undeclared variable
- syntax error like an unclosed parenthesis
- accessing a
chrome
API without declaring it in manifest.json'spermissions
field - a crash in the worker process
这篇关于Service Worker 注册失败.Chrome 扩展程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!