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