我有一个Chrome扩展,我用一个html文件'index.html'覆盖newtab。
我想在此'index.html'上使用jQuery
。
我该怎么做?在Chrome扩展中重写的新选项卡中使用jQuery违反了内容安全策略?
这里是我的简化代码:
的manifest.json
{
"name": "Test Name",
"description": "Test Description",
"version": "0.1",
"chrome_url_overrides": {
"newtab": "index.html"
},
"manifest_version": 2,
}
的index.html
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="index.js"></script>
</head>
<body>
<div> Hello World ! </div>
</body>
</html>
index.js
console.log('Extension loaded successfully ...');
console.log($('div')); // console.log(jQuery('div'));
但我不断收到在控制台中的以下两个错误。
Refused to load the script ' https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js ' because it violates the following Content Security Policy directive: "script-src 'self' chrome-extension-resource:".
Extension loaded successfully ...
Uncaught ReferenceError: $ is not defined
更新:1我也试图在清单文件中添加content security policy,但它不工作,而且还产生了错误:
"content_security_policy": "script-src 'self' https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js 'unsafe-eval'; object-src 'self'",
更新:2我也尝试在清单文件中添加权限,但它不起作用,仍然是相同的错误:
"permissions": [ "http://*/", "https://*/" ]
我该如何解决这个问题?
尝试本地文件版本的jQuery –
在本地文件的情况下,两个错误消失,但$('div')选择器返回空数组。 –
您必须在清单文档中提供权限,检出权限和内容安全策略 –