Chrome 扩展程序“拒绝加载脚本,因为它违反了以下内容安全策略指令”

Imr*_*ony 14 javascript google-chrome google-chrome-extension cryptojs content-security-policy

我尝试通过 cryptojs 库加密用户数据并通过 ajax 发送到服务器,但控制台显示错误:

拒绝加载脚本“ https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/crypto-js.min.js ”,因为它违反了以下内容安全策略指令:“script-src 'self' https://apis.google.com 'unsafe-eval'”。请注意,“script-src-elem”未明确设置,因此“script-src”用作后备。

我的清单代码包含:

"content_security_policy": "script-src 'self' https://apis.google.com 'unsafe-eval'; object-src 'self'"

如何解决这个问题呢?

ko0*_*tik 5

对于那些遇到同样问题的人。我也有同样的问题,在我更新content_security_policy以包含我尝试加载的 googleapis url 后问题得到解决。

我的代码:

<head>
...
<script src="https://maps.googleapis.com/maps/api/js?key=API_KEY;libraries=places"></script>
</head>
Run Code Online (Sandbox Code Playgroud)

需要

{
  "content_security_policy": "script-src 'self' 'unsafe-eval' https://maps.googleapis.com 'unsafe-inline'; object-src 'self'",
}
Run Code Online (Sandbox Code Playgroud)

  • 您使用的清单版本是什么?我认为对于 v3 你不能使用外部代码。 (3认同)