在Chrome扩展程序的覆盖的新标签中使用jQuery是否违反了内容安全政策?

Ash*_*hir 5 jquery google-chrome-extension

我有一个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,
}
Run Code Online (Sandbox Code Playgroud)


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>
Run Code Online (Sandbox Code Playgroud)


index.js

console.log('Extension loaded successfully ...');
console.log($('div')); // console.log(jQuery('div'));
Run Code Online (Sandbox Code Playgroud)


但是我一直在控制台中收到以下两个错误。

拒绝加载脚本' https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js ',因为它违反了以下内容安全政策指令:“ script-src'self'chrome -extension-resource:”。

扩展程序加载成功...

未捕获的ReferenceError:未定义$


更新:1我也试图在清单文件中添加内容安全策略,但是它不起作用,并且仍然会产生错误:

"content_security_policy": "script-src 'self' https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js 'unsafe-eval'; object-src 'self'",
Run Code Online (Sandbox Code Playgroud)


更新:2我也试图在清单文件中添加权限,但是它也行不通,仍然是相同的错误:

"permissions": [ "http://*/", "https://*/" ]
Run Code Online (Sandbox Code Playgroud)


我该如何解决?

Xan*_*Xan 5

您为CSP字符串使用了错误的格式。正确的方法是(注意没有路径):

"content_security_policy": "script-src 'self' https://ajax.googleapis.com 'unsafe-eval'; object-src 'self'"
Run Code Online (Sandbox Code Playgroud)

但是,更好的做法是包括在Chrome扩展程序中使用的库的本地副本,而不要依赖CDN。

想象一下,新的选项卡页面由于连接不起作用而完全无法正确加载,或者由于连接不良而导致加载缓慢。