我有一个 chrome 扩展,我在其中使用 html 文件“index.html”覆盖 newtab。
我想使用 jQuery
在此'index.html'上。
我该怎么做?
这是我的简化代码:
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在 list 文件中,但它不起作用,并且仍然生成错误:
"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我也尝试在 list 文件中添加权限,但它也不起作用,仍然出现相同的错误:
"permissions": [ "http://*/", "https://*/" ]
我该如何解决这个问题?
最佳答案
您使用的 CSP 字符串格式错误。正确的应该是(注意没有路径):
"content_security_policy": "script-src 'self' https://ajax.googleapis.com 'unsafe-eval'; object-src 'self'"
不过,更好的做法是包含您在 Chrome 扩展程序中使用的库的本地副本,而不是依赖 CDN。
想象一下,由于连接不起作用,您的新标签页完全无法正确加载,或者由于连接不良而加载缓慢。
关于jquery - 在 Chrome 扩展中重写的新选项卡中使用 jQuery 违反了内容安全策略?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29822021/