Loading external js to “extend” firefox extension

北城余情 提交于 2019-12-10 12:16:36

问题


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.


回答1:


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() {}).



来源:https://stackoverflow.com/questions/8915087/loading-external-js-to-extend-firefox-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!