9

I would like to include jQuery in a Firefox extension.

I add the following line of code to import the jQuery file:

Components.utils.import("resource://js/jquery.js", window.content.document);

Firefox runs the file immediately after importing. The jQuery file looks like this with an anonymous closures:

 (function( window, undefined ) {
        ...bunch of code....
       _jQuery = window.jQuery,
 })(window);

When the extension runs there is an error "window is not defined". What is a way to give jQuery access to the window?

Klaster_1
  • 11,206
  • 8
  • 62
  • 70
Alexis
  • 21,572
  • 18
  • 95
  • 139
  • 2
    Looks like a duplicate of: http://stackoverflow.com/questions/491490/how-to-use-jquery-in-firefox-extension ? – Mike Ruhlin Nov 09 '10 at 19:03
  • 1
    There they use the – Alexis Nov 09 '10 at 19:10

3 Answers3

4

Write this to your *.xul file to include jQuery.

<script type="application/x-javascript" src="toolbar.js"></script>
Yi Jiang
  • 48,053
  • 16
  • 135
  • 134
Yashwant Kumar Sahu
  • 3,286
  • 7
  • 25
  • 42
1

Not tested, but on normal websites, jQuery is loaded into the context of window. Therefore, you have to use window.content as scope:

Components.utils.import("resource://js/jquery.js", window.content);

window.content.document does not contain a property window.

Felix Kling
  • 756,363
  • 169
  • 1,062
  • 1,111
  • This did not work for me. I even tried this: `var scope = { window: contentWindow };` with no luck. – BrunoLM Apr 03 '12 at 20:19
0

If it's same as for greasemonkey userjs, try using an unsafeWindow, not the window.

Klaster_1
  • 11,206
  • 8
  • 62
  • 70