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.
10 ////////////////////////////////////////////////////////////////////////////////
11 const {utils
:Cu
, classes
:Cc
, interfaces
:Ci
, results
:Cr
} = Components
;
16 ////////////////////////////////////////////////////////////////////////////////
17 function initAddon (global
, addonName
, contentUrl
, jsUrl
, startupCB
) {
18 if (addonobj
=== null) {
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");
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
);
38 if (addonobj
) try { addonobj
.onShutdown(); } catch (xx
) {}
40 Components
.utils
.reportError(e
.stack
);
41 Components
.utils
.reportError(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 ////////////////////////////////////////////////////////////////////////////////
60 function newWindow (global
, win
) {
62 win
.addEventListener("load", function () {
64 //Components.utils.reportError("NEW WINDOW: "+windowCount);
66 global
.window
.addEventListener("unload", function () {
68 //Components.utils.reportError("DEAD WINDOW: "+windowCount);
69 addonobj
.onWindowUnload(win
);
70 if (windowCount
== 0) {
71 // no more registered windows --> shutdown
72 //Components.utils.reportError("SHUTTING DOWN!");
73 addonobj
.onShutdown();
76 addonobj
.onWindowLoad(win
);
81 ////////////////////////////////////////////////////////////////////////////////
82 let EXPORTED_SYMBOLS
= ["initAddon", "getAddonObj", "newWindow"];