加载外部的js来“扩展” Firefox扩展 [英] Loading external js to "extend" firefox extension

查看:157
本文介绍了加载外部的js来“扩展” Firefox扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用大量外部服务的Firefox扩展,我希望在服务器上托管这些外部服务的.js文件,然后在需要时将每个扩展加载到扩展中。



这些外部js文件不是在firefox窗口中执行的普通js,它们包含应该在扩展上下文中执行的代码,例如他们需要使用 Components.classes [@ mozilla.org/embedcomp/prompt-service;1]



例如:

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $' b
$ service1_func:function(){


$ b code $ pre

我希望能够从外部文件加载service1_Func,它应该像硬编码到扩展文件一样工作。我需要这个的原因是因为service1_Func需要经常更新,我不想每次都更新整个扩展。我知道这可能会造成一些安全风险,但扩展名不是用于发布,但是它将被用在20多台电脑上,所以这将是我维护它的最简单的方法。



我怎么能做到这一点? b
$ b

英语不是我的主要语言,所以我希望我解释得很好,如果我需要澄清一些问题,请提问评论。 :这是一个安全漏洞,不要在其他人应该使用的扩展中这样做!



使用 XMLHttpRequest 下载JavaScript文件和 Function 构造函数来编译它。就像这样:
$ b

  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 应该包含函数体(没有周围的函数(){} )。


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.

解决方案

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

这篇关于加载外部的js来“扩展” Firefox扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆