内容安全策略getJSON

时间:2015-04-20 20:11:29

标签: jquery google-chrome-extension getjson content-security-policy

对不起我的英语,

我的HTML中有以下代码:

<script type="text/javascript" src="plugin.js"></script>

在我的JS中:

    $(document).ready(function() {
        $.getJSON("https://api.twitch.tv/kraken/streams/"+"NameOfStreamer"+"?callback=?",function(c) {

        if (c.stream == null) {
            $("p").text("Stream offline, n'hésitez pas à me rejoindre sur les réseaux sociaux afficher ci-dessous.");

        } else {

            $("p").text("Stream online, rejoins moi sur Domingo.tv en cliquant sur le lien ci dessous");

        }
    });
});

在我的清单中:

{
  "manifest_version": 2,

  "name": "Getting started example",
  "description": "This extension shows a Google Image search result for the current page",
  "version": "1.0",
  "content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'",

  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  },

  "permissions": [
    "activeTab",
    "https://ajax.googleapis.com/",
  ]
}

我得到了:

Refused to load the script 'https://api.twitch.tv/kraken/streams/NameOfStreamer?callback=jQuery11120590793181443587_1429560015317&_=1429560015318' because it violates the following Content Security Policy directive: "script-src 'self' chrome-extension-resource:".

我想知道如何解决这个问题。

1 个答案:

答案 0 :(得分:1)

您的问题是尝试使用JSONP,这是不需要的。

您希望接收数据,而不是要执行的脚本。如果远程服务器不允许,JSONP是绕过无法发出跨域请求的技巧;但是,Chrome扩展程序有host permissions that bypass cross-domain restrictions

  1. 为您正在使用的API添加跨域权限。

      "permissions": [
        "activeTab",
        "https://ajax.googleapis.com/*",
        "https://api.twitch.tv/*"
      ]
    
  2. 调用API ,不带回调参数:

    $.getJSON("https://api.twitch.tv/kraken/streams/"+"NameOfStreamer", 
      function(c) {
        if (c.stream == null) {
          /*...*/
        }
      }
    );