Chrome扩展程序内容安全策略不允许使用资源

时间:2015-04-30 12:51:26

标签: google-chrome google-chrome-extension

加载本地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'"

最后一行是否应该允许我访问这两个资源?

2 个答案:

答案 0 :(得分:4)

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

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

  2. 删除尾部斜杠(我不确定,但我发现没有一个CSP示例使用尾部斜杠)

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

  4. localhost错误是由执行同源政策而非CSP引起的。 (受connect-src CSP指令约束的Ajax请求不受Chrome扩展程序&默认CSP的限制。)您需要将http://localhost/*添加为host permission in your manifest's permissions field

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

答案 1 :(得分:0)

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

这是正确的,但请确保将其包含在单独的文件中。我遇到了一个内联问题,所以我刚刚创建了一个名为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'"