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

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

问题描述

尝试将扩展从manifest 1移植到版本2时,显示为:


端口错误:无法建立连接。接收结束不存在
。 chromeHidden.Port.dispatchOnDisconnect
miscellaneous_bindings:232


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



什么会导致这个问题?还有什么方法可以确切知道是什么原因造成的?
谢谢。

解决方案

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

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

上一行是内联代码的示例。解决方案是将脚本放置在外部JS文件中:

 < script src =script.js><< ;! - 将原始内容移至script.js  - >< / script> 



后台页面/脚本



正在使用后台页面,请勿 使用:




  • background_page:background.htm

  • background:{page:background.htm}

    但是
  • background:{scripts:[background.js]}

    其中 background.js 包含最初放在​​< script> 标签内< background.htm 。
    b
    $ b

    内嵌事件侦听器



    浏览器动作弹出窗口,应用程序启动器,选项页面等通常包含内联事件侦听器。



    < button onclick =test();> 不起作用。解决方案是使用 外部JS文件中添加事件。 rel =nofollow noreferrer> addEventListener 。查看文档这个答案

    其他




    • 从字符串创建JavaScript( eval 函数 setTimeout ,...)是禁止的。重写代码以不从字符串创建代码,或者使用沙箱清单选项 (在Chrome 21中引入)。 自Chrome 22以来 unsafe-eval CSP政策可以使用以取消此限制。 JSONP 不起作用,因为外部(JavaScript)资源无法在扩展的上下文中加载。使用普通的 XMLHttpRequest 代替JSONP(更多信息+示例)。

      唯一的异常是通过 https 不是http 。可以调整CSP以引入此例外情况 - 查看文档

       content_security_policy:script-src'self'https://example.com; object-src'self',




    官方文件



    官方文档还提供了有关该主题的精彩解释,请参阅教程:迁移到Manifest V2


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

    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.

    解决方案

    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>
    

    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 pages/scripts

    When you were using background pages, do not use:

    • "background_page": "background.htm", or
    • "background": {"page": "background.htm"},
      but
    • "background": {"scripts": ["background.js"]}
      where background.js contains the script which was initially placed within the <script> tags at background.htm.

    Inline event listeners

    Browser action popups, app launchers, option pages, etc. often contain inline event listeners. By the CSP, these are also forbidden.

    <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.

    Other

    • 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, the unsafe-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 over httpsnot http. The CSP can be adjusted to introduce this exception - see documentation:

      "content_security_policy": "script-src 'self' https://example.com; object-src 'self'",
      

    Official documentation

    The official documentation also provides an excellent explanation on the topic, see "Tutorial: Migrate to Manifest V2".

    这篇关于端口错误,同时将chrome扩展程序从清单v1更改为v2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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