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?

Alexis
  • 23,545
  • 19
  • 104
  • 143
  • 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
  • 49,435
  • 16
  • 136
  • 136
Yashwant Kumar Sahu
  • 3,356
  • 7
  • 27
  • 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
  • 795,719
  • 175
  • 1,089
  • 1,143
  • 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.