We created extensions for Chrome, Firefox and Safari. Our Firefox extension has a tracker, tracker.js, which is required from the controller by this line:
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. And the trackers are different (Mozilla didn't approve our Chrome tracker).
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
// 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
There is also a require
function in workers that can be used like this:
importScripts('resource://gre/modules/workers/require.js');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.