Chrome 扩展内容安全策略不允许资源

Chrome Extension Content Security Policy not Allowing Resources

我在加载本地 chrome 扩展时收到以下错误:

Refused to load the script 'https://widget.intercom.io/widget/APPID' because it violates the following Content Security Policy directive: "script-src 'self' http://localhost https://widget.intercome.io/ 'unsafe-eval'".

index.html:1 XMLHttpRequest cannot load http://localhost:5000/login. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'chrome-extension://bbmhkchajfbhfjkfiaadlnohbfhbnegj' is therefore not allowed access.

我试图在我的 manifest.json 中添加以下行:

"content_security_policy": "script-src 'self' http://localhost https://widget.intercome.io/ 'unsafe-eval'; object-src 'self'"

最后一行不应该特别允许我访问这两个资源吗?

您对 https://widget.intercome.io CSP 规则的想法是正确的,但您可能需要

  1. 更正您域名的拼写(您的错误有 intercom.io,但您的 CSP 显示 intercome.io

  2. 删除尾部斜杠(我不确定,但我发现 none 的 CSP 示例使用了尾部斜杠)

  3. 进行更改后重新加载您的扩展程序

localhost 错误是由实施同源策略引起的,而不是由 CSP 引起的。 (Ajax 请求由 connect-src CSP 指令管理,不受 Chrome 扩展的默认 CSP 限制。)您需要添加 http://localhost/* 作为 host permission in your manifest's permissions field:

"permissions": [
    "http://localhost/*",
    ...
]

要扩展 ,对于将来来到这里的任何人:

这是正确的,但请确保将其包含在单独的文件中。我 运行 遇到了一个内联问题,所以我刚刚创建了一个名为 intercom.js 的文件,然后在其中包含了脚本,在我的 HTML 中,我将它包含在 <script src="intercom.js" charset="utf-8"></script>

这也是对我有用的:

"content_security_policy": "script-src 'self' http://localhost https://widget.intercom.io https://js.intercomcdn.com 'unsafe-eval'; object-src 'self'"