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

时间:2014-04-22 10:25:56

标签: javascript html google-chrome google-chrome-extension content-security-policy

我试图制作无线电流镀铬扩展但是存在问题。当我在浏览器中运行我的脚本时,就像普通的JS + HTML + CSS一样,但是当我尝试像Chrome扩展程序那样运行时,我会收到此错误:

  

拒绝执行内联脚本,因为它违反了以下内容   内容安全政策指令:" script-src' self'   铬扩展资源:&#34 ;. “不安全 - 内联”和“不安全”。关键字,a   哈希(' 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'"

}

这是主要的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>

4 个答案:

答案 0 :(得分:18)

您的问题如下:

  1. Chrome CSP forbids inline code,这不受覆盖。您的'unsafe-eval'无法解决问题,而'unsafe-inline'则会帮助will be ignored Chrome。

    您需要摆脱内联代码:

    <script>$(function(){$("#radioplayere").plate({playlist: [{file:"http://RADIO_STATION_STREAM_URL/;"}], phpGetter: "http://hostingshoutcast.com/stream/plate/php/plate.php"});});</script>
    

    这需要在js文件中移动。

  2. 你的manifest.json中有一个拼写错误,你忘记了一个逗号:

    "version": "1.0",
    

    通常,使用JSON validator可以帮助您发现这些错误。

答案 1 :(得分:1)

我知道我有点迟到了,但根据OP对Xan回答的评论,解决问题的另一个组成部分是调整隐含的AJAX调用。

我收到了同样的错误并将我的API调整调整为:

dataType: 'json'

而不是:

dataType: 'jsonp'

(解决了问题,获得批准后,仍然需要删除任何内联脚本。)

答案 2 :(得分:1)

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

答案 3 :(得分:-1)

在manifest.json文件中添加CSP可以消除该错误。它适用于我的应用程序。 我想缺少','是代码的问题,否则请添加以下行,然后尝试。

jit

content security policy