How to link to a file in the Firefox extension data directory?

I am working on a Firefox extension and I need to add JavaScript to the page from the contents of the script. In my Chrome extension, I did the following:

this.initializeJplayerSupport = function() { var script = document.createElement('script'); script.setAttribute('type', 'application/javascript'); script.setAttribute('src', chrome.extension.getURL('js/custom-jplayer.js')); document.head.appendChild(script); } 

The file is in my data directory. How can I reference the js file in the contents of the firefox extension script (where I used chrome.extension.getURL() for Chrome)?

+7
source share
2 answers

If you are in main.js in your SDK add-in, you need to use the "data" helper from the "self" object:

 var data = require('self').data; console.log(data.url('somefile.js')); // prints the resource uri to the file. 

For more information:

https://developer.mozilla.org/en-US/Add-ons/SDK/High-Level_APIs/self#data

Once you get this uri resource, you can send it to the content script using self.postMessage or self.port.emit:

https://developer.mozilla.org/en-US/Add-ons/SDK/Guides/Content_Scripts

+10
source

It seems that starting with Firefox 38, cfx been replaced by jpm .

What could be why this line is not working for me:

 var data = require('self').data; 

I just had to rewrite it a bit:

 var data = require('sdk/self').data; 
+2
source

All Articles