jQuery UI Dialog Throw Errors When Invoked from Greasemonkey

假如想象 提交于 2019-12-03 21:05:51

This thread is pretty old but the way to use Greasemonkey with Jquery to focus() is to add a [0] to the jquery object to turn it back to a DOM element.

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

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

Here is one workaround, but there are still other less dramatic problems involved.

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

The problems having now stem from $ being in the unsafeWindow context, so certain GM methods cannot be called from the unsafe context (like GM_getValue when inside $.each). There's got to be a way to get to the root of this and have jQueryUI work from within Greasemonkey. I'm 90% certain that it's an XPCNativeWrapper issue, so there should be an simple workaround by changing some code in the dialog plugin.

Not a direct answer, but:

If you're not married to Greasemonkey, but want good jQuery integration and Greasemonkey-like functionality in Firefox, you should check out Mozilla Ubiquity. It's got jQuery built-in, good access to the browser window, relative freedom with regards to loading content from arbitrary locations, an on-every-page-load execution option (a la Greasemonkey), an external script loader (this is how I'd go about trying to load jQuery UI..) and a bunch of other really cool stuff. I found it a lot easier to play in and get running within minutes as opposed to messing around with GM / Firefox addon weirdness.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!