2010-01-16 33 views
10

我見過the other question on here關於在Greasemonkey中加載jQuery。已經嘗試過的方法,用這種需要聲明我==UserScript==標籤內:Greasemonkey @require jQuery不工作「組件不可用」

// @require http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js 

我仍然可以在Firefox的錯誤控制檯以下錯誤信息:

Error: Component is not available 
Source File: file:///Users/greg/Library/Application%20Support/ 
     Firefox/Profiles/xo9xhovo.default/gm_scripts/myscript/jquerymin.js 
Line: 36 

這會停止我的Greasemonkey代碼運行。我確定我包含了jQuery的@require並在安裝之前保存了我的js文件,因爲所需文件只在安裝時加載。

代碼:

// ==UserScript== 
// @name   My Script 
// @namespace  http://www.google.com 
// @description My test script 
// @include  http://www.google.com 
// @require  http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js 
// ==/UserScript== 

GM_log("Hello"); 

我的Greasemonkey 0.8.20091209.4安裝在Firefox 3.5.7上我的MacBook Pro,豹(10.5.8)。我清除了緩存(Cookie除外),並禁用了Flashblock 1.5.11.2,Web Developer 1.1.8和Adblock Plus 1.1.3以外的所有其他插件。

我​​3210與我的Greasemonkey腳本安裝:

<UserScriptConfig> 
<Script filename="myscript.user.js" name="My Script" 
namespace="http://www.google.com" description="My test script" enabled="true" 
basedir="myscript"> 
    <Include>http://www.google.com</Include> 
    <Require filename="jquerymin.js"/> 
</Script> 

我可以看到jquerymin.js坐在gm_scripts/myscript/目錄。

此外,在安裝Greasemonkey腳本時,在控制檯中發生此錯誤常見嗎?

Error: not well-formed 
Source File: file:///Users/Greg/Documents/myscript.user.js 
Line: 1, Column: 1 
Source Code: 
    // ==UserScript== 
+0

我試過你的腳本,它工作得很好。這裏沒問題,使用Greasemonkey版本0.8.20091129.3。你確定在安裝新腳本之前你已經正確地卸載了以前的腳本。我也在Firefox中緩存時遇到過麻煩。 – Anders 2010-01-16 15:14:47

+0

這似乎是jquery 1.4的一個bug,幾天前剛剛發佈。至少,我有與jquery 1.4相同的問題,但不是與jquery 1.3。 – ShreevatsaR 2010-01-16 17:24:20

回答

6

好吧,所以我更深入地研究了這一點。我正是用你的腳本,但用我們的jQuery版本,使它看起來像這樣:

// ==UserScript== 
// @name   My Script 
// @namespace  http://www.google.com 
// @description My test script 
// @include  http://www.google.se/* 
// @include  http://www.dn.se/* 
// @require  http://myserver/jquery-1.3.2.js 
// ==/UserScript== 

GM_log("Hello"); 

這對我的作品就好了,我的猜測,JQuery的向上的谷歌API缺少一些功能。因爲上面的代碼,工作得很好。還請注意每個網址末尾的/*,請包括這一點。

嘗試另一個JQuery,並更改網址,它應該世界正確。

+0

謝謝,我將我的需求URL更改爲http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js,它的工作原理! – 2010-01-16 15:53:38

0

@require屬性在Greasemonkey和jQuery中無法正常工作......在FireBug中也會出現同樣的錯誤。

另一種方法是通過創建腳本標記通過Greasemonkey在頁面中包含jQuery。 Here's how to do that

+0

謝謝,我已經看到了這個鏈接 - 我想知道爲什麼@require是一個記錄的方法,但不適合我。 http://www.keyvan.net/2008/10/greasemonkey-jquery/ – 2010-01-16 15:13:22

+0

我們正在使用的Greasemonkey用的jQuery的@require屬性和它的作品就好了我們。 – Anders 2010-01-16 15:16:41

0

不完全如此,它似乎像jQuery 1.4試圖檢測一些使用只是在greasemonkey環境中不起作用的調用。 @require通常會正常工作。

所以恢復到1.3.2確實可行,但我寧願找到一個讓我使用1.4的解決方案。

順便說一句,我用這個,略有不同:

// @require http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js 
7

我發現用jQuery 1.4.1使用非理想的方式 - 這似乎解決它。這似乎是「打破」它的new browser sniffing

jQuery的1.4.1.min.js:

[old] 36: var o=r.createElement("div");n="on"+n;var m=n in o; 
    [new] 36: var o=r.createElement("div");n="on"+n;var m=true; 

jQuery的1.4.1.js

[old] 934: var isSupported = (eventName in el); 
    [new] 934: var isSupported = true; 
6

我周圍試圖處理這一問題與通用汽車0.8和jQuery絆腳石1.4.2,發現這個:http://forum.jquery.com/topic/importing-jquery-1-4-1-into-greasemonkey-scripts-generates-an-error

它看起來對我來說,明確的答案的問題,以及如何解決它。解決方法爲我工作。

+1

感謝您的鏈接,未來這是一個明確的答案(ichau也提到了相同的修復,但沒有鏈接)。 當時我問這個問題,1.4只是剛剛出來,我正在使用1.3.2 Anders答案解決我的問題。 – 2010-03-22 10:29:26

+0

感謝您的鏈接,我編輯我的鏈接以防萬一。當時沒有鏈接,並從我的腳本中複製了一些評論:) – 2010-12-02 15:27:51

2

修補的jquery-1.4.3.min.js

[舊]線路41 u.createElement( 「DIV」); S 「上的」 + S =; VAR B = S IN v;
[新]線路41 u.createElement( 「DIV」); S 「上的」 + S =; VAR B =真;

3

好消息,並更新所有的供求信息:

以上修補程序允許1.5.2之前版本的jQuery在Greasemonkey腳本中運行,但幸運的是,如果您使用curren,則不再需要該修補程序t jQuery 1.5.2版本。

我檢查它的代碼,並注意到,在jQuery的

var eventSupported = function(eventName) { ... } 

的eventSupported功能代碼已經與後果更新的未打補丁的jQuery 1.5.2現在的Greasemonkey 0.9.2運行。