5

According to this guide I tried to load JQuery to my Firefox extension.

var Myext = {

  loadJQuery: function(wnd) {
      var loader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"]
        .getService(Components.interfaces.mozIJSSubScriptLoader);
      loader.loadSubScript("chrome://myext/content/jquery-1.7.2.min.js", wnd);
      var jQuery = wnd.jQuery.noConflict(true);
      try {
        loader.loadSubScript("chrome://myext/content/jquery.hoverIntent.js", jQuery);
      catch (Except) {
        alert(Except.toString());
      }
      return jQuery;
  },

  onLoad: function(e) {
    Myext.jQuery = Myext.loadJQuery(window);
  },

  showDialog: function(e) {
    var $ = Myext.jQuery;
    /* JQuery code */
  }

}

window.addEventListener("load", function(e) { Myext.onLoad(e); }, false);
window.addEventListener("DOMContentLoaded", function(e) { Myext.showDialog(e); }, false);

Loader has a problem to load jquery.hoverIntent.js. I downloaded it here

error message: "Type Error: $ is undefined"

xralf
  • 3,312
  • 45
  • 129
  • 200

2 Answers2

1

In order to use .dialog() you need to include jQuery UI library also. Put the next line right after you loaded the jQuery library:

loader.loadSubScript("chrome://myext/content/jquery-ui-1.8.18.custom.min.js", wnd);

The last jQuery UI library you can download from here.

gakhov
  • 1,925
  • 4
  • 27
  • 39
  • I have done it but still can't see the dialog. I copied only the file `jquery-ui-1.8.18.custom.min.js` to `myext/content` directory. – xralf Mar 30 '12 at 14:06
  • It seems that nothing from JQuery works. When I replace dialog function with `$("a").click(function() { alert("Hello world!"); });` I can't see message when clicking on links. – xralf Mar 30 '12 at 14:15
  • Where do you have `
    text
    `? It should be part of the DOM of the html page in the tab. Also you probably need to add smth. like `$("#dialog", gBrowser.contentDocument).dialog()` ... Check also Error Console (`Ctrl`+`Shift`+`J`) and don't forget to restart Firefox after every changes you made.
    – gakhov Mar 30 '12 at 14:22
  • I removed the code with dialog and try only the code from my previous comment. Could I open the dialog without having an element with `#dialog` id? I'd like to open it on event. – xralf Mar 30 '12 at 14:27
  • the code `var loader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"].getService(Components.interfaces.mozIJSSubscriptLoader);` is not executed at all. – xralf Mar 30 '12 at 14:35
  • OK there was an error (sorry for my blind eyes) `mozIJSSubScript` , but there is still something wrong. – xralf Mar 30 '12 at 14:40
  • @xraf Did you include that file in your default XUL file (which you specified in `chrome.manifest`? [docs](https://developer.mozilla.org/en/Chrome_Registration#Example_chrome_manifest) – gakhov Mar 30 '12 at 14:44
  • Yes, functions without jquery work. `jquery` was loaded but there are problems with loading `jquery-ui` and `jquery-hover-intent` now. – xralf Mar 30 '12 at 14:46
  • Why do you have `jQuery` as the `targetObj` parameter when you load `jquery-hover-intent`? – gakhov Mar 30 '12 at 14:58
  • Because it is [here](http://forums.mozillazine.org/viewtopic.php?f=19&t=2105087). I'm beginning with jquery. – xralf Mar 30 '12 at 15:00
  • 1
    For me everything looks OK then, check [this question](http://stackoverflow.com/questions/9906899/loading-jquery-ui-in-ff-11-give-error-typeerror-a-is-undefined). Probably, you have the same problem with jQuery UI – gakhov Mar 30 '12 at 16:20
  • I'm sorry, I had to change the question a little because I have to overcome the error I've stuck. You advised me to download jquery-ui, which was needed too for the code to work. I will probably ask on the dialogs in another question, but I have to first overcome this. Thank you. – xralf Apr 03 '12 at 07:59
0

This line:

onLoad: function(e) {
    Myext.jQuery = Myext.loadJQuery(window);
  },

Shouldn't be?

onLoad: function(e) {
    Myext.jQuery = loadJQuery(window);
  },
JFK
  • 40,963
  • 31
  • 133
  • 306