cosmetix
[guerillascript.git] / main / addon.js
blob36c1b5b6468f34ad8e0122e0f9e3621b3243c3f9
1 /* coded by Ketmar // Invisible Vector (psyc://ketmar.no-ip.org/~Ketmar)
2 * Understanding is not required. Only obedience.
4 * This program is free software. It comes without any warranty, to
5 * the extent permitted by applicable law. You can redistribute it
6 * and/or modify it under the terms of the Do What The Fuck You Want
7 * To Public License, Version 2, as published by Sam Hocevar. See
8 * http://www.wtfpl.net/txt/copying/ for more details.
9 */
10 ////////////////////////////////////////////////////////////////////////////////
11 const {utils:Cu, classes:Cc, interfaces:Ci, results:Cr} = Components;
13 let addonobj = null;
16 ////////////////////////////////////////////////////////////////////////////////
17 function initAddon (global, addonName, contentUrl, jsUrl, startupCB) {
18 if (addonobj === null) {
19 // check arguments
20 if (typeof(addonName) !== "string" || addonName.length == 0) throw new Error("addon name expected (non-empty string)");
21 if (typeof(contentUrl) !== "string" || contentUrl.length == 0) throw new Error("content url expected (non-empty string)");
22 if (contentUrl[contentUrl.length-1] != "/") contentUrl += "/";
23 if (typeof(jsUrl) === "undefined") jsUrl = contentUrl;
24 if (typeof(jsUrl) !== "string" || jsUrl.length == 0) throw new Error("js url expected (non-empty string)");
25 if (typeof(startupCB) === "undefined") startupCB = null;
26 if (startupCB && typeof(startupCB) !== "function") throw new Error("startup callback expected (function)");
28 //Components.utils.reportError("ADDON '"+addonName+"': initializing");
29 // load "mainjs:init.js"
30 let uri = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService).newURI(jsUrl+"init.js", null, null);
31 let initfn = Cc["@mozilla.org/moz/jssubscript-loader;1"].getService(Ci.mozIJSSubScriptLoader).loadSubScript(uri.spec, this, "UTF-8");
32 try {
33 addonobj = initfn(global, addonName, contentUrl, jsUrl);
34 if (!addonobj) throw new Error("can't init addon '"+addonName+"'"); // alas, something is wrong
35 if (startupCB) startupCB(addonobj);
36 addonobj.onStartup();
37 } catch (e) {
38 if (addonobj) try { addonobj.onShutdown(); } catch (xx) {}
39 addonobj = false;
40 Components.utils.reportError(e.stack);
41 Components.utils.reportError(e);
42 throw e;
45 if (addonobj) return addonobj;
46 throw new Error("addon object wasn't initialized");
50 ////////////////////////////////////////////////////////////////////////////////
51 function getAddonObj () {
52 if (addonobj) return addonobj;
53 throw new Error("addon object wasn't initialized");
57 ////////////////////////////////////////////////////////////////////////////////
58 let EXPORTED_SYMBOLS = ["initAddon", "getAddonObj"];