使用parcel和web-ext构建webextension,正确的内容安全策略是什么

问题描述

Parcel 支持捆绑网络扩展:https://v2.parceljs.org/recipes/web-extension/

设置构建服务器并观察更改的命令是:parcel src/manifest.json --host localhost --target webext-dev。这会将网络扩展捆绑到 dist/ 文件夹中。

我想使用 web-ext run 运行此扩展程序。这是 Mozilla 的一个工具,它会自动将 webextension 加载到 Firefox 中并在更改时重新加载。在 dist/ 文件夹中运行它应该是很好的包裹。

但实际上捆绑的 webextension 并没有执行我的内容脚本。 这是因为parcel 是从localhost 为它们提供服务的。它需要标志 --host localhost 来进行热模块更换。 但是 webextension 然后抱怨 "page's settings blocked loading of a resource at ws://localhost:1234/ (“connect-src”)."

parcel 文档还演示了一个构建命令:parcel build src/manifest.json --target webext-prod。如果我从 dist/webext-prod 执行 web-ext run,它就像一个魅力。新运行的parcel build 命令会自动触发重新加载webextension。但这对开发来说是无效的,因为包裹构建花费的时间太长。我想使用它的开发设置。

如何正确配置我的 webextension 的内容安全策略以允许从本地主机加载的内容脚本?

到目前为止,我的 manifest.json 中的条目如下所示:

  "content_security_policy": "connect-src 'self' 'localhost:';",

解决方法

  1. 'localhost:' 的语法错误,不应使用单引号和尾随冒号“:”。它只是特定的主机名,如域名。

  2. localhost 来源涵盖 http://localhosthttps://localhost(仅限 http/https 方案),但仅限 with standard ports

  3. 'self' 令牌仅涵盖 CSP3 浏览器中的 ws://localhostwss://localhost 主机源,并且仅包含标准端口。

由于您确实使用了非标准端口号,请尝试:

"content_security_policy": "connect-src 'self' ws://localhost:1234;"

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...