将 Chrome 扩展程序从清单 v1 更改为 v2 时出现端口错误 [英] Port error while changing chrome extension from manifest v1 to 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
包含最初放置在标签内的脚本,位于
background.htm
.
"background_page": "background.htm"
, or"background": {"page": "background.htm"}
,
but"background": {"scripts": ["background.js"]}
wherebackground.js
contains the script which was initially placed within the<script>
tags atbackground.htm
.
浏览器操作弹出窗口、应用启动器、选项页面等通常包含内联事件侦听器.CSP 也禁止这些.
Browser action popups, app launchers, option pages, etc. often contain inline event listeners. By the CSP, these are also forbidden.
不起作用.解决方案是使用外部JS文件中添加事件rel="nofollow noreferrer">
addEventListener
.查看文档或这个答案 举个例子.
<button onclick="test();">
does not work. The solution is to add the event in an external JS file using addEventListener
. Have a look at the documentation or this answer for an example.
- 禁止从字符串(
eval
、Function
、setTimeout
、...)创建 JavaScript.重写您的代码以不从字符串创建代码,或使用 沙盒清单选项(在 Chrome 21 中引入).自 Chrome 22 起,unsafe-eval
CSP 政策 可用于解除此限制. JSONP 不起作用,因为无法在扩展程序的上下文中加载外部 (JavaScript) 资源.使用普通的
XMLHttpRequest
而不是 JSONP (更多信息 +示例).
唯一的例外是通过https
而非http获取资源.可以调整 CSP 以引入此异常 - 参见文档:
- JavaScript creation from strings (
eval
,Function
,setTimeout
, ...) is forbidden. Rewrite your code to not create code from strings, or use the sandbox manifest option (introduced in Chrome 21). Since Chrome 22, theunsafe-eval
CSP policy can be used to lift this restriction. JSONP does not work, because external (JavaScript) resources cannot be loaded in the extension's context. Use an ordinary
XMLHttpRequest
instead of JSONP (more information + example).
The only exception is when the resource is fetched overhttps
not http. The CSP can be adjusted to introduce this exception - see documentation:
"content_security_policy": "script-src 'self' https://example.com; object-src 'self'",
官方文档也提供了关于该主题的出色解释,请参阅"教程:迁移到清单V2".
The official documentation also provides an excellent explanation on the topic, see "Tutorial: Migrate to Manifest V2".
这篇关于将 Chrome 扩展程序从清单 v1 更改为 v2 时出现端口错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!