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 ////////////////////////////////////////////////////////////////////////////////
29 function alert (str
) {
30 let ps
= Cc
["@mozilla.org/embedcomp/prompt-service;1"].getService(Ci
.nsIPromptService
);
31 ps
.alert(null, "Guerilla", ""+str
);
33 addExport("alert", alert
);
36 ////////////////////////////////////////////////////////////////////////////////
37 let dirSvcProps
= Cc
["@mozilla.org/file/directory_service;1"].getService(Ci
.nsIProperties
);
40 // get profile directory
41 function getProfileDir () {
42 return dirSvcProps
.get("ProfD", Ci
.nsIFile
);
44 addExport("getProfileDir", getProfileDir
);
47 ////////////////////////////////////////////////////////////////////////////////
48 function hitch (obj
, method
) {
49 if (obj
&& method
&& typeof(method
) == "string") {
50 if (!obj
[method
]) throw new Error("method ["+method
+"] doesn't exist in object ["+obj
+"]");
52 } else if (typeof(method
) == "function") {
55 throw new Error("invalid arguments to hitch()");
57 let stargs
= Array
.prototype.splice
.call(arguments
, 2, arguments
.length
);
59 // make a copy of stargs (don't modify it because it gets reused for every invocation)
60 let args
= Array
.prototype.slice
.call(stargs
);
61 // add all the new arguments
62 Array
.prototype.push
.apply(args
, arguments
);
63 // invoke the original function with the correct this obj and the combined list of static and dynamic arguments
64 return method
.apply(obj
, args
);
67 addExport("hitch", hitch
);
70 ////////////////////////////////////////////////////////////////////////////////
71 // string aString: A string of data to be hashed.
72 // string aAlg: optional; the hash algorithm to be used; possible values are: MD2, MD5, SHA1, SHA256, SHA384, and SHA512; defaults to SHA1
73 // string aCharset: optional; the charset used by the passed string; defaults to UTF-8
74 function cryptoHash (aString
, aAlg
, aCharset
) {
75 const PR_UINT32_MAX
= 0xffffffff; // this tells updateFromStream to read the entire string
78 let alg
= (""+(aAlg
||"SHA1")).trim().toUpperCase();
79 let charset
= (""+(aCharset
||"UTF-8")).trim();
81 let chashObj
= Cc
["@mozilla.org/security/hash;1"].createInstance(Ci
.nsICryptoHash
);
84 chashObj
.initWithString(alg
);
86 logError("invalid hash algorithm: '"+aAlg
+"'");
87 throw new Error("invalid hash algorithm: '"+aAlg
+"'");
90 let uniconvObj
= Cc
["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(Ci
.nsIScriptableUnicodeConverter
);
92 uniconvObj
.charset
= charset
;
94 logError("invalid charset: '"+aCharset
+"'");
95 throw new Error("invalid charset: '"+aCharset
+"'");
98 if (str
) chashObj
.updateFromStream(uniconvObj
.convertToInputStream(str
), PR_UINT32_MAX
);
99 let hash
= chashObj
.finish(false); // hash as raw octets
100 return [("0"+hash
.charCodeAt(i
).toString(16)).slice(-2) for (i
in hash
)].join("");
102 addExport("cryptoHash", cryptoHash
);