2010-11-09 120 views
9

我想在Firefox擴展中包含jQuery。Firefox擴展中的jQuery

我添加以下代碼行來導入jQuery的文件:

Components.utils.import("resource://js/jquery.js", window.content.document); 

火狐立即運行該文件導入後。 jQuery的文件看起來像這樣一個匿名閉包:

(function(window, undefined) { 
     ...bunch of code.... 
     _jQuery = window.jQuery, 
})(window); 

當分機運行存在錯誤「沒有定義窗口」。什麼是讓jQuery訪問窗口的方法?

+2

看起來像一個副本:http://stackoverflow.com/questions/491490/how-to-use-jquery-in-firefox-extension? – 2010-11-09 19:03:11

+1

他們使用

0

如果它與greasemonkey userjs相同,請嘗試使用unsafeWindow而不是window

1

未經測試,但在正常的網站上,jQuery被加載到window的上下文中。因此,你必須使用window.content的範圍:

Components.utils.import("resource://js/jquery.js", window.content); 

window.content.document不包含財產window

+0

這不適合我。我甚至試過這個:'var scope = {window:contentWindow};'沒有運氣。 – BrunoLM 2012-04-03 20:19:46