簡體   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