【问题标题】:Chrome extension Content Security Policy directive errorChrome 扩展内容安全策略指令错误
【发布时间】:2014-06-06 16:01:56
【问题描述】:

我正在尝试制作无线电流 chrome 扩展,但出现了问题。当我像普通的 JS+HTML+CSS 一样在浏览器中运行我的脚本时,它可以工作,但是当我尝试像 Chrome 扩展程序一样运行它时,我得到了这个错误:

拒绝执行内联脚本,因为它违反了以下内容 内容安全策略指令:“script-src 'self' chrome-extension-resource:"。要么是 'unsafe-inline' 关键字,要么是 哈希 ('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>

【问题讨论】:

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


    【解决方案1】:

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

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

    content security policy

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

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

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

        dataType: 'json'
        

        代替:

        dataType: 'jsonp'
        

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

        【讨论】:

          【解决方案4】:

          您的问题如下:

          1. Chrome CSP forbids inline code,这不受覆盖。您的 'unsafe-eval' 没有解决问题,而 'unsafe-inline' 本来可以帮助 Chrome 的 will be ignored

            你需要去掉内联代码:

            <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 可以帮助您发现这些错误。

          【讨论】:

          • 嗨,tnx 这解决了我一半的问题,现在我可以像扩展程序一样运行它,但我收到了这个错误:Refused to load the script 'http://ws.audioscrobbler.com/2.0/?callback=jQuery17105420794193632901_13982…rtist=David+Craig+ft+Sting&amp;track=Rise+and+Fall&amp;format=json&amp;_=1398287917991' because it violates the following Content Security Policy directive: "script-src 'self' chrome-extension-resource:". 因此我无法获得歌曲名称。跨度>
          • @user3397388 仔细阅读内容安全政策。它解释了您需要如何修改它。
          • 没有帮助.. 和 Xan,一个链接会有帮助
          • @ValentinoPereira 指向什么的链接?答案中链接了有关 CSP 的文档(两个链接都指向同一页面的不同部分),其中包含有关如何处理内联代码和加载外部代码的建议。
          猜你喜欢
          • 2013-02-01
          • 2021-09-24
          • 2013-12-23
          • 1970-01-01
          • 2014-10-26
          • 1970-01-01
          • 2015-10-16
          • 2018-08-14
          • 2020-02-28
          相关资源
          最近更新 更多