Left: | ||
Right: |
OLD | NEW |
---|---|
1 /* | 1 /* |
2 * This file is part of the URL Fixer, | 2 * This file is part of the URL Fixer, |
3 * Copyright (C) 2006-2016 Eyeo GmbH | 3 * Copyright (C) 2006-2016 Eyeo GmbH |
4 * | 4 * |
5 * URL Fixer is free software: you can redistribute it and/or modify | 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 | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * URL Fixer is distributed in the hope that it will be useful, | 9 * URL Fixer is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 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/>. | 15 * along with URL Fixer. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 let {Prefs} = require("prefs"); | 18 let {Prefs} = require("prefs"); |
19 let {WindowObserver} = require("windowObserver"); | |
20 let appIntegration = require("typoAppIntegration"); | |
21 let {onWhitelistEntryRemoved} = require("typoRules"); | 19 let {onWhitelistEntryRemoved} = require("typoRules"); |
22 let {processUserCorrection} = require("typoCollector"); | 20 let {processUserCorrection} = require("typoCollector"); |
23 | 21 |
24 // Load HTML code to add to network error pages | 22 function getNetErrorOverlay(message) |
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 { | 23 { |
30 netErrorOverlay = event.target.responseXML; | 24 let request = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"] |
31 | 25 .createInstance(Ci.nsIXMLHttpRequest); |
32 new WindowObserver({ | 26 request.open("GET", "chrome://" + require("info").addonName + |
33 applyToWindow: function(window) | 27 "/content/netError.xhtml"); |
34 { | 28 request.addEventListener("load", function(event) |
35 if (!appIntegration.isKnownWindow(window)) | 29 { |
36 return; | 30 message.target.sendAsyncMessage("URLFixer:NetErrorOverlay", |
37 | 31 event.target.responseText); |
38 let browser = appIntegration.getBrowser(window); | 32 }); |
39 if (browser) | 33 request.send(null); |
40 browser.addEventListener("DOMContentLoaded", handlePageLoad, false); | 34 } |
41 }, | |
42 | 35 |
43 removeFromWindow: function(window) | 36 function processCorrection(message) |
44 { | 37 { |
45 if (!appIntegration.isKnownWindow(window)) | 38 let {oldHost, newHost} = message.data; |
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 | 39 |
67 function handlePageLoad(event) | 40 processUserCorrection(oldHost, newHost); |
68 { | 41 |
69 let document = event.target; | 42 if (newHost.indexOf("www.") == 0 && oldHost.indexOf("www.") == 0) |
70 if (document.documentURI.indexOf("about:neterror?") != 0 || | |
71 document.documentURI.indexOf("e=netOffline") > 0 || | |
72 document.documentURI.indexOf("e=notCached") > 0) | |
73 { | 43 { |
74 return; | 44 // Ignore www. prefix if they both start with it |
45 newHost = newHost.substr(4); | |
46 oldHost = oldHost.substr(4); | |
47 } | |
48 if (oldHost && newHost != oldHost) | |
49 { | |
50 Prefs.custom_replace[oldHost] = newHost; | |
51 Prefs.custom_replace = JSON.parse(JSON.stringify(Prefs.custom_replace)); | |
75 } | 52 } |
76 | 53 |
77 if (!netErrorOverlay || document.getElementById("url-fixer-section")) | 54 // Remove from whitelist |
78 return; | 55 if (oldHost in Prefs.whitelist) |
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 { | 56 { |
92 let newURL = textField.value.replace(/^\s+/, "").replace(/\s+$/, ""); | 57 delete Prefs.whitelist[oldHost]; |
93 if (!newURL.length) | 58 onWhitelistEntryRemoved(oldHost); |
94 return; | 59 Prefs.whitelist = JSON.parse(JSON.stringify(Prefs.whitelist)); |
95 | 60 } |
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 } | 61 } |
124 | 62 |
125 function removeFromNetErrorPage(window) | 63 let info = require("info"); |
126 { | 64 let processScript = info.addonRoot + "lib/child/typoNetError.js?" + Math.random( ); |
127 let overlay = window.document.getElementById("url-fixer-section"); | 65 let messageManager = Cc["@mozilla.org/parentprocessmessagemanager;1"] |
128 if (overlay) | 66 .getService(Ci.nsIProcessScriptLoader) |
129 overlay.parentNode.removeChild(overlay); | 67 .QueryInterface(Ci.nsIMessageListenerManager) |
130 } | 68 .QueryInterface(Ci.nsIMessageBroadcaster); |
69 messageManager.loadProcessScript(processScript, true); | |
131 | 70 |
132 function parseURL(url) | 71 onShutdown.add(() => { |
Thomas Greiner
2016/03/08 12:34:44
Coding style: Opening braces always go on their ow
Wladimir Palant
2016/03/08 14:37:48
That style makes relatively little sense when used
Thomas Greiner
2016/03/11 17:49:21
So we'll have yet another exception to that rule?
Wladimir Palant
2016/03/13 21:56:52
I consulted eslint and it thinks that this shouldn
Thomas Greiner
2016/03/14 11:06:29
Thanks for checking. Note that this is also the ca
Wladimir Palant
2016/03/14 14:19:36
Heh, missed that due to misconfiguring eslint...
| |
133 { | 72 messageManager.broadcastAsyncMessage("URLFixer:Shutdown", processScript); |
134 if (/^\s*((?:\w+:)?\/*(?:[^\/#]*@)?)([^\/:#]*)/.test(url)) | 73 messageManager.removeDelayedProcessScript(processScript); |
135 return [RegExp.$1, RegExp.$2.toLowerCase(), RegExp.rightContext]; | 74 }); |
136 else | 75 |
137 return [url, null, null]; | 76 messageManager.addMessageListener("URLFixer:GetNetErrorOverlay", |
138 } | 77 getNetErrorOverlay); |
78 messageManager.addMessageListener("URLFixer:UserCorrection", | |
79 processCorrection); | |
80 onShutdown.add(() => { | |
81 messageManager.removeMessageListener("URLFixer:GetNetErrorOverlay", | |
82 getNetErrorOverlay); | |
83 messageManager.removeMessageListener("URLFixer:UserCorrection", | |
84 processCorrection); | |
85 }); | |
OLD | NEW |