Greasemonkey @require jQuery不工作“组件不可用”

我在这里看到了另一个关于在Greasemonkey中加载jQuery的问题 。 尝试过该方法后,在我的==UserScript==标签内使用此require语句:

 // @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代码运行。 我已经确保在安装之前包含@require for jQuery并保存了我的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"); 

我在我的Macbook Pro,Leopard(10.5.8)上的Firefox 3.5.7上安装了Greasemonkey 0.8.20091209.4。 我已经清除了我的缓存(除了cookie)并禁用了除Flashblock 1.5.11.2,Web Developer 1.1.8和Adblock Plus 1.1.3之外的所有其他插件。

安装了我的Greasemonkey脚本的config.xml

   http://www.google.com   

我可以看到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== 

好的,所以我更深入地研究了这个。 我完全使用了你的脚本,但使用了我们的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"); 

对我来说这很好用,我的猜测,google api上的JQuery缺少一些function。 因为上面的代码,工作得很好。 另请注意每个url末尾的/* ,请注明。

尝试另一个JQuery并更改url,它应该正确世界。

我找到了一种非常理想的方法来使用它与jQuery 1.4.1 – 这似乎解决了它。 这是新的浏览器嗅探似乎“打破”它。

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; 

我试图用GM 0.8和jquery 1.4.2来解决这个问题,并且发现了这个问题: http : //forum.jquery.com/topic/importing-jquery-1-4-1-into-greasemonkey-scripts-生成-一个误差

在我看来,这个问题的确切答案以及如何解决这个问题。 解决方法对我有用。

好消息并更新所有post:

上面的补丁允许在Greasemonkey脚本中运行1.5.2之前的jQuery版本,但幸运的是,如果使用当前的jQuery 1.5.2版本,则不再需要补丁。

我检查了它的代码并注意到jQuery中的eventSupported函数代码

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

已更新的结果是未修补的jQuery 1.5.2现在在Greasemonkey 0.9.2中运行。

补丁为jquery-1.4.3.min.js

[old]第41行u.createElement(“div”); s =“on”+ s; var B = s in v;
[new]第41行u.createElement(“div”); s =“on”+ s; var B = true;

@require属性在Greasemonkey和jQuery中无法正常工作……同样的错误也可能在FireBug中发生。

另一种方法是通过创建脚本标记,通过Greasemonkey在页面中包含jQuery。 这是如何做到这一点 。

不完全正确,似乎jQuery 1.4尝试使用在greasemonkey环境中无效的调用来检测某些内容。 @require通常可以正常工作。

所以恢复到1.3.2确实可以解决问题,但我宁愿找到一个让我使用1.4的解决方案。

顺便说一句,我用这个,略有不同:

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

这是Greasemonkey的jQuery 1.4.4的缩小版本:

http://userscripts.org/scripts/show/92329

希望它有所帮助,是的