now it works again
[guerillascript.git] / includes / basic.js
blob6ff61d67a9e04ba325cebfa5851da96739eb239f
1 /*
2 * Copyright 2015 Ketmar Dark <ketmar@ketmar.no-ip.org>
3 * Portions copyright 2010, Erik Vold
4 * Contributors: See contributors list in install.rdf and CREDITS
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * Note that this license applies only to the Greasemonkey extension source
14 * files, not to the user scripts which it runs. User scripts are licensed
15 * separately by their authors.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
25 * The above copyright notice and this permission notice shall be included in all
26 * copies or substantial portions of the Software.
28 // WARNING! there is no `require` yet!
29 const {utils: Cu, classes: Cc, interfaces: Ci} = Components;
30 Cu.import("resource://gre/modules/Services.jsm", this);
33 ////////////////////////////////////////////////////////////////////////////////
34 // error console log
35 function logError () {
36 if (arguments.length) {
37 let s = "";
38 for (let idx = 0; idx < arguments.length; ++idx) s += ""+arguments[idx];
39 Cu.reportError(s);
43 function logException (msg, e) {
44 if (e) {
45 Cu.reportError(""+msg+": "+e.name+": "+e.message)
46 Cu.reportError(e);
47 if (e.stack) Components.utils.reportError(e.stack);
48 } else {
49 Cu.reportError(""+msg);
53 // https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsIConsoleService#logStringMessage() - wstring / wide string
54 (function (global) {
55 const conService = global.Cc["@mozilla.org/consoleservice;1"].getService(global.Ci.nsIConsoleService);
56 global.conlog = function () {
57 if (global.debugOptions.logEnabled && arguments.length) {
58 let s = "";
59 for (let idx = 0; idx < arguments.length; ++idx) s += ""+arguments[idx];
60 conService.logStringMessage(s);
63 })(this);
66 ////////////////////////////////////////////////////////////////////////////////
67 // include
68 (function (global) {
69 let myUrl = global.__SCRIPT_URI_SPEC__;
70 let scp = {};
71 global.Cu.import("resource://gre/modules/Services.jsm", scp);
73 global.include = function (name) {
74 if (name.indexOf("/") >= 0 || name.length == 0) { global.logError("invalid include name: '"+name+"'"); throw new Error("invalid include name: '"+name+"'"); }
75 if (!(/.\.js$/.test(name))) name += ".js";
76 let uri = scp.Services.io.newURI(myUrl+"/../includes/"+name, null, null);
77 try {
78 scp.Services.scriptloader.loadSubScript(uri.spec, global);
79 } catch (e) {
80 global.logException("INCLUDE ERROR");
81 throw e;
84 })(this);
87 ////////////////////////////////////////////////////////////////////////////////
88 // addExport, require
89 (function (global) {
90 let myUrl = global.__SCRIPT_URI_SPEC__;
91 let scp = {};
92 const Cu = global.Cu;
93 Cu.import("resource://gre/modules/Services.jsm", scp);
95 let loc = null;
96 global.getLocale = function () {
97 if (loc === null) loc = global.Cc["@mozilla.org/chrome/chrome-registry;1"].getService(global.Ci.nsIXULChromeRegistry).getSelectedLocale("global");
98 return loc;
101 // list of symbols exported to modules
102 let exportList = {};
104 function addExport (name, fn) {
105 if (typeof(name) != "string" || name.length == 0) throw new Error("invalid export name");
106 if (typeof(fn) === null || typeof(fn) == "undefined") {
107 // remove export
108 delete exportList[name];
109 } else {
110 exportList[name] = fn;
113 global.addExport = addExport;
115 addExport("Cu", global.Cu);
116 addExport("Cc", global.Cc);
117 addExport("Ci", global.Ci);
118 addExport("Services", global.Services);
120 addExport("logException", global.logException);
121 addExport("logError", global.logError);
122 addExport("conlog", global.conlog);
124 function fillExports (scope) {
125 for (let name in exportList) if (typeof(name) == "string" && name.length > 0) scope[name] = exportList[name];
128 // imports a commonjs style javascript file with loadSubScrpt
129 let modules = {}; // list of loaded modules
131 global.require = function (name) {
132 if (name.indexOf("/") >= 0 || name.length == 0) { global.logError("invalid module name: '"+name+"'"); throw new Error("invalid module name: '"+name+"'"); }
133 if (!(/.\.js$/.test(name))) name += ".js";
134 if (modules[name]) return modules[name];
135 // functions available to modules
136 let scope = {};
137 fillExports(scope);
138 //scope.global = global; // just in case
139 // uses(name[, name]...);
140 scope.uses = function () {
141 if (arguments.length != 1) {
142 for (let idx = 0; idx < arguments.length; ++idx) Cu.import(arguments[idx], scope);
143 return null;
144 } else {
145 return Cu.import(arguments[0], scope);
148 scope.exports = {};
149 try {
150 let uri = scp.Services.io.newURI(myUrl+"/../modules/"+name, null, null);
151 scp.Services.scriptloader.loadSubScript(uri.spec, scope);
152 } catch (e) {
153 global.logException("REQUIRE ERROR");
154 throw e;
156 modules[name] = scope.exports;
157 return scope.exports;
160 addExport("getLocale", global.getLocale);
161 addExport("addExport", global.addExport);
162 addExport("require", global.require);
164 addExport("debugOptions", global.debugOptions);
165 addExport("guerillaOptions", global.guerillaOptions);
166 })(this);