2

每当我尝试从 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() {
 $('<div title="Test">SomeText</div>').dialog();
});

错误:[异常...“组件不可用”nsresult:“0x80040111(NS_ERROR_NOT_AVAILABLE)”位置:“JS 框架 :: file:///home/daniel/.mozilla/firefox/.../components/greasemonkey. js :: 匿名 :: 第 347 行”数据:否] [打破此错误] 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 在某个时候调用了 focus 方法?

任何帮助将不胜感激!

4

3 回答 3

2

这个线程已经很老了,但是将 Greasemonkey 与 Jquery 一起使用到 focus() 的方法是将 [0] 添加到 jquery 对象以将其转回 DOM 元素。

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

      //Hybrid:
      $(#obj)[0].focus();                         //Work around
于 2010-11-06T19:23:46.627 回答
1

这是一种解决方法,但还涉及其他不那么引人注目的问题。

// ==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();
});

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

于 2009-02-20T04:23:03.090 回答
1

不是直接回答,而是:

如果您没有与 Greasemonkey 结婚,但想要在 Firefox 中实现良好的 jQuery 集成和类似 Greasemonkey 的功能,您应该查看Mozilla Ubiquity。它内置了 jQuery,可以很好地访问浏览器窗口,从任意位置加载内容的相对自由度,每页加载执行选项(a la Greasemonkey),外部脚本加载器(这就是我会去尝试加载 jQuery UI..) 和一堆其他非常酷的东西。我发现在几分钟内玩游戏和运行起来比搞乱 GM / Firefox 插件的怪异要容易得多。

于 2009-04-27T03:24:18.783 回答