2014-03-12 59 views
0

這是一個Chrome擴展程序,用於替換用戶的新選項卡屏幕。爲什麼不將jQuery加載到我的Chrome擴展中?

我的jQuery沒有加載,因爲我的HTML中的'你好'沒有提醒。我應該在清單或HTML中加載jQuery嗎?無論如何,我認爲HTML應該在同一個地方。

manifest.json的:

{ 
    "manifest_version": 2, 

    "name": "Some title", 
    "description": "Some description.", 
    "version": "1.0", 

    "chrome_url_overrides": { 
    "newtab": "index.html" 
    }, 

"content_scripts": [ 
    { 
     "matches": ["newtab"], 
     "css": ["style.css"], 
     "js": ["jquery.js"] 
    } 
    ] 

} 

的index.html

<html> 
<head> 
    <link rel="stylesheet" type="text/css" href="bootstrap.min.css"> 
    <link rel="stylesheet" type="text/css" href="style.css"> 
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> 
    <script> 
     $(document).ready(function() { 
      alert("hello"); 
     $(function(){ 
      $('#container').masonry({ 
      // options 
      itemSelector : '.item', 
      columnWidth : 300 
     }); 
    }); 
    </script> 
</head> 
<body> 
<!-- etc --> 

回答

1

你不能沒有manifest.json明確指定允許負載從Chrome瀏覽器擴展遠程腳本,您可以在manifest.json指定權限或下載jQuery並用jQuery文件分發你的擴展。第二個選項(在擴展文件夾中有jquery的本地副本)似乎更合理。

+0

我試過第二個選項 - 我通過上面的內容腳本和HTML加載它 - 但CSS不會加載內容腳本,並且jQuery不會加載。 – Sebastian

0

如果index.html是您的後臺頁面,那麼我會將jquery添加到擴展文件夾並相對引用它。

如果它應用於頁面,則指定一個內容腳本,如here所述。再次,保持本地和相對。

+0

根據我上面的代碼,我有一個內容腳本,但它不工作,因爲如果我從我的HTML刪除CSS鏈接標記,沒有CSS加載。 – Sebastian

+0

所以你所指的index.html是一個網絡資源ala http://www.ibm.com?如果是這種情況,那麼無論何時您的URL「匹配」模式正在工作內容腳本加載時,都不需要添加腳本標記。 –

+0

index.html是每次創建新選項卡時顯示的頁面。 – Sebastian

相關問題