| Index: lib/child/typoNetError.js |
| =================================================================== |
| new file mode 100644 |
| --- /dev/null |
| +++ b/lib/child/typoNetError.js |
| @@ -0,0 +1,137 @@ |
| +/* |
| + * This file is part of the URL Fixer, |
| + * Copyright (C) 2006-2016 Eyeo GmbH |
| + * |
| + * URL Fixer is free software: you can redistribute it and/or modify |
| + * it under the terms of the GNU General Public License version 3 as |
| + * published by the Free Software Foundation. |
| + * |
| + * URL Fixer is distributed in the hope that it will be useful, |
| + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| + * GNU General Public License for more details. |
| + * |
| + * You should have received a copy of the GNU General Public License |
| + * along with URL Fixer. If not, see <http://www.gnu.org/licenses/>. |
| + */ |
| + |
| +"use strict"; |
| + |
| +const {classes: Cc, interfaces: Ci, utils: Cu} = Components; |
| + |
| +let {Services} = Cu.import("resource://gre/modules/Services.jsm", {}); |
| + |
| +let onShutdown = |
| +{ |
| + shutdownHandlers: [], |
| + done: false, |
| + add: function(handler) |
| + { |
| + if (this.shutdownHandlers.indexOf(handler) < 0) |
| + this.shutdownHandlers.push(handler); |
| + }, |
| + remove: function(handler) |
| + { |
| + let index = this.shutdownHandlers.indexOf(handler); |
| + if (index >= 0) |
| + this.shutdownHandlers.splice(index, 1); |
| + } |
| +}; |
| + |
| +let netErrorOverlay = null; |
| + |
| +function receivedNetErrorOverlay(message) |
| +{ |
| + let parser = Cc["@mozilla.org/xmlextras/domparser;1"] |
| + .createInstance(Ci.nsIDOMParser); |
| + netErrorOverlay = parser.parseFromString(message.data, "application/xml"); |
|
Thomas Greiner
2016/03/08 12:34:43
Note that previously we were first loading the err
Wladimir Palant
2016/03/08 14:37:48
Yes. There is a check for netErrorOverlay in the e
|
| +} |
| + |
| +function shutdown(message) |
| +{ |
| + if (message.data == Components.stack.filename && !onShutdown.done) |
| + { |
| + onShutdown.done = true; |
| + for (let i = onShutdown.shutdownHandlers.length - 1; i >= 0; i --) |
| + { |
| + try |
| + { |
| + onShutdown.shutdownHandlers[i](); |
| + } |
| + catch (e) |
| + { |
| + Cu.reportError(e); |
| + } |
| + } |
| + onShutdown.shutdownHandlers = null; |
| + } |
| +} |
| + |
| +function onDocumentCreated(subject, topic, data) |
| +{ |
| + if (topic != "content-document-global-created") |
|
Thomas Greiner
2016/03/08 12:34:44
Detail: Is this check necessary given that you've
Wladimir Palant
2016/03/08 14:37:48
It's a somewhat theoretical scenario but some misg
|
| + return; |
| + if (!(subject instanceof Ci.nsIDOMWindow)) |
| + return; |
| + |
| + subject.addEventListener("DOMContentLoaded", handlePageLoad); |
|
Thomas Greiner
2016/03/08 12:34:43
Detail: We usually explicitly specify the "useCapt
Wladimir Palant
2016/03/08 14:37:48
Well, we can stop doing that - it's optional ever
|
| +} |
| + |
| +function handlePageLoad(event) |
| +{ |
| + let document = event.target; |
| + if (document.documentURI.indexOf("about:neterror?") != 0 || |
| + document.documentURI.indexOf("e=netOffline") > 0 || |
| + document.documentURI.indexOf("e=notCached") > 0) |
| + { |
| + return; |
| + } |
| + |
| + if (!netErrorOverlay || document.getElementById("url-fixer-section")) |
| + return; |
| + |
| + let container = document.getElementById("errorPageContainer"); |
| + if (!container) |
| + return; |
| + |
| + container.appendChild(netErrorOverlay.documentElement.cloneNode(true)); |
| + |
| + let textField = document.getElementById("url-fixer-intention"); |
| + textField.value = document.defaultView.location.href; |
| + |
| + let retryButton = document.getElementById("url-fixer-retry"); |
| + retryButton.addEventListener("click", function() |
| + { |
| + let newURL = textField.value.replace(/^\s+/, "").replace(/\s+$/, ""); |
| + if (!newURL.length) |
| + return; |
| + |
| + let [prefix, newHost, suffix] = parseURL(newURL); |
| + let oldHost = document.defaultView.location.hostname.toLowerCase(); |
| + |
| + sendAsyncMessage("URLFixer:UserCorrection", {oldHost, newHost}); |
| + |
| + document.defaultView.location.replace(newURL); |
| + }, false); |
| +} |
| + |
| +function parseURL(url) |
| +{ |
| + if (/^\s*((?:\w+:)?\/*(?:[^\/#]*@)?)([^\/:#]*)/.test(url)) |
| + return [RegExp.$1, RegExp.$2.toLowerCase(), RegExp.rightContext]; |
| + else |
| + return [url, null, null]; |
| +} |
| + |
| +addMessageListener("URLFixer:NetErrorOverlay", receivedNetErrorOverlay); |
| +addMessageListener("URLFixer:Shutdown", shutdown); |
| +onShutdown.add(() => { |
| + removeMessageListener("URLFixer:NetErrorOverlay", receivedNetErrorOverlay); |
| + removeMessageListener("URLFixer:Shutdown", shutdown); |
| +}); |
| +sendAsyncMessage("URLFixer:GetNetErrorOverlay"); |
| + |
| +Services.obs.addObserver(onDocumentCreated, "content-document-global-created", false); |
| +onShutdown.add(() => { |
| + Services.obs.removeObserver(onDocumentCreated, "content-document-global-created"); |
| +}); |