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