Mozilla FireFox插件 - >在main.js中包含外部库 [英] Mozilla FireFox Addons -> Include external library in main.js

查看:203
本文介绍了Mozilla FireFox插件 - >在main.js中包含外部库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的FireFox插件中的main.js中导入/包含一个javascript文件,但由于main.js不包含文档,因此我无法以正常/简单的方式插入它。

I need to import/include a javascript file in my main.js in my FireFox Addon, but as the main.js does not contain a "document" I can not insert it the normal/easy way.

我尝试了一些东西,但从来没有让它发挥作用。

I've tried some stuff but never got it working.

这是我想要完成的事情:
我使用外部时区检测脚本( https://bitbucket.org/pellepim/jstimezonedetect/overview)。我需要确定main.js中的时区以下载Google日历文件+将时间转换为用户时区。这不能在以后完成!到目前为止,我只是将代码手动插入到文件中(复制+粘贴),但这不是一个非常好的清晰的方法。

Here is what I exactly want to accomplish: I use an external timezone detection script (https://bitbucket.org/pellepim/jstimezonedetect/overview). I need to determine the timezone in the main.js to download the Google Calendar File + convert the times to the users timezone. This can not be done later! Until now I just inserted the code manually into the file (copy+paste), but this is not a very nice and clear way of doing this.

推荐答案

您需要创建一个CommonJS模块。将你的.js文件添加到addon的lib文件夹中,并通过exports指令导出你需要的所有函数。完成后,可以通过require指令使用导出的函数。

You would need to create a CommonJS module. Add your .js file inside the addon's lib folder and export all functions that you will be needing via the "exports" directive. Once you do that, you can use the exported functions via the "require" directive.

例如,在你将重复使用的模块中,你可以把:

For example in the module that you will be reusing, you can put:

// REUSABLE MODULE
exports.somefunction = somefunction;

function somefunction() {
    doSomething();
 }

然后在将使用它的模块中:

And then in the module that will be using this:

var othermodule = require("reusable_module");
othermodule.somefunction();

以下是相关文件: https://addons.mozilla.org/en-US/developers/docs/sdk/latest/ dev-guide / guides / modules.html

这篇关于Mozilla FireFox插件 - >在main.js中包含外部库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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