繁体   English   中英

Chrome扩展程序未加载jquery

[英]Chrome extension not loading jquery

我尝试将jQuery导入我的Chrome扩展程序,但是当我尝试时

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>

它说它Refused to load the script 'http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js' because it violates the following Content Security Policy directive: "script-src 'self' chrome-extension-resource:". 这意味着什么,更重要的是,我将如何解决这个问题?

当我通过manifest.json加载jQuery时,对我有用:

{
...
  "background": {
    "scripts": ["jquery.min.js", "background.js"]
  },
...
}

要解决此问题,您必须覆盖默认CSP,或者必须从本地资源导入JS文件。 有时覆盖CSP可能不起作用(不允许HTTP)。 所以我建议你总是从本地导入JS文件。 Chrome扩展程序有一些不使用外部JS的安全准则。

只需将jQuery插件保存到您的本地,并尝试导入您的html页面。 例如,假设您已将jQuery插件保存为本地的“jquery_local_file.js”。

<html>
<head>
    <script src="jquery_local_file.js"></script>
</head>
<body>
    //body code here
</body>
</html>

(要么)

的manifest.json

"content_security_polciy:"srcipt-src 'self' https://ajax.googleapis.com, object-src 'self'";

绝对有效。 :)

安全原因 - 扩展可以比普通的沙盒网页做得更多。 出于这个原因,你可能不得不使用你的扩展包装的jQuery版本,而不是通过互联网加载的版本,尤其是通过HTTP加载的(与HTTPS相比)。

出于安全原因,您无法在Google Chrome扩展程序中引用外部JavaScript文件。 为了解决您的问题,您必须在您的扩展中嵌入jQuery库,然后在background.html页面中引用它。 就像是 :

<html>
  <head>
    <script src='jquery.min.js'></script>
    <!-- ... -->
  </head>
  <body>
    <!-- ... -->
  </body>
</html>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM