从Greasemonkey调用时,jQuery UI对话框引发错误

每当我尝试从Greasemonkey创建一个对话框时,我都会遇到这个尴尬的错误……我认为它与XPCNativeWrapper https://developer.mozilla.org/en/XPCNativeWrapper#Limitations_of_XPCNativeWrapper的限制有关,尽管我是不是100%肯定。

我使用的核心jQuery方法都没有导致错误(append,css,submit,keydown,each,…)。

这可能是Greasemonkey中的错误,或者是由于Greasemonkey和jquery ui之间的交互,但我真的很想知道如何让它们一起工作。

// ==UserScript== // @name Dialog Test // @namespace http://strd6.com // @description jquery-ui-1.6rc6 Dialog Test // @include * // // @require http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js // @require http://strd6.com/stuff/jqui/jquery-ui-personalized-1.6rc6.min.js // ==/UserScript== $(document).ready(function() { $('
SomeText
').dialog(); });

错误:[例外…“组件不可用”nsresult:“0x80040111(NS_ERROR_NOT_AVAILABLE)”位置:“JS frame :: file:///home/daniel/.mozilla/firefox/…/components/greasemonkey。 js :: anonymous :: line 347“data:no] [中断此错误] if(line){

Firefox版本:Mozilla / 5.0(X11; U; Linux i686; en-US; rv:1.9.0.6)Gecko / 2009020911 Ubuntu / 8.04(hardy)Firefox / 3.0.6

更新:标准jQuery库中的focus()方法也会抛出相同的错误:

 $('body').focus(); 

也许UI在某些时候调用焦点方法?

任何帮助将不胜感激!

这个线程很老,但是使用Greasemonkey和Jquery来聚焦()的方法是将一个[0]添加到jquery对象以将其转回DOM元素。

  //Example: $('#obj').focus(); //Does not work document.getElementById('obj').focus(); //Works //Hybrid: $(#obj)[0].focus(); //Work around 

这是一个解决方法,但还有其他不太引人注目的问题。

 // ==UserScript== // @name Dialog Test // @namespace http://strd6.com // @description jquery-ui-1.6rc6 Dialog Test // @include * // // @resource jQuery http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js // @resource jQueryUI http://strd6.com/stuff/jqui/jquery-ui-personalized-1.6rc6.min.js // ==/UserScript== // Inject jQuery into page... gross hack... for now... (function() { var head = document.getElementsByTagName('head')[0]; var script = document.createElement('script'); script.type = 'text/javascript'; var jQuery = GM_getResourceText('jQuery'); var jQueryUI = GM_getResourceText('jQueryUI'); script.innerHTML = jQuery + jQueryUI; head.appendChild(script); $ = unsafeWindow.$; })(); $(document).ready(function() { $('
SomeText
').dialog(); });

现在的问题源于$在unsafeWindow上下文中,因此某些GM方法无法从不安全的上下文中调用(如$ .each中的GM_getValue)。 必须有一种方法来找到它的根,并让GreQuerymonkey中的jQueryUI工作。 我90%肯定这是一个XPCNativeWrapper问题,因此应该通过更改对话框插件中的一些代码来实现一个简单的解决方法。

不是直接的答案,但是:

如果您没有与Greasemonkey结婚,但想要在Firefox中使用良好的jQuery集成和类似Greasemonkey的function,那么您应该查看Mozilla Ubiquity 。 它有内置的jQuery,对浏览器窗口的良好访问,从任意位置加载内容的相对自由,每页加载执行选项(la Greasemonkey),外部脚本加载器(这就是我的方式) ‘试着加载jQuery UI ..)和其他一些非常酷的东西。 我发现玩起来很容易并在几分钟内运行,而不是搞乱GM / Firefox插件怪异。