[英]How do I require files in Firefox extension (add-on)?
We created extensions for Chrome, Firefox and Safari. 我们为Chrome,Firefox和Safari创建了扩展程序。 Our Firefox extension has a tracker, tracker.js, which is required from the controller by this line: 我们的Firefox扩展包含一个跟踪器tracker.js,此行从控制器中需要它:
tracker = require("../../firefox/tracker.js").tracker;
The tracker works with requiring other files, such as: 跟踪器需要其他文件,例如:
if (typeof exports !== 'undefined') {
common = require("../content/src/common.js").common;
utils = require("../content/src/utils.js").utils;
}
var tracker = new function() {
this.ws_track = function(params) {
params["from_extension"] = true;
params["platform"] = common.sys.platform;
params["version"] = utils.get_version();
if (params["e"] === "install") {
utils.send_get_request(common.config.urls.apis.wstrack, params, function(data) {}, 'json');
}
};
};
if (typeof exports !== 'undefined') {
exports.tracker = tracker;
}
But, when I try to require the controller file from the tracker, I receive the following error: 但是,当我尝试从跟踪器中获取控制器文件时,出现以下错误:
JPM [error] Message: TypeError: tracker is undefined
Here is the tracker code in this case: 在这种情况下,以下是跟踪代码:
if (typeof exports !== 'undefined') {
common = require("../content/src/common.js").common;
controller = require("../content/src/controller.js").controller;
utils = require("../content/src/utils.js").utils;
}
var tracker = new function() {
this.ws_track = function(params) {
params["from_extension"] = true;
params["platform"] = common.sys.platform;
params["version"] = utils.get_version();
var uid = controller.load_param("uid");
if (uid) {
params["uid"] = uid;
}
if (params["e"] === "install") {
utils.send_get_request(common.config.urls.apis.wstrack, params, function(data) {}, 'json');
}
};
};
if (typeof exports !== 'undefined') {
exports.tracker = tracker;
}
But if I don't require the controller, I receive an error that it's undefined. 但是,如果我不需要控制器,则会收到一个未定义的错误消息。 How can I require the controller from the tracker file? 如何从跟踪器文件中请求控制器? I can't unite the files because the controller file is the same for Chrome, Firefox and Safari. 我无法统一文件,因为控制器文件对于Chrome,Firefox和Safari是相同的。 And the trackers are different (Mozilla didn't approve our Chrome tracker). 而且跟踪器是不同的(Mozilla不批准我们的Chrome跟踪器)。
Update: I moved the controller code to the controller, and always called the tracker from the controller, and it solves this problem (the tracker doesn't use the controller). 更新:我将控制器代码移至控制器,并始终从控制器调用跟踪器,从而解决了此问题(跟踪器不使用控制器)。 But if you find a solution to this problem, please let me know. 但是,如果您找到解决此问题的方法,请告诉我。
There is a SDK require funciton, I use it like this - https://developer.mozilla.org/en-US/Add-ons/SDK/Low-Level_APIs/system_child_process#Using_child_process_in_non-jpm_extensions 有一个SDK需要功能,我像这样使用它-https: //developer.mozilla.org/zh-CN/Add-ons/SDK/Low-Level_APIs/system_child_process#Using_child_process_in_non-jpm_extensions
// Import SDK Stuff
const COMMONJS_URI = 'resource://gre/modules/commonjs';
const { require } = Cu.import(COMMONJS_URI + '/toolkit/require.js', {});
var child_process = require('sdk/system/child_process');
// Use it in the same way as in the example above
It seems it works on CommonJS modules check this out - https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/Using#Importing_CommonJS_modules 似乎可以在CommonJS模块上正常运行-将此内容检出-https: //developer.mozilla.org/zh-CN/docs/Mozilla/JavaScript_code_modules/Using#Importing_CommonJS_modules
There is also a require
function in workers that can be used like this: 工作者中还有一个require
函数,可以像这样使用:
importScripts('resource://gre/modules/workers/require.js');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.