user979390
user979390

Reputation: 309

Loading external js to "extend" firefox extension

I am working on a firefox extensions that makes uses a lot of external services, I want to have a .js file foreach of these external services hosted on my server and then load each one into the extension when needed.

These external js files are not "normal js" that would execute on a firefox window, they contain code that should be executed on the extension context, for example they need to make use of Components.classes["@mozilla.org/embedcomp/prompt-service;1"].

For example:

var myExtensionName = {
    init: function() {

    },

    service1_func: function() {

    }
}

I want to be able to load service1_Func from and external file and it should work the same as if it were hardcoded into the extension files. The reason I need this is because the service1_Func needs to be updated often and I don't want to update the entire extension each time. I know this could create some security risks but the extension is not for distribution, but it will be used on more than 20 computers so this will be the easiest way for me to maintain it.

How could I achieve this?

English is not my main language, so I hope I explained myself well, please ask comment with questions if I need to clarify something.

Upvotes: 1

Views: 1351

Answers (1)

Wladimir Palant
Wladimir Palant

Reputation: 57691

Warning: This is a security hole, don't do this in an extension that other people are supposed to use!

Use XMLHttpRequest to download the JavaScript file and Function constructor to "compile" it. Something like this:

var request = new XMLHttpRequest();
request.open("GET", "http://example.com/func1.js");
request.addEventListener("load", function()
{
  myExtensionName.service1_func = new Function(request.responseText);
}, false);
request.send();

http://example.com/func1.js should contain the function body (without the surrounding function() {}).

Upvotes: 1

Related Questions