OLD | NEW |
1 /* This Source Code Form is subject to the terms of the Mozilla Public | 1 /* This Source Code Form is subject to the terms of the Mozilla Public |
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file, | 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ | 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
4 | 4 |
| 5 let {hook} = require("hooks"); |
5 let functionHooks = new WeakMap(); | 6 let functionHooks = new WeakMap(); |
6 let {hook} = require("hooks"); | 7 |
| 8 exports.removeFromWindow = function(window) |
| 9 { |
| 10 if (functionHooks.has(window)) |
| 11 { |
| 12 let unhook = functionHooks.get(window); |
| 13 unhook(); |
| 14 functionHooks.delete(window); |
| 15 } |
| 16 }; |
| 17 |
7 let {application} = require("info"); | 18 let {application} = require("info"); |
8 switch (application) | 19 switch (application) |
9 { | 20 { |
10 case "firefox": | 21 case "firefox": |
11 { | 22 { |
12 // Firefox | 23 // Firefox |
13 exports.isKnownWindow = function(window) window.document.documentElement.get
Attribute("windowtype") == "navigator:browser"; | 24 exports.isKnownWindow = function(window) window.document.documentElement.get
Attribute("windowtype") == "navigator:browser"; |
14 | 25 |
15 exports.getURLBar = function(window) "gURLBar" in window ? window.gURLBar :
null; | 26 exports.getURLBar = function(window) "gURLBar" in window ? window.gURLBar :
null; |
16 | 27 |
17 exports.getBrowser = function(window) "gBrowser" in window ? window.gBrowser
: null; | 28 exports.getBrowser = function(window) "gBrowser" in window ? window.gBrowser
: null; |
18 | 29 |
19 exports.applyToWindow = function(window, corrector) | 30 exports.applyToWindow = function(window, corrector) |
20 { | 31 { |
21 let urlbar = exports.getURLBar(window); | 32 let urlbar = exports.getURLBar(window); |
22 if (urlbar && urlbar.handleCommand && !functionHooks.has(window)) | 33 if (urlbar && urlbar.handleCommand && !functionHooks.has(window)) |
23 { | 34 { |
24 // Handle new URLs being entered | 35 // Handle new URLs being entered |
25 let unhook = hook(urlbar, "handleCommand", function() { | 36 let unhook = hook(urlbar, "handleCommand", function() |
| 37 { |
26 let correction = corrector(window, urlbar.value); | 38 let correction = corrector(window, urlbar.value); |
27 if (correction) | 39 if (correction) |
28 urlbar.value = correction; | 40 urlbar.value = correction; |
29 }); | 41 }); |
30 functionHooks.set(window, unhook); | 42 functionHooks.set(window, unhook); |
31 } | 43 } |
32 }; | 44 }; |
33 | 45 |
34 exports.removeFromWindow = function(window) | |
35 { | |
36 if (functionHooks.has(window)) | |
37 { | |
38 let unhook = functionHooks.get(window); | |
39 unhook(); | |
40 functionHooks.delete(window); | |
41 } | |
42 }; | |
43 | |
44 exports.openInfobar = function(window, id, message, buttons, persistence) | 46 exports.openInfobar = function(window, id, message, buttons, persistence) |
45 { | 47 { |
46 let browser = exports.getBrowser(window); | 48 let browser = exports.getBrowser(window); |
47 let infobar = browser.getNotificationBox(); | 49 let infobar = browser.getNotificationBox(); |
48 let notif = infobar.getNotificationWithValue(id); | 50 let notif = infobar.getNotificationWithValue(id); |
49 | 51 |
50 if (notif) | 52 if (notif) |
51 { | 53 { |
52 infobar.removeNotification(notif); | 54 infobar.removeNotification(notif); |
53 } | 55 } |
54 | 56 |
55 notif = infobar.appendNotification( | 57 notif = infobar.appendNotification( |
56 message, | 58 message, |
57 id, | 59 id, |
58 require("info").addonRoot + "icon64.png", | 60 require("info").addonRoot + "icon64.png", |
59 infobar.PRIORITY_INFO_HIGH, | 61 infobar.PRIORITY_INFO_HIGH, |
60 buttons | 62 buttons |
61 ); | 63 ); |
62 notif.persistence = persistence; | 64 notif.persistence = persistence; |
63 }; | 65 }; |
64 | 66 |
65 exports.loadURI = function(uri) | 67 exports.loadURI = function(window, uri) |
66 { | 68 { |
67 exports.getBrowser(Services.wm.getMostRecentWindow("navigator:browser")).l
oadURI(uri); | 69 exports.getBrowser(window).loadURI(uri); |
68 }; | 70 }; |
69 | 71 |
70 break; | 72 break; |
71 } | 73 } |
72 case "seamonkey": | 74 case "seamonkey": |
73 { | 75 { |
74 let eventListeners = new WeakMap(); | 76 let eventListeners = new WeakMap(); |
75 | 77 |
76 // SeaMonkey | 78 // SeaMonkey |
77 exports.isKnownWindow = function(window) window.document.documentElement.get
Attribute("windowtype") == "navigator:browser"; | 79 exports.isKnownWindow = function(window) window.document.documentElement.get
Attribute("windowtype") == "navigator:browser"; |
78 | 80 |
79 exports.getURLBar = function(window) "gURLBar" in window ? window.gURLBar :
null; | 81 exports.getURLBar = function(window) "gURLBar" in window ? window.gURLBar :
null; |
80 | 82 |
81 exports.getBrowser = function(window) "gBrowser" in window ? window.gBrowser
: null; | 83 exports.getBrowser = function(window) "gBrowser" in window ? window.gBrowser
: null; |
82 | 84 |
83 exports.applyToWindow = function(window, corrector) | 85 exports.applyToWindow = function(window, corrector) |
84 { | 86 { |
85 let urlbar = exports.getURLBar(window); | 87 let urlbar = exports.getURLBar(window); |
86 let goButton = window.document.getElementById("go-button-container"); | 88 let goButton = window.document.getElementById("go-button-container"); |
87 | 89 |
88 if (urlbar && urlbar._fireEvent && !functionHooks.has(window)) | 90 if (urlbar && urlbar._fireEvent && !functionHooks.has(window)) |
89 { | 91 { |
90 function correctURL() | 92 function correctURL() |
91 { | 93 { |
92 let correction = corrector(window, urlbar.value); | 94 let correction = corrector(window, urlbar.value); |
93 if (correction) | 95 if (correction) |
94 urlbar.value = correction; | 96 urlbar.value = correction; |
95 } | 97 } |
96 | 98 |
97 let unhook = hook(urlbar, "_fireEvent", function(eventType) { | 99 let unhook = hook(urlbar, "_fireEvent", function(eventType) |
| 100 { |
98 if (eventType == "textentered") | 101 if (eventType == "textentered") |
99 { | 102 { |
100 correctURL(); | 103 correctURL(); |
101 } | 104 } |
102 }); | 105 }); |
103 functionHooks.set(window, unhook); | 106 functionHooks.set(window, unhook); |
104 | 107 |
105 if (goButton) | 108 if (goButton) |
106 { | 109 { |
107 goButton.addEventListener("command", correctURL, true); | 110 goButton.addEventListener("command", correctURL, true); |
108 eventListeners.set(window, { | 111 eventListeners.set(window, { |
109 "listener": correctURL, | 112 "listener": correctURL, |
110 "element": goButton | 113 "element": goButton |
111 }); | 114 }); |
112 } | 115 } |
113 } | 116 } |
114 }; | 117 }; |
115 | 118 |
| 119 let basicRemove = exports.removeFromWindow; |
116 exports.removeFromWindow = function(window) | 120 exports.removeFromWindow = function(window) |
117 { | 121 { |
118 if (functionHooks.has(window)) | 122 basicRemove(window); |
119 { | 123 |
120 let unhook = functionHooks.get(window); | |
121 unhook(); | |
122 functionHooks.delete(window); | |
123 } | |
124 | |
125 if (eventListeners.has(window)) | 124 if (eventListeners.has(window)) |
126 { | 125 { |
127 let eventListener = eventListeners.get(window); | 126 let eventListener = eventListeners.get(window); |
128 eventListener.element.removeEventListener("command", eventListener.liste
ner, true); | 127 eventListener.element.removeEventListener("command", eventListener.liste
ner, true); |
129 } | 128 } |
130 }; | 129 }; |
131 | 130 |
132 exports.openInfobar = function(window, id, message, buttons, persistence) | 131 exports.openInfobar = function(window, id, message, buttons, persistence) |
133 { | 132 { |
134 let browser = exports.getBrowser(window); | 133 let browser = exports.getBrowser(window); |
135 let infobar = browser.getNotificationBox(); | 134 let infobar = browser.getNotificationBox(); |
136 let notif = infobar.getNotificationWithValue(id); | 135 let notif = infobar.getNotificationWithValue(id); |
137 | 136 |
138 if (notif) | 137 if (notif) |
139 { | 138 { |
140 infobar.removeNotification(notif); | 139 infobar.removeNotification(notif); |
141 } | 140 } |
142 | 141 |
143 notif = infobar.appendNotification( | 142 notif = infobar.appendNotification( |
144 message, | 143 message, |
145 id, | 144 id, |
146 require("info").addonRoot + "icon64.png", | 145 require("info").addonRoot + "icon64.png", |
147 infobar.PRIORITY_INFO_HIGH, | 146 infobar.PRIORITY_INFO_HIGH, |
148 buttons | 147 buttons |
149 ); | 148 ); |
150 notif.persistence = persistence; | 149 notif.persistence = persistence; |
151 }; | 150 }; |
152 | 151 |
153 exports.loadURI = function(uri) | 152 exports.loadURI = function(window, uri) |
154 { | 153 { |
155 exports.getBrowser(Services.wm.getMostRecentWindow("navigator:browser")).l
oadURI(uri); | 154 exports.getBrowser(window).loadURI(uri); |
156 }; | 155 }; |
157 | 156 |
158 break; | 157 break; |
159 } | 158 } |
160 case "fennec": | 159 case "fennec": |
161 { | 160 { |
162 // XUL Fennec | 161 // XUL Fennec |
163 exports.isKnownWindow = function(window) window.document.documentElement.get
Attribute("windowtype") == "navigator:browser"; | 162 exports.isKnownWindow = function(window) window.document.documentElement.get
Attribute("windowtype") == "navigator:browser"; |
164 | 163 |
165 exports.getURLBar = function(window) null; | 164 exports.getURLBar = function(window) null; |
166 | 165 |
167 exports.getBrowser = function(window) null; | 166 exports.getBrowser = function(window) null; |
168 | 167 |
169 exports.applyToWindow = function(window, corrector) | 168 exports.applyToWindow = function(window, corrector) |
170 { | 169 { |
171 if ("BrowserUI" in window && window.BrowserUI.goToURI && !("urlfixerOldHan
dler" in window.BrowserUI.goToURI)) | 170 if ("BrowserUI" in window && window.BrowserUI.goToURI && !functionHooks.ha
s(window)) |
172 { | 171 { |
173 // Handle new URLs being entered | 172 // Handle new URLs being entered |
174 let oldHandler = window.BrowserUI.goToURI; | 173 let unhook = hook(window.BrowserUI, "goToURI", function(url) |
175 window.BrowserUI.goToURI = function(url) | |
176 { | 174 { |
177 url = url || this._edit.value; | 175 url = url || this._edit.value; |
178 try | 176 |
179 { | 177 let correction = corrector(window, url); |
180 let correction = corrector(window, url); | 178 if (correction) |
181 if (correction) | 179 url = correction; |
182 url = correction; | 180 |
183 } | 181 return [url]; |
184 catch(e) | 182 }); |
185 { | 183 functionHooks.set(window, unhook); |
186 Cu.reportError(e); | |
187 } | |
188 oldHandler.call(this, url); | |
189 } | |
190 window.BrowserUI.goToURI.urlfixerOldHandler = oldHandler; | |
191 window.BrowserUI.goToURI.toString = doNotRecompile; | |
192 } | 184 } |
193 }; | 185 }; |
194 | 186 |
195 exports.removeFromWindow = function(window) | 187 exports.openInfobar = function(window, id, message, buttons, persistence) |
196 { | 188 { |
197 if ("BrowserUI" in window && window.BrowserUI.goToURI && "urlfixerOldHandl
er" in window.BrowserUI.goToURI) | 189 if ("getNotificationBox" in window) |
198 window.BrowserUI.goToURI = window.BrowserUI.goToURI.urlfixerOldHandler; | 190 { |
199 }; | 191 let infobar = window.getNotificationBox(); |
| 192 let notification = infobar.getNotificationWithValue(id); |
200 | 193 |
201 exports.openInfobar = function() | 194 if (notification) |
202 { | 195 { |
203 // TODO: Implement infobar | 196 infobar.removeNotification(notification); |
| 197 } |
| 198 |
| 199 notification = infobar.appendNotification( |
| 200 message, |
| 201 id, |
| 202 require("info").addonRoot + "icon64.png", |
| 203 infobar.PRIORITY_INFO_HIGH, |
| 204 buttons |
| 205 ); |
| 206 notification.persistence = persistence; |
| 207 } |
204 }; | 208 }; |
205 | 209 |
206 exports.loadURI = function(window, uri) | 210 exports.loadURI = function(window, uri) |
207 { | 211 { |
208 // TODO: Implement infobar | 212 if ("BrowserUI" in window && "goToURI" in window.BrowserUI) |
| 213 { |
| 214 window.BrowserUI.goToURI(uri); |
| 215 } |
209 }; | 216 }; |
210 | 217 |
211 break; | 218 break; |
212 } | 219 } |
213 case "fennec2": | 220 case "fennec2": |
214 { | 221 { |
215 // Native Fennec | 222 // Native Fennec |
216 exports.isKnownWindow = function(window) window.document.documentElement.get
Attribute("windowtype") == "navigator:browser"; | 223 exports.isKnownWindow = function(window) window.document.documentElement.get
Attribute("windowtype") == "navigator:browser"; |
217 | 224 |
218 exports.getURLBar = function(window) null; | 225 exports.getURLBar = function(window) null; |
219 | 226 |
220 exports.getBrowser = function(window) null; | 227 exports.getBrowser = function(window) null; |
221 | 228 |
222 exports.applyToWindow = function(window, corrector) | 229 exports.applyToWindow = function(window, corrector) |
223 { | 230 { |
224 if ("BrowserApp" in window && window.BrowserApp.observe && !("urlfixerOldH
andler" in window.BrowserApp.observe)) | 231 if ("BrowserApp" in window && window.BrowserApp.observe && !functionHooks.
has(window)) |
225 { | 232 { |
226 let oldHandler = window.BrowserApp.observe; | 233 let innerUnhook = null; |
227 let oldFunc = null; | 234 function cleanup() |
228 let handler = function() | |
229 { | 235 { |
230 let params = Array.prototype.slice.apply(arguments); | 236 if (innerUnhook) |
231 try | 237 innerUnhook(); |
232 { | |
233 let correction = corrector(window, params[0]); | |
234 if (correction) | |
235 params[0] = correction; | |
236 } | |
237 catch(e) | |
238 { | |
239 Cu.reportError(e); | |
240 } | |
241 return oldFunc.apply(this, params); | |
242 }; | |
243 | 238 |
244 window.BrowserApp.observe = function(subject, topic, data) | 239 innerUnhook = null; |
| 240 } |
| 241 |
| 242 let unhook = hook(window.BrowserApp, "observe", function(subject, topic,
data) |
245 { | 243 { |
246 // Huge hack: we replace addTab/loadURI when the observer is | 244 // Huge hack: we replace addTab/loadURI when the observer is |
247 // triggered. This seems to be the only way to know that the calls | 245 // triggered. This seems to be the only way to know that the calls |
248 // originate from user input. | 246 // originate from user input. |
249 let method = null; | 247 let method = null; |
250 if (topic == "Tab:Add") | 248 if (topic == "Tab:Add") |
251 method = "addTab"; | 249 method = "addTab"; |
252 else if (topic == "Tab:Load") | 250 else if (topic == "Tab:Load") |
253 method = "loadURI"; | 251 method = "loadURI"; |
254 | 252 |
255 if (method) | 253 if (method) |
256 { | 254 { |
257 oldFunc = this[method]; | 255 innerUnhook = hook(this, method, function() |
258 this[method] = handler; | 256 { |
| 257 let params = Array.prototype.slice.apply(arguments); |
| 258 let correction = corrector(window, params[0]); |
| 259 if (correction) |
| 260 params[0] = correction; |
| 261 return params; |
| 262 }); |
259 } | 263 } |
260 | 264 }, cleanup); |
261 try | 265 functionHooks.set(window, unhook); |
262 { | |
263 oldHandler.apply(this, arguments); | |
264 } | |
265 finally | |
266 { | |
267 if (method) | |
268 this[method] = oldFunc; | |
269 } | |
270 }; | |
271 window.BrowserApp.observe.urlfixerOldHandler = oldHandler; | |
272 window.BrowserApp.observe.toString = doNotRecompile; | |
273 } | 266 } |
274 }; | 267 }; |
275 | 268 |
276 exports.removeFromWindow = function(window) | |
277 { | |
278 if ("BrowserApp" in window && window.BrowserApp.observe && "urlfixerOldHan
dler" in window.BrowserApp.observe) | |
279 window.BrowserApp.observe = window.BrowserApp.observe.urlfixerOldHandler
; | |
280 }; | |
281 | |
282 exports.openInfobar = function(window, id, message, buttons, persistence) | 269 exports.openInfobar = function(window, id, message, buttons, persistence) |
283 { | 270 { |
284 if ("BrowserApp" in window && "selectedTab" in window.BrowserApp) | 271 if ("BrowserApp" in window && "selectedTab" in window.BrowserApp) |
285 { | 272 { |
286 window.NativeWindow.doorhanger.show(message, id, buttons, window.Browser
App.selectedTab.id, | 273 window.NativeWindow.doorhanger.show(message, id, buttons, window.Browser
App.selectedTab.id, |
287 { | 274 { |
288 persistence: persistence | 275 persistence: persistence |
289 } | 276 } |
290 ); | 277 ); |
291 } | 278 } |
292 }; | 279 }; |
293 | 280 |
294 exports.loadURI = function(uri) | 281 exports.loadURI = function(window, uri) |
295 { | 282 { |
296 let window = Services.wm.getMostRecentWindow("navigator:browser"); | |
297 if ("BrowserApp" in window && "loadURI" in window.BrowserApp) | 283 if ("BrowserApp" in window && "loadURI" in window.BrowserApp) |
298 window.BrowserApp.loadURI(uri); | 284 window.BrowserApp.loadURI(uri); |
299 }; | 285 }; |
300 | 286 |
301 break; | 287 break; |
302 } | 288 } |
303 default: | 289 default: |
304 { | 290 { |
305 exports.isKnownWindow = function(window) false; | 291 exports.isKnownWindow = function(window) false; |
306 break; | 292 break; |
307 } | 293 } |
308 } | 294 } |
OLD | NEW |