Javascript 从Greasemonkey调用jQuery UI对话框时抛出错误

Javascript 从Greasemonkey调用jQuery UI对话框时抛出错误,javascript,jquery,greasemonkey,Javascript,Jquery,Greasemonkey,每当我尝试从中创建对话框时,都会遇到这个尴尬的错误 油嘴滑舌。。。我相信这是因为 XPCNativeWrapper ,虽然我不是100%肯定 我使用的所有核心jQuery方法都没有导致错误 (附加、css、提交、向下键、每个…) 这可能是由于Greasemonkey中的错误或 Greasemonkey和jquery ui之间的交互,但我确实 对如何让他们一起工作感兴趣 // ==UserScript== // @name Dialog Test // @namespace

每当我尝试从中创建对话框时,都会遇到这个尴尬的错误 油嘴滑舌。。。我相信这是因为 XPCNativeWrapper ,虽然我不是100%肯定

我使用的所有核心jQuery方法都没有导致错误 (附加、css、提交、向下键、每个…)

这可能是由于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() {
 $('<div title="Test">SomeText</div>').dialog();
});
也许UI在某个时候调用了focus方法


任何帮助都将不胜感激

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

// ==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() {
  $('<div title="Test">SomeText</div>').dialog();
});
/==UserScript==
//@name对话测试
//@名称空间http://strd6.com
//@description jquery-ui-1.6rc6对话测试
//@包括*
//
//@resourcejqueryhttp://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js
//@resource jQueryUIhttp://strd6.com/stuff/jqui/jquery-ui-personalized-1.6rc6.min.js
//==/UserScript==
//将jQuery插入页面。。。粗俗的黑客。。。现在。。。
(功能(){
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(脚本);
$=不安全窗口。$;
})();
$(文档).ready(函数(){
$('SomeText').dialog();
});

现在出现的问题源于$在unsafeWindow上下文中,因此某些GM方法不能从不安全上下文中调用(比如在$.each内时的GM_getValue)。一定有办法找到问题的根源,让jQueryUI在Greasemonkey内部工作。我90%确定这是XPCNativeWrapper问题,因此应该通过更改对话框插件中的一些代码来解决这个问题。

不是直接的答案,而是:


如果你还没有嫁给Greasemonkey,但想在Firefox中实现良好的jQuery集成和类似Greasemonkey的功能,你应该去看看。它内置了jQuery,可以很好地访问浏览器窗口,相对自由地从任意位置加载内容,每一页都有一个加载执行选项(la Greasemonkey),一个外部脚本加载器(这就是我尝试加载jQuery UI的方式…)和其他一些非常酷的东西。我发现在几分钟内玩游戏并开始运行要比在GM/Firefox加载项上胡闹容易得多。

这个线程已经很老了,但是在Jquery to focus()中使用Greasemonkey的方法是在Jquery对象中添加一个[0],将其转换回DOM元素

      //Example:  
      $('#obj').focus();                          //Does not work
      document.getElementById('obj').focus();     //Works

      //Hybrid:
      $(#obj)[0].focus();                         //Work around

>>某些GM方法无法从不安全上下文中调用
      //Example:  
      $('#obj').focus();                          //Does not work
      document.getElementById('obj').focus();     //Works

      //Hybrid:
      $(#obj)[0].focus();                         //Work around