服务人员注册失败.Chrome扩展程序 [英] Service worker registration failed. Chrome extension

查看:178
本文介绍了服务人员注册失败.Chrome扩展程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不了解如何在Service Worker中从清单v2迁移到v3.发生错误服务工作者注册失败

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);

推荐答案

  • 服务工作者文件必须在扩展名的根路径中,其中 manifest.json 所在.

    这是Service Worker规范的限制,请参见 https://crbug.com/1136582 并单击那里的星形图标.希望它会针对扩展程序进行修复.

    This is a limitation of Service Worker specification, see https://crbug.com/1136582 and click the star icon there. Hopefully it'll be fixed for extensions.

    同时,正确的manifest.json应该如下所示:

    Meanwhile, the correct manifest.json should look like this:

    "background": {
      "service_worker": "background.js"
    },
    

    使用 importScripts('/path/foo.js','/path/bar.js'); 从其他目录导入脚本.

    Use importScripts('/path/foo.js', '/path/bar.js'); to import scripts from other directories.

    如果工作程序脚本在启动过程中引发错误,则该工作程序将完全不会注册,并且由于

    If the worker script throws an error during start, the worker won't be registered at all and there'll be no error displayed anywhere due to a bug in Chrome.

    将其他内容包装在try/catch中,如其他答案所示,或使用加载程序"脚本.

    Wrap everything in try/catch as shown in the other answer or use a "loader" script.

    典型原因:

    • 访问未声明的变量
    • 语法错误,如未括上的括号
    • 访问 chrome API而不在manifest.json的 permissions 字段中声明它
    • accessing an undeclared variable
    • syntax error like an unclosed parenthesis
    • accessing a chrome API without declaring it in manifest.json's permissions field

    这篇关于服务人员注册失败.Chrome扩展程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆