Javascript Chrome扩展内容安全策略指令错误

Javascript Chrome扩展内容安全策略指令错误,javascript,html,google-chrome,google-chrome-extension,content-security-policy,Javascript,Html,Google Chrome,Google Chrome Extension,Content Security Policy,我正在尝试制作广播流chrome扩展,但有一个问题。当我像普通的JS+HTML+CSS那样在浏览器中运行脚本时,它会工作,但当我像Chrome extension那样运行脚本时,我会出现以下错误: 拒绝执行内联脚本,因为它违反以下条件 内容安全策略指令:“脚本src'self” chrome扩展资源:“。“不安全内联”关键字或 需要哈希('sha256-…')或nonce('nonce-…')才能启用 内联执行 之后,我将此添加到我的清单中: "content_security_policy":

我正在尝试制作广播流chrome扩展,但有一个问题。当我像普通的JS+HTML+CSS那样在浏览器中运行脚本时,它会工作,但当我像Chrome extension那样运行脚本时,我会出现以下错误:

拒绝执行内联脚本,因为它违反以下条件 内容安全策略指令:“脚本src'self” chrome扩展资源:“。“不安全内联”关键字或 需要哈希('sha256-…')或nonce('nonce-…')才能启用 内联执行

之后,我将此添加到我的清单中:

"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'"
{
   "background": {
      "scripts": [ "jquery.js", "jquery-ui.js", "plate.js" ]
        },

   "browser_action": {
      "default_icon": "Images/action-normal.png",
      "default_popup": "player.html",
      "default_title": ""
   },
   "description": "Chrome Player",
   "manifest_version": 2,
   "name": "Radio Chrome Player",
   "permissions": [ "http://www.radio-station.com/" ],
   "version": "1.0"

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

}
但在此之后,我收到了错误消息(清单行中的错误与上面的代码一致)


这是我的舱单:

"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'"
{
   "background": {
      "scripts": [ "jquery.js", "jquery-ui.js", "plate.js" ]
        },

   "browser_action": {
      "default_icon": "Images/action-normal.png",
      "default_popup": "player.html",
      "default_title": ""
   },
   "description": "Chrome Player",
   "manifest_version": 2,
   "name": "Radio Chrome Player",
   "permissions": [ "http://www.radio-station.com/" ],
   "version": "1.0"

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

}
这是主html文件:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="jquery.js"></script>
<script src="jquery-ui.js"></script>
<script src="main.js"></script>
<script>$(function(){$("#radioplayere").plate({playlist: [{file:"http://RADIO_STATION_STREAM_URL/;"}], phpGetter: "http://hostingshoutcast.com/stream/plate/php/plate.php"});});</script>
</head>
<body>
<div id="radioplayer">If you are seeing this then an error has occurred!</div>
</body>
</html>

$(function(){$(“#radioplayere”).plate({播放列表:[{文件:http://RADIO_STATION_STREAM_URL/;“}],phpGetter:http://hostingshoutcast.com/stream/plate/php/plate.php"});});
如果您看到这一点,则发生了错误!

您的问题如下:

  • Chrome CSP,这不受覆盖。你的
    'safe-eval'
    没有解决这个问题,而
    'safe-inline'
    浏览器本可以帮助你解决这个问题

    您需要去掉内联代码:

    <script>$(function(){$("#radioplayere").plate({playlist: [{file:"http://RADIO_STATION_STREAM_URL/;"}], phpGetter: "http://hostingshoutcast.com/stream/plate/php/plate.php"});});</script>
    
    通常,使用a可以帮助您捕获这些错误


  • 我知道我做这件事有点晚了,但根据OP对Xan答案的评论,解决这个问题的另一个组件是调整隐含的AJAX调用

    我收到了相同的错误,并将API调用调整为:

    dataType: 'json'
    
    而不是:

    dataType: 'jsonp'
    

    (解决了这个问题,当然,仍然需要删除任何内联脚本。)

    对我来说,原因是我使用的jQuery的旧版本(如v1.7)存在CSP问题,请选择新版本(v2.1.3)。

    在manifest.json文件中添加CSP会删除错误。它适用于我的react应用程序。 我想您的代码可能缺少“,”,否则请添加下面的行,然后重试

    "csp": "script-src 'self' 'unsafe-inline'; object-src 'self'"
    

    嗨,tnx这解决了我一半的问题,现在我可以像扩展一样运行它,但是我遇到了这样的错误:
    拒绝加载脚本的http://ws.audioscrobbler.com/2.0/?callback=jQuery17105420794193632901_13982…rtist=David+Craig+ft+Sting&track=Rise+and+Fall&format=json&"因为它违反了以下内容安全策略指令:“script src'self'chrome extension resource:”。
    因此我无法获取歌曲名称。@user3397388请仔细阅读内容安全策略。它解释了您需要如何修改它。没有帮助..和Xan,需要一个链接helpful@ValentinoPereira答案中链接了有关CSP的什么?文档(两个链接都指向同一页面的不同部分),其中包含关于如何处理内联代码和加载外部代码的建议。