I want to use jQuery inside a firefox extension, I imported the library in the xul file like this:
<script type="application/x-javascript" src="chrome://myExtension/content/jquery.js"> </script>
but the $() function is not recognized in the xul file neither do the jQuery().
I googled about the problem and found some solutions but no one did work with me: http://gluei.com/blog/view/using-jquery-inside-your-firefox-extension http://forums.mozillazine.org/viewtopic.php?f=19&t=989465
I've also tried to pass the 'content.document' object(which refrences the 'document' object) as the context parameter to the jQuery function like this:
$('img',content.document);
but still not working, does any one came across this problem before?
It may be bad practice, but have you considered including it inline?
The following solution makes it possibile to use jQuery in contentScriptFile (Targetting 1.5 Addon-sdk)
In your main.js:
In your message.js :
Some pitfalls you need to watchs out for:
I use the following
example.xul
:And here is an
example.js
There is an excellent article in the mozillaZine forums that describes this step-by-step: http://forums.mozillazine.org/viewtopic.php?f=19&t=2105087
I haven't tried it yet, though so I hesitate to duplicate the info here.
I think this is what Eric was saying, but you can load Javascript from the URL directly.
Im assuming you want your extension to load JQuery so you can manipulate the page elements easily? My company's labs has something that does this using Javascript directly here: http://parkerfox.co.uk/labs/pixelperfect
Turns out the current top-answer by @sunsean does not work as expected when it comes to handling multiple loads. The function should properly close over the document and avoid global state.
Also, you have to call
jQuery.noConflict(true)
to really avoid conflicts with other add-ons!This is who I would write it (then again, I would avoid jquery (in add-ons) like the plague...). First the overlay XUL
And then the overlay script:
Here is a complete add-on as a gist. Just drop in a copy of jquery and it should be good to go.