简体   繁体   English

Chrome扩展程序未加载jquery

[英]Chrome extension not loading jquery

I tried importing jQuery into my Chrome extension, but when I try 我尝试将jQuery导入我的Chrome扩展程序,但是当我尝试时

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

It says that it 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:". 它说它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:". What does that mean and, more importantly, how would I go about that? 这意味着什么,更重要的是,我将如何解决这个问题?

Works for me when I load jQuery through manifest.json : 当我通过manifest.json加载jQuery时,对我有用:

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

To overcome this, you have to override the default CSP or you have to import the JS file from local resources. 要解决此问题,您必须覆盖默认CSP,或者必须从本地资源导入JS文件。 Sometimes overriding the CSP may not work (HTTP is not permitted). 有时覆盖CSP可能不起作用(不允许HTTP)。 So I suggest you to always import the JS files from local. 所以我建议你总是从本地导入JS文件。 Chrome Extensions have some security guidelines for not using external JS. Chrome扩展程序有一些不使用外部JS的安全准则。

Just save the jQuery plugin to your local and try to import in you html page. 只需将jQuery插件保存到您的本地,并尝试导入您的html页面。 For example, imagine you have saved the jQuery plugin as "jquery_local_file.js" in your local. 例如,假设您已将jQuery插件保存为本地的“jquery_local_file.js”。

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

(OR) (要么)

manifest.json 的manifest.json

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

It definitely works. 绝对有效。 :) :)

Security reasons -- extensions can do much more than a normal sandboxed web page. 安全原因 - 扩展可以比普通的沙盒网页做得更多。 For that reason you'll likely have to use a version of jQuery you package with your extension, instead of one to be loaded over the internet, especially one loaded over HTTP (vs HTTPS). 出于这个原因,你可能不得不使用你的扩展包装的jQuery版本,而不是通过互联网加载的版本,尤其是通过HTTP加载的(与HTTPS相比)。

For security reason, you cannot refer to an external JavaScript file within your Google Chrome extension. 出于安全原因,您无法在Google Chrome扩展程序中引用外部JavaScript文件。 In order to overcome your problem you have to embed the jQuery library in your extension and then reference it in your background.html page. 为了解决您的问题,您必须在您的扩展中嵌入jQuery库,然后在background.html页面中引用它。 Something like : 就像是 :

<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