Index: lib/main.js |
=================================================================== |
new file mode 100644 |
--- /dev/null |
+++ b/lib/main.js |
@@ -0,0 +1,88 @@ |
+/* |
+ * This Source Code is subject to the terms of the Mozilla Public License |
+ * version 2.0 (the "License"). You can obtain a copy of the License at |
+ * http://mozilla.org/MPL/2.0/. |
+ */ |
+ |
+/** |
+ * @module main |
+ */ |
+ |
+Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
+Cu.import("resource://gre/modules/Services.jsm"); |
+Cu.import("resource://gre/modules/Promise.jsm"); |
+ |
+require("commandLine"); |
+let {run} = require("crawler"); |
+let {reportException} = require("debug"); |
+ |
+let baseURL = null; |
+ |
+/** |
+ * Waits for the application to initialize. |
+ * @type {Promise} |
+ */ |
+let applicationReady = (function() |
+{ |
+ return new Promise((resolve, reject) => |
+ { |
+ let observer = { |
+ observe: function(subject, topic, data) |
+ { |
+ Services.obs.removeObserver(this, "sessionstore-windows-restored"); |
+ resolve(); |
+ }, |
+ QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver, Ci.nsISupportsWeakReference]) |
+ }; |
+ |
+ Services.obs.addObserver(observer, "sessionstore-windows-restored", true); |
+ onShutdown.add(() => Services.obs.removeObserver(observer, "sessionstore-windows-restored")); |
+ }); |
+})(); |
+ |
+/** |
+ * Startup function, called from command line handler. |
+ * |
+ * @param {int} port Port to communicate with |
+ */ |
+function startup(port) |
+{ |
+ baseURL = "http://localhost:" + port + "/"; |
+ |
+ let request = new XMLHttpRequest(); |
+ request.open("GET", baseURL + "parameters"); |
+ request.addEventListener("load", onParametersLoaded, false); |
+ request.addEventListener("error", onParametersFailed, false); |
+ request.responseType = "json"; |
+ request.send(); |
+} |
+exports.startup = startup; |
+ |
+/** |
+ * Called if parameters loaded succesfully. |
+ * |
+ * @param {Event} event |
+ */ |
+function onParametersLoaded(event) |
+{ |
+ let {urls, timeout, maxtabs} = event.target.response; |
+ |
+ applicationReady.then(function() |
+ { |
+ let window = Services.wm.getMostRecentWindow("navigator:browser"); |
+ run(window, urls, timeout, maxtabs, baseURL + "save", function() |
+ { |
+ Services.startup.quit(Services.startup.eAttemptQuit); |
+ }); |
+ }, reportException); |
+} |
+ |
+/** |
+ * Called if requesting parameters failed. |
+ * |
+ * @param {Event} event |
+ */ |
+function onParametersFailed(event) |
+{ |
+ reportException(new Error("Failed loading parameters")); |
+} |