将 Chrome 扩展程序从清单 v1 更改为 v2 时出现端口错误 [英] Port error while changing chrome extension from manifest v1 to v2

查看:14
本文介绍了将 Chrome 扩展程序从清单 v1 更改为 v2 时出现端口错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试将扩展程序从清单版本 1 移植到版本 2 时,出现以下情况:

While attempting to port the extension from manifest version 1 to version 2, this appeared:

端口错误:无法建立连接.接收端不存在.chromeHidden.Port.dispatchOnDisconnectmiscellaneous_bindings:232

Port error: Could not establish connection. Receiving end does not exist. chromeHidden.Port.dispatchOnDisconnect miscellaneous_bindings:232

这出现在开发者工具的控制台中.我不知道从哪里开始解决这个问题,因为我不知道是什么导致它开始......

This appeared in Console in developer tools. I have no idea where to start fixing this cause i don't know what's causing it to begin with..

什么会导致这个问题?有没有办法确切地知道是什么原因造成的?谢谢.

what can cause this problem? and is there someway to know exactly what's causing it? Thanks.

推荐答案

最可能的失败原因是 默认内容安全策略"manifest_version": 2 处于活动状态.默认 CSP 的结果是 内联 JavaScript 将不会执行.

The most likely cause of failure is the activation of the default Content security policy when "manifest_version": 2 is active. A consequence of the default CSP is that inline JavaScript will not be executed.

<script>chrome.extension.onConnect.addListener(...);</script>

上一行是内联代码的示例.解决办法是将脚本放在一个外部JS文件中:

The previous line is an example of inline code. The solution is to place the script in an external JS file:

<script src="script.js"><!--original contents moved to script.js--></script>

背景页面/脚本

当您使用背景页面时,不要 使用:

  • "background_page": "background.htm", 或
  • "background": {"page": "background.htm"},
    但是
  • "background": {"scripts": ["background.js"]}
    其中 background.js 包含最初放置在
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆