OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of the URL Fixer, |
| 3 * Copyright (C) 2006-2012 Eyeo GmbH |
| 4 * |
| 5 * URL Fixer is free software: you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * URL Fixer is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 14 * You should have received a copy of the GNU General Public License |
| 15 * along with URL Fixer. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ |
| 17 |
| 18 let {Prefs} = require("prefs"); |
| 19 let {WindowObserver} = require("windowObserver"); |
| 20 let appIntegration = require("typoAppIntegration"); |
| 21 let {onWhitelistEntryRemoved} = require("typoRules"); |
| 22 let {processUserCorrection} = require("typoCollector"); |
| 23 |
| 24 // Load HTML code to add to network error pages |
| 25 let netErrorOverlay = null; |
| 26 let request = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.ns
IXMLHttpRequest); |
| 27 request.open("GET", "chrome://" + require("info").addonName + "/content/netError
.xhtml"); |
| 28 request.addEventListener("load", function(event) |
| 29 { |
| 30 netErrorOverlay = event.target.responseXML; |
| 31 |
| 32 new WindowObserver({ |
| 33 applyToWindow: function(window) |
| 34 { |
| 35 if (!appIntegration.isKnownWindow(window)) |
| 36 return; |
| 37 |
| 38 let browser = appIntegration.getBrowser(window); |
| 39 if (browser) |
| 40 browser.addEventListener("DOMContentLoaded", handlePageLoad, false); |
| 41 }, |
| 42 |
| 43 removeFromWindow: function(window) |
| 44 { |
| 45 if (!appIntegration.isKnownWindow(window)) |
| 46 return; |
| 47 |
| 48 let browser = appIntegration.getBrowser(window); |
| 49 if (browser) |
| 50 { |
| 51 browser.removeEventListener("DOMContentLoaded", handlePageLoad, false); |
| 52 if (browser.browsers) |
| 53 { |
| 54 for (let i = 0; i < browser.browsers.length; i++) |
| 55 { |
| 56 let contentWnd = browser.browsers[i].contentWindow; |
| 57 if (contentWnd.document.documentURI.indexOf("about:neterror?") == 0) |
| 58 removeFromNetErrorPage(contentWnd); |
| 59 } |
| 60 } |
| 61 } |
| 62 } |
| 63 }); |
| 64 }, false); |
| 65 request.send(null); |
| 66 |
| 67 function handlePageLoad(event) |
| 68 { |
| 69 let document = event.target; |
| 70 if (document.documentURI.indexOf("about:neterror?") != 0 || |
| 71 document.documentURI.indexOf("e=netOffline") > 0 || |
| 72 document.documentURI.indexOf("e=notCached") > 0) |
| 73 { |
| 74 return; |
| 75 } |
| 76 |
| 77 if (!netErrorOverlay || document.getElementById("url-fixer-section")) |
| 78 return; |
| 79 |
| 80 let container = document.getElementById("errorPageContainer"); |
| 81 if (!container) |
| 82 return; |
| 83 |
| 84 container.appendChild(netErrorOverlay.documentElement.cloneNode(true)); |
| 85 |
| 86 let textField = document.getElementById("url-fixer-intention"); |
| 87 textField.value = document.defaultView.location.href; |
| 88 |
| 89 let retryButton = document.getElementById("url-fixer-retry"); |
| 90 retryButton.addEventListener("click", function() |
| 91 { |
| 92 let newURL = textField.value.replace(/^\s+/, "").replace(/\s+$/, ""); |
| 93 if (!newURL.length) |
| 94 return; |
| 95 |
| 96 let [prefix, newHost, suffix] = parseURL(newURL); |
| 97 let oldHost = document.defaultView.location.hostname.toLowerCase(); |
| 98 |
| 99 processUserCorrection(oldHost, newHost); |
| 100 |
| 101 if (newHost.indexOf("www.") == 0 && oldHost.indexOf("www.") == 0) |
| 102 { |
| 103 // Ignore www. prefix if they both start with it |
| 104 newHost = newHost.substr(4); |
| 105 oldHost = oldHost.substr(4); |
| 106 } |
| 107 if (oldHost && newHost != oldHost) |
| 108 { |
| 109 Prefs.custom_replace[oldHost] = newHost; |
| 110 Prefs.custom_replace = JSON.parse(JSON.stringify(Prefs.custom_replace)); |
| 111 } |
| 112 |
| 113 // Remove from whitelist |
| 114 if (oldHost in Prefs.whitelist) |
| 115 { |
| 116 delete Prefs.whitelist[oldHost]; |
| 117 onWhitelistEntryRemoved(oldHost); |
| 118 Prefs.whitelist = JSON.parse(JSON.stringify(Prefs.whitelist)); |
| 119 } |
| 120 |
| 121 document.defaultView.location.replace(newURL); |
| 122 }, false); |
| 123 } |
| 124 |
| 125 function removeFromNetErrorPage(window) |
| 126 { |
| 127 let overlay = window.document.getElementById("url-fixer-section"); |
| 128 if (overlay) |
| 129 overlay.parentNode.removeChild(overlay); |
| 130 } |
| 131 |
| 132 function parseURL(url) |
| 133 { |
| 134 if (/^\s*((?:\w+:)?\/*(?:[^\/#]*@)?)([^\/:#]*)/.test(url)) |
| 135 return [RegExp.$1, RegExp.$2.toLowerCase(), RegExp.rightContext]; |
| 136 else |
| 137 return [url, null, null]; |
| 138 } |
OLD | NEW |