prateekkathal
prateekkathal

Reputation: 3582

Adding Jquery and Other JS libraries to Firefox Extension

How do I include jquery and other scripts in my firefox extension? I know this topic is a copy of many other questions asked on SO, but none of them were so helpful.

I tried using in one of the JS files where i want to use jquery,

Components.utils.import('chrome://myaddon/content/jquery.min.js');

but it gives me an error saying,

ReferenceError: window is not defined

I also tried adding the script in XUL file, but somehow, I cant get it to work as well.

Is there any other way around it?

Upvotes: 0

Views: 436

Answers (2)

Noitidart
Noitidart

Reputation: 37328

Ok man here's the solution to using Custom Events.

Much thanks to @M.J. Saedy for figuring this one out.

Gist - HERE is a template that will listen to custom events dispatched from non-privileged scope.

Upvotes: 1

Noitidart
Noitidart

Reputation: 37328

You can't import a library to use in your addon. What you gotta do is inject that into the web page you want to use it on.

Is this a bootstrap addon? Did you make a chrome.manifest? Before injecting to websites you have to make the injectable stuff contentaccessible in the manifest.

so an example manifest file:

content myaddon ./ contentaccessible

then your code of Cu.import('chrome://myaddon/content/jquery.min.js') will work

if you want to use this library in an iframe for like a panel. put in that iframe <script src="chrome://myaddon/content/jquery.min.js">

Let me know if you need more help, if you upload it to GitHub I can see what exactly you're doing

Upvotes: 0

Related Questions