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
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 ////////////////////////////////////////////////////////////////////////////////
35 function logError () {
36 if (arguments.length) {
38 for (let idx = 0; idx < arguments.length; ++idx) s += ""+arguments[idx];
43 function logException (msg, e) {
45 Cu.reportError(""+msg+": "+e.name+": "+e.message)
47 if (e.stack) Components.utils.reportError(e.stack);
49 Cu.reportError(""+msg);
53 // https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsIConsoleService#logStringMessage() - wstring / wide string
55 const conService = global.Cc["@mozilla.org/consoleservice;1"].getService(global.Ci.nsIConsoleService);
56 global.conlog = function () {
57 if (global.debugOptions.logEnabled && arguments.length) {
59 for (let idx = 0; idx < arguments.length; ++idx) s += ""+arguments[idx];
60 conService.logStringMessage(s);
66 ////////////////////////////////////////////////////////////////////////////////
69 let myUrl = global.__SCRIPT_URI_SPEC__;
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);
78 scp.Services.scriptloader.loadSubScript(uri.spec, global);
80 global.logException("INCLUDE ERROR");
87 ////////////////////////////////////////////////////////////////////////////////
90 let myUrl = global.__SCRIPT_URI_SPEC__;
93 Cu.import("resource://gre/modules/Services.jsm", scp);
96 global.getLocale = function () {
97 if (loc === null) loc = global.Cc["@mozilla.org/chrome/chrome-registry;1"].getService(global.Ci.nsIXULChromeRegistry).getSelectedLocale("global");
101 // list of symbols exported to modules
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") {
108 delete exportList[name];
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
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);
145 return Cu.import(arguments[0], scope);
150 let uri = scp.Services.io.newURI(myUrl+"/../modules/"+name, null, null);
151 scp.Services.scriptloader.loadSubScript(uri.spec, scope);
153 global.logException("REQUIRE ERROR");
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);