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.
11 let EXPORTED_SYMBOLS
= [
14 "getDomWindowForChannel",
15 "getBrowserForDomWindow",
16 "createInputStreamFromString",
24 "imagoCallWindowHook",
30 const {utils
:Cu
, classes
:Cc
, interfaces
:Ci
, results
:Cr
} = Components
;
33 //////////////////////////////////////////////////////////////////////////////
34 const MODULE_PATH
= "chrome://k8-imago-code/content/modules/";
37 //////////////////////////////////////////////////////////////////////////////
41 function setupGBrowser (gb
) {
46 //////////////////////////////////////////////////////////////////////////////
49 // array hooks (value)
52 function registerWindowHook (name
, cb
) {
53 if (typeof(name
) !== "string" || !name
) throw new Error("string hook name expected");
54 if (typeof(cb
) !== "function") throw new Error("function hook callback expected");
55 if (name
!== "load" && name
!== "unload") throw new Error("invalid window hook: '"+name
+"'");
56 let arr
= winhooks
[name
];
57 if (arr
=== undefined) {
61 for (let h
of arr
) if (h
=== cb
) return;
66 function imagoCallWindowHook (name
, win
) {
67 //win.setTimeout(function() conlog("****", Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULAppInfo).name), 3000); // "Pale Moon"
68 if (typeof(name
) !== "string" || !name
) return;
69 let arr
= winhooks
[name
];
70 if (arr
!== undefined) {
75 Component
.utils
.reportError(e
);
82 //////////////////////////////////////////////////////////////////////////////
83 const iosvc
= Cc
["@mozilla.org/network/io-service;1"].getService(Ci
.nsIIOService
);
86 //////////////////////////////////////////////////////////////////////////////
88 function getDomWindowForChannel (chan
) {
91 if (chan
.notificationCallbacks
) {
92 notifyCBs
= chan
.notificationCallbacks
;
93 } else if (chan
.loadGroup
) {
94 notifyCBs
= chan
.loadGroup
.notifyCBs
;
96 return (notifyCBs
? notifyCBs
.getInterface(Ci
.nsIDOMWindow
) : null);
98 //userChrome.logError(e);
104 function getBrowserForDomWindow (dw
) {
105 if (!dw
) return null;
106 if (!gBrowser
) return null;
107 if (typeof(gBrowser
.getBrowserForDocument
) !== "function") return null;
108 return gBrowser
.getBrowserForDocument(dw
.top
.document
);
112 //////////////////////////////////////////////////////////////////////////////
113 function createInputStreamFromString (data
) {
114 if (typeof(data
) !== "string") data
= "";
115 let ost
= Cc
["@mozilla.org/binaryoutputstream;1"].createInstance(Ci
.nsIBinaryOutputStream
);
116 let sst
= Cc
["@mozilla.org/storagestream;1"].createInstance(Ci
.nsIStorageStream
);
117 sst
.init(8192, data
.length
, null); // default allocator
118 ost
.setOutputStream(sst
.getOutputStream(0));
119 ost
.writeBytes(data
, data
.length
);
120 return sst
.newInputStream(0);
124 //////////////////////////////////////////////////////////////////////////////
125 function url2uri (url
) {
126 return iosvc
.newURI(url
, null, null);
130 function cssEscape (s
) s
.replace(/([\]\[(){}"'`#.\/\\])/g, "\\$1"); //"
133 //////////////////////////////////////////////////////////////////////////////
134 // returns timer object
135 function createTimer (aCallback
, aDelay
, oneshot
) {
136 // create the timer object
137 let timer
= Cc
["@mozilla.org/timer;1"].createInstance(Ci
.nsITimer
);
138 // The timer object may be garbage collected before it fires, so we need to
139 // keep a reference to it alive (https://bugzil.la/647998).
140 // However, simply creating a closure over the timer object without using it
141 // may not be enough, as the timer might get optimized out of the closure
142 // scope (https://bugzil.la/640629#c9). To work around this, the timer object
143 // is explicitly stored as a property of the observer.
146 observe: function () {
148 // just-in-case check
149 if (observer
.timer
&& oneshot
) delete observer
.timer
;
154 timer
.init(observer
, aDelay
, (oneshot
? Ci
.nsITimer
.TYPE_ONE_SHOT
: Ci
.nsITimer
.TYPE_REPEATING_SLACK
));
156 get active () !fired
,
157 get callback () aCallback
,
158 get oneShot () !!oneShot
,
159 get interval () aDelay
,
160 cancel: function () {
161 if (observer
.timer
) {
162 observer
.timer
.cancel();
163 delete observer
.timer
;
169 function oneShotTimer (aCallback
, aDelay
) createTimer(aCallback
, aDelay
, true);
170 function intervalTimer (aCallback
, aDelay
) createTimer(aCallback
, aDelay
, false);
173 //////////////////////////////////////////////////////////////////////////////
174 function getRulesIFile () {
175 let pd
= Cc
["@mozilla.org/file/directory_service;1"].getService(Ci
.nsIProperties
).get("ProfD", Ci
.nsIFile
);
176 pd
.append("k8imago");
177 pd
.append("rules.rc");
182 //////////////////////////////////////////////////////////////////////////////
183 function getFileTextContents (file
) {
184 let encoding
= "UTF-8";
185 function toUnicode (text
) {
186 if (typeof(text
) === "string") {
187 if (text
.length
== 0) return "";
188 } else if (text
instanceof ArrayBuffer
) {
189 if (text
.byteLength
== 0) return "";
190 text
= new Uint8Array(text
);
191 return new TextDecoder(encoding
).decode(text
);
192 } else if ((text
instanceof Uint8Array
) || (text
instanceof Int8Array
)) {
193 if (text
.length
== 0) return "";
194 return new TextDecoder(encoding
).decode(text
);
198 let converter
= Cc
["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(Ci
.nsIScriptableUnicodeConverter
);
199 converter
.charset
= encoding
;
200 let res
= converter
.ConvertToUnicode(text
);
201 if (res
.length
>= 3 && res
.substr(0, 3) == "\u00EF\u00BB\u00BF") res
= res
.substr(3); // fuck BOM
205 //debuglog("file=[", file.path, "]");
207 let inputStream
= Cc
["@mozilla.org/network/file-input-stream;1"].createInstance(Ci
.nsIFileInputStream
);
208 inputStream
.init(file
, -1, -1, null);
209 let scInputStream
= Cc
["@mozilla.org/scriptableinputstream;1"].createInstance(Ci
.nsIScriptableInputStream
);
210 scInputStream
.init(inputStream
);
211 let output
= scInputStream
.read(-1);
212 scInputStream
.close();
214 return toUnicode(output
, encoding
);