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