OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of the URL Fixer, |
| 3 * Copyright (C) 2006-2016 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 "use strict"; |
| 19 |
| 20 const {classes: Cc, interfaces: Ci, utils: Cu} = Components; |
| 21 |
| 22 let {Services} = Cu.import("resource://gre/modules/Services.jsm", {}); |
| 23 |
| 24 let onShutdown = |
| 25 { |
| 26 shutdownHandlers: [], |
| 27 done: false, |
| 28 add: function(handler) |
| 29 { |
| 30 if (this.shutdownHandlers.indexOf(handler) < 0) |
| 31 this.shutdownHandlers.push(handler); |
| 32 }, |
| 33 remove: function(handler) |
| 34 { |
| 35 let index = this.shutdownHandlers.indexOf(handler); |
| 36 if (index >= 0) |
| 37 this.shutdownHandlers.splice(index, 1); |
| 38 } |
| 39 }; |
| 40 |
| 41 let netErrorOverlay = null; |
| 42 |
| 43 function receivedNetErrorOverlay(message) |
| 44 { |
| 45 let parser = Cc["@mozilla.org/xmlextras/domparser;1"] |
| 46 .createInstance(Ci.nsIDOMParser); |
| 47 netErrorOverlay = parser.parseFromString(message.data, "application/xml"); |
| 48 } |
| 49 |
| 50 function shutdown(message) |
| 51 { |
| 52 if (message.data == Components.stack.filename && !onShutdown.done) |
| 53 { |
| 54 onShutdown.done = true; |
| 55 for (let i = onShutdown.shutdownHandlers.length - 1; i >= 0; i --) |
| 56 { |
| 57 try |
| 58 { |
| 59 onShutdown.shutdownHandlers[i](); |
| 60 } |
| 61 catch (e) |
| 62 { |
| 63 Cu.reportError(e); |
| 64 } |
| 65 } |
| 66 onShutdown.shutdownHandlers = null; |
| 67 } |
| 68 } |
| 69 |
| 70 function onDocumentCreated(subject, topic, data) |
| 71 { |
| 72 if (topic != "content-document-global-created") |
| 73 return; |
| 74 if (!(subject instanceof Ci.nsIDOMWindow)) |
| 75 return; |
| 76 |
| 77 subject.addEventListener("DOMContentLoaded", handlePageLoad); |
| 78 } |
| 79 |
| 80 function handlePageLoad(event) |
| 81 { |
| 82 let document = event.target; |
| 83 if (document.documentURI.indexOf("about:neterror?") != 0 || |
| 84 document.documentURI.indexOf("e=netOffline") > 0 || |
| 85 document.documentURI.indexOf("e=notCached") > 0) |
| 86 { |
| 87 return; |
| 88 } |
| 89 |
| 90 if (!netErrorOverlay || document.getElementById("url-fixer-section")) |
| 91 return; |
| 92 |
| 93 let container = document.getElementById("errorPageContainer"); |
| 94 if (!container) |
| 95 return; |
| 96 |
| 97 container.appendChild(netErrorOverlay.documentElement.cloneNode(true)); |
| 98 |
| 99 let textField = document.getElementById("url-fixer-intention"); |
| 100 textField.value = document.defaultView.location.href; |
| 101 |
| 102 let retryButton = document.getElementById("url-fixer-retry"); |
| 103 retryButton.addEventListener("click", function() |
| 104 { |
| 105 let newURL = textField.value.replace(/^\s+/, "").replace(/\s+$/, ""); |
| 106 if (!newURL.length) |
| 107 return; |
| 108 |
| 109 let [prefix, newHost, suffix] = parseURL(newURL); |
| 110 let oldHost = document.defaultView.location.hostname.toLowerCase(); |
| 111 |
| 112 sendAsyncMessage("URLFixer:UserCorrection", {oldHost, newHost}); |
| 113 |
| 114 document.defaultView.location.replace(newURL); |
| 115 }, false); |
| 116 } |
| 117 |
| 118 function parseURL(url) |
| 119 { |
| 120 if (/^\s*((?:\w+:)?\/*(?:[^\/#]*@)?)([^\/:#]*)/.test(url)) |
| 121 return [RegExp.$1, RegExp.$2.toLowerCase(), RegExp.rightContext]; |
| 122 else |
| 123 return [url, null, null]; |
| 124 } |
| 125 |
| 126 addMessageListener("URLFixer:NetErrorOverlay", receivedNetErrorOverlay); |
| 127 addMessageListener("URLFixer:Shutdown", shutdown); |
| 128 onShutdown.add(() => |
| 129 { |
| 130 removeMessageListener("URLFixer:NetErrorOverlay", receivedNetErrorOverlay); |
| 131 removeMessageListener("URLFixer:Shutdown", shutdown); |
| 132 }); |
| 133 sendAsyncMessage("URLFixer:GetNetErrorOverlay"); |
| 134 |
| 135 Services.obs.addObserver(onDocumentCreated, "content-document-global-created", f
alse); |
| 136 onShutdown.add(() => |
| 137 { |
| 138 Services.obs.removeObserver(onDocumentCreated, "content-document-global-create
d"); |
| 139 }); |
OLD | NEW |