1
由于某些原因,当我尝试使用我的Chrome扩展程序时,我总是收到以下错误; 拒绝加载脚本'http://dynamic.xkcd.com/api-0/jsonp/comic/1123?callback=random&_=1411616016083',因为它违反了以下内容安全策略指令:“script-src”self'chrome-extension-resource:“。无法对我的Chrome扩展程序执行HTTP请求
这里是我的代码:
的manifest.json
{
"manifest_version": 2,
"name": "XKCD New Tab",
"description": "This extension demonstrates a browser action with kittens.",
"version": "1.0",
"permissions": [
"tabs",
"http://dynamic.xkcd.com/*"
],
"chrome_url_overrides": {
"newtab": "index.html"
}
}
的index.html
<html>
<head>
<title>New Tab</title>
<script src="jquery-2.1.1.min.js"></script>
<script src="testcode.js"></script>
</head>
<style>
h1 {
font-family: Helvetica;
color:#3498db;
font-weight: 100;
}
.container {
text-align: center;
background-color: #ecf0f1;
}
}
</style>
<body bgcolor="#ecf0f1">
<div id="xkcdcontent" class="container"></div>
</body>
</html>
testcode.js
var x = Math.floor((Math.random() * 1420) + 1);
$.ajax({
url: "http://dynamic.xkcd.com/api-0/jsonp/comic/"+ x +"?callback=?",
dataType: "json",
jsonpCallback: "random",
success: function(data) {
$("#xkcdcontent").append(
$("<h1 style=text-align:center;/>").text(data.title),
$("<img align=middle/>").attr({
src: data.img,
title: data.alt,
alt: data.title
})
);
}
});