如何通过内容安全策略允许外部脚本在Chrome Extension popup.html中加载



Chrome给了我以下错误:

Refused to load the script 'http://domain.com/myexternalscript.js' because it violates the following Content Security Policy directive: "script-src 'self' 'unsafe-eval'"

目前,在我的清单中,我的内容安全政策如下:

"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'"

如何更改安全策略以允许脚本运行?

在正常页面上,您可以使用

script-src 'self' 'unsafe-eval' domain.com; ...

但是扩展只允许HTTPS上的外部脚本。你需要使用

script-src 'self' 'unsafe-eval' https://domain.com;

并通过HTTPS提供您的脚本。

您必须将要使用的每个外部域列入白名单。您可以使用通配符来匹配任何子域:https://*.domain.com

有关详细信息,请参阅谷歌关于放宽默认CSP的扩展文档。另请参阅MDN的"使用内容安全策略"页面。

最新更新