Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: lib/typoAppIntegration.js

Issue 8559070: Integrated URL Fixer into Adblock Plus (Closed)
Patch Set: Second batch of changes Created Nov. 9, 2012, 2:33 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(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 {application, addonName} = require("info");
7
8 let functionHooks = new WeakMap();
9
10 exports.removeFromWindow = function(window)
11 {
12 if (functionHooks.has(window))
13 {
14 let unhook = functionHooks.get(window);
15 unhook();
16 functionHooks.delete(window);
17 }
18 };
19
20 switch (addonName)
21 {
22 case "url-fixer":
23 {
24 // URL Fixer
25 exports.isTypoCorrectionEnabled = function(window, prefix, domain, suffix) t rue;
26
27 break;
28 }
29 case "adblockplus":
30 {
31 // Adblock Plus
32 exports.isTypoCorrectionEnabled = function(window, prefix, domain, suffix)
33 {
34 let {Prefs} = require("prefs");
35
36 if (!Prefs.correctTyposAsked && !Prefs.correctTypos)
37 {
38 let {Utils} = require("utils");
39 let message = Utils.getString("typo_optin_message").replace(/\?1\?/, dom ain);
40 let yes = Utils.getString("typo_optin_yes");
41 let no = Utils.getString("typo_optin_no");
42 let buttons = [
43 {
44 label: yes,
45 accessKey: null,
46 callback: function()
47 {
48 // Yes: Enable typo correction
49 Prefs.correctTypos = true;
50 exports.loadURI(window, prefix + domain + suffix);
51 Prefs.correctTyposAsked = true;
52 }
53 },
54 {
55 label: no,
56 accessKey: null,
57 callback: function()
58 {
59 // No: Do nothing
60 Prefs.correctTyposAsked = true;
61 }
62 }
63 ];
64 // We need to have persistence being set to 1 due to redirect which happ ens afterwards
65 exports.openInfobar(window, "adblockplus-infobar-correct-typos-ask", mes sage, buttons, 1);
66 }
67
68 return Prefs.correctTypos;
69 };
70
71 break;
72 }
73 }
74
75 switch (application)
76 {
77 case "firefox":
78 {
79 // Firefox
80 exports.isKnownWindow = function(window) window.document.documentElement.get Attribute("windowtype") == "navigator:browser";
81
82 exports.getURLBar = function(window) "gURLBar" in window ? window.gURLBar : null;
83
84 exports.getBrowser = function(window) "gBrowser" in window ? window.gBrowser : null;
85
86 exports.applyToWindow = function(window, corrector)
87 {
88 let urlbar = exports.getURLBar(window);
89 if (urlbar && urlbar.handleCommand && !functionHooks.has(window))
90 {
91 // Handle new URLs being entered
92 let unhook = hook(urlbar, "handleCommand", function()
93 {
94 let correction = corrector(window, urlbar.value);
95 if (correction)
96 urlbar.value = correction;
97 });
98 functionHooks.set(window, unhook);
99 }
100 };
101
102 exports.openInfobar = function(window, id, message, buttons, persistence)
103 {
104 let browser = exports.getBrowser(window);
105 let infobar = browser.getNotificationBox();
106 let notification = infobar.getNotificationWithValue(id);
107
108 if (notification)
109 {
110 infobar.removeNotification(notification);
111 }
112 notification = infobar.appendNotification(
113 message,
114 id,
115 "chrome://" + addonName + "/skin/icon16.png",
116 infobar.PRIORITY_INFO_HIGH,
117 buttons
118 );
119 notification.persistence = persistence;
120 };
121
122 exports.loadURI = function(window, uri)
123 {
124 exports.getBrowser(window).loadURI(uri);
125 };
126
127 break;
128 }
129 case "seamonkey":
130 {
131 let eventListeners = new WeakMap();
132
133 // SeaMonkey
134 exports.isKnownWindow = function(window) window.document.documentElement.get Attribute("windowtype") == "navigator:browser";
135
136 exports.getURLBar = function(window) "gURLBar" in window ? window.gURLBar : null;
137
138 exports.getBrowser = function(window) "gBrowser" in window ? window.gBrowser : null;
139
140 exports.applyToWindow = function(window, corrector)
141 {
142 let urlbar = exports.getURLBar(window);
143 let goButton = window.document.getElementById("go-button-container");
144
145 if (urlbar && urlbar._fireEvent && !functionHooks.has(window))
146 {
147 function correctURL()
148 {
149 let correction = corrector(window, urlbar.value);
150 if (correction)
151 urlbar.value = correction;
152 }
153
154 let unhook = hook(urlbar, "_fireEvent", function(eventType)
155 {
156 if (eventType == "textentered")
157 {
158 correctURL();
159 }
160 });
161 functionHooks.set(window, unhook);
162
163 if (goButton)
164 {
165 goButton.addEventListener("command", correctURL, true);
166 eventListeners.set(window, {
167 "listener": correctURL,
168 "element": goButton
169 });
170 }
171 }
172 };
173
174 let basicRemove = exports.removeFromWindow;
175 exports.removeFromWindow = function(window)
176 {
177 basicRemove(window);
178
179 if (eventListeners.has(window))
180 {
181 let eventListener = eventListeners.get(window);
182 eventListener.element.removeEventListener("command", eventListener.liste ner, true);
183 eventListeners.delete(window);
184 }
185 };
186
187 exports.openInfobar = function(window, id, message, buttons, persistence)
188 {
189 let browser = exports.getBrowser(window);
190 let infobar = browser.getNotificationBox();
191 let notification = infobar.getNotificationWithValue(id);
192
193 if (notification)
194 {
195 infobar.removeNotification(notification);
196 }
197
198 notification = infobar.appendNotification(
199 message,
200 id,
201 "chrome://" + addonName + "/skin/icon16.png",
202 infobar.PRIORITY_INFO_HIGH,
203 buttons
204 );
205 notification.persistence = persistence;
206 };
207
208 exports.loadURI = function(window, uri)
209 {
210 exports.getBrowser(window).loadURI(uri);
211 };
212
213 break;
214 }
215 case "fennec":
216 {
217 // XUL Fennec
218 exports.isKnownWindow = function(window) window.document.documentElement.get Attribute("windowtype") == "navigator:browser";
219
220 exports.getURLBar = function(window) null;
221
222 exports.getBrowser = function(window) null;
223
224 exports.applyToWindow = function(window, corrector)
225 {
226 if ("BrowserUI" in window && window.BrowserUI.goToURI && !functionHooks.ha s(window))
227 {
228 // Handle new URLs being entered
229 let unhook = hook(window.BrowserUI, "goToURI", function(url)
230 {
231 url = url || this._edit.value;
232
233 let correction = corrector(window, url);
234 if (correction)
235 url = correction;
236
237 return [url];
238 });
239 functionHooks.set(window, unhook);
240 }
241 };
242
243 exports.openInfobar = function(window, id, message, buttons, persistence)
244 {
245 if ("getNotificationBox" in window)
246 {
247 let infobar = window.getNotificationBox();
248 let notification = infobar.getNotificationWithValue(id);
249
250 if (notification)
251 {
252 infobar.removeNotification(notification);
253 }
254
255 notification = infobar.appendNotification(
256 message,
257 id,
258 "chrome://" + addonName + "/skin/icon16.png",
259 infobar.PRIORITY_INFO_HIGH,
260 buttons
261 );
262 notification.persistence = persistence;
263 }
264 };
265
266 exports.loadURI = function(window, uri)
267 {
268 if ("BrowserUI" in window && "goToURI" in window.BrowserUI)
269 {
270 window.BrowserUI.goToURI(uri);
271 }
272 };
273
274 break;
275 }
276 case "fennec2":
277 {
278 // Native Fennec
279 exports.isKnownWindow = function(window) window.document.documentElement.get Attribute("windowtype") == "navigator:browser";
280
281 exports.getURLBar = function(window) null;
282
283 exports.getBrowser = function(window) null;
284
285 exports.applyToWindow = function(window, corrector)
286 {
287 if ("BrowserApp" in window && window.BrowserApp.observe && !functionHooks. has(window))
288 {
289 let innerUnhook = null;
290 function cleanup()
291 {
292 if (innerUnhook)
293 innerUnhook();
294
295 innerUnhook = null;
296 }
297
298 let unhook = hook(window.BrowserApp, "observe", function(subject, topic, data)
299 {
300 // Huge hack: we replace addTab/loadURI when the observer is
301 // triggered. This seems to be the only way to know that the calls
302 // originate from user input.
303 let method = null;
304 if (topic == "Tab:Add")
305 method = "addTab";
306 else if (topic == "Tab:Load")
307 method = "loadURI";
308
309 if (method)
310 {
311 innerUnhook = hook(this, method, function()
312 {
313 let params = Array.prototype.slice.apply(arguments);
314 let correction = corrector(window, params[0]);
315 if (correction)
316 params[0] = correction;
317 return params;
318 });
319 }
320 }, cleanup);
321 functionHooks.set(window, unhook);
322 }
323 };
324
325 exports.openInfobar = function(window, id, message, buttons, persistence)
326 {
327 if ("BrowserApp" in window && "selectedTab" in window.BrowserApp)
328 {
329 window.NativeWindow.doorhanger.show(message, id, buttons, window.Browser App.selectedTab.id,
330 {
331 persistence: persistence
332 }
333 );
334 }
335 };
336
337 exports.loadURI = function(window, uri)
338 {
339 if ("BrowserApp" in window && "loadURI" in window.BrowserApp)
340 window.BrowserApp.loadURI(uri);
341 };
342
343 break;
344 }
345 default:
346 {
347 exports.isKnownWindow = function(window) false;
348 break;
349 }
350 }
OLDNEW
« lib/main.js ('K') | « lib/main.js ('k') | lib/typoCollector.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld