7

I'm working on a Firefox extension and I need to inject a JavaScript into a page from a content script. In my Chrome extension I have done 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 a js file in a firefox extension content script (where I have used chrome.extension.getURL() for Chrome)?

Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126
PeeHaa
  • 71,436
  • 58
  • 190
  • 262

2 Answers2

10

If you're in main.js in your SDK-based add-on, you require and 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 info:

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

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

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

therealjeffg
  • 5,790
  • 1
  • 23
  • 24
2

It looks like that starting in Firefox 38, cfx has been replaced with jpm.

Which might be why this line wasn't working for me:

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

I simply had to rewrite it a bit:

var data = require('sdk/self').data;
customcommander
  • 17,580
  • 5
  • 58
  • 84