2014-09-03 126 views
0

我正在試着學習如何製作擴展名,而且我開始非常基本。我不斷收到一個錯誤「未捕獲的ReferenceError:$沒有定義」如何在Chrome擴展中使用JQuery?

這是我的manifest.json:

{ 
    "manifest_version" : 2, 
    "name": "My Extension", 
    "version": "1", 
    "description": "Testing", 
    "content_scripts": [ 
     { 
      "matches": ["http://www.google.com/*"], 
      "js": ["jquery.min.js"] 
     } 
    ], 

    "background": { 
     "scripts": ["run.js"] 
    } 
} 

jQuery的文件被正確命名和位於擴展文件夾內。這是'run.js'腳本:

$(document).ready(function() { 
    alert("document loaded"); 
}) 

我該如何解決這個問題,以便我可以正確使用JQuery?提前致謝。

回答

1

使用content_scripts/matches屬性,您將限制您的jQuery只在當前URL匹配http://www.google.com/*時才加載。更改以匹配所有域:

{ 
    "manifest_version" : 2, 
    "name": "My Extension", 
    "version": "1", 
    "description": "Testing", 
    "content_scripts": [ 
     { 
      "matches": ["http://*"], 
      "js": ["jquery.min.js"] 
     } 
    ], 

    "background": { 
     "scripts": ["run.js"] 
    } 
} 

docs

Required. Specifies which pages this content script will be injected into. See Match Patterns for more details on the syntax of these strings and Match patterns and globs for information on how to exclude URLs.

OR,你可以把它添加到您現有的後臺腳本:

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

啊,它添加到後臺腳本工作。謝謝! – David 2014-09-03 07:39:25

+0

@David你可能需要這個方案,試試'http:// *'或者我的第二個建議 – CodingIntrigue 2014-09-03 07:40:17