Index: lib/typoNetError.js |
=================================================================== |
--- a/lib/typoNetError.js |
+++ b/lib/typoNetError.js |
@@ -11,128 +11,75 @@ |
* 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/>. |
*/ |
let {Prefs} = require("prefs"); |
-let {WindowObserver} = require("windowObserver"); |
-let appIntegration = require("typoAppIntegration"); |
let {onWhitelistEntryRemoved} = require("typoRules"); |
let {processUserCorrection} = require("typoCollector"); |
-// Load HTML code to add to network error pages |
-let netErrorOverlay = null; |
-let request = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpRequest); |
-request.open("GET", "chrome://" + require("info").addonName + "/content/netError.xhtml"); |
-request.addEventListener("load", function(event) |
+function getNetErrorOverlay(message) |
{ |
- netErrorOverlay = event.target.responseXML; |
- |
- new WindowObserver({ |
- applyToWindow: function(window) |
- { |
- if (!appIntegration.isKnownWindow(window)) |
- return; |
- |
- let browser = appIntegration.getBrowser(window); |
- if (browser) |
- browser.addEventListener("DOMContentLoaded", handlePageLoad, false); |
- }, |
+ let request = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"] |
+ .createInstance(Ci.nsIXMLHttpRequest); |
+ request.open("GET", "chrome://" + require("info").addonName + |
+ "/content/netError.xhtml"); |
+ request.addEventListener("load", function(event) |
+ { |
+ message.target.sendAsyncMessage("URLFixer:NetErrorOverlay", |
+ event.target.responseText); |
+ }); |
+ request.send(null); |
+} |
- removeFromWindow: function(window) |
- { |
- if (!appIntegration.isKnownWindow(window)) |
- return; |
- |
- let browser = appIntegration.getBrowser(window); |
- if (browser) |
- { |
- browser.removeEventListener("DOMContentLoaded", handlePageLoad, false); |
- if (browser.browsers) |
- { |
- for (let i = 0; i < browser.browsers.length; i++) |
- { |
- let contentWnd = browser.browsers[i].contentWindow; |
- if (contentWnd.document.documentURI.indexOf("about:neterror?") == 0) |
- removeFromNetErrorPage(contentWnd); |
- } |
- } |
- } |
- } |
- }); |
-}, false); |
-request.send(null); |
+function processCorrection(message) |
+{ |
+ let {oldHost, newHost} = message.data; |
-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) |
+ processUserCorrection(oldHost, newHost); |
+ |
+ if (newHost.indexOf("www.") == 0 && oldHost.indexOf("www.") == 0) |
{ |
- return; |
+ // Ignore www. prefix if they both start with it |
+ newHost = newHost.substr(4); |
+ oldHost = oldHost.substr(4); |
+ } |
+ if (oldHost && newHost != oldHost) |
+ { |
+ Prefs.custom_replace[oldHost] = newHost; |
+ Prefs.custom_replace = JSON.parse(JSON.stringify(Prefs.custom_replace)); |
} |
- 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() |
+ // Remove from whitelist |
+ if (oldHost in Prefs.whitelist) |
{ |
- 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(); |
- |
- processUserCorrection(oldHost, newHost); |
- |
- if (newHost.indexOf("www.") == 0 && oldHost.indexOf("www.") == 0) |
- { |
- // Ignore www. prefix if they both start with it |
- newHost = newHost.substr(4); |
- oldHost = oldHost.substr(4); |
- } |
- if (oldHost && newHost != oldHost) |
- { |
- Prefs.custom_replace[oldHost] = newHost; |
- Prefs.custom_replace = JSON.parse(JSON.stringify(Prefs.custom_replace)); |
- } |
- |
- // Remove from whitelist |
- if (oldHost in Prefs.whitelist) |
- { |
- delete Prefs.whitelist[oldHost]; |
- onWhitelistEntryRemoved(oldHost); |
- Prefs.whitelist = JSON.parse(JSON.stringify(Prefs.whitelist)); |
- } |
- |
- document.defaultView.location.replace(newURL); |
- }, false); |
+ delete Prefs.whitelist[oldHost]; |
+ onWhitelistEntryRemoved(oldHost); |
+ Prefs.whitelist = JSON.parse(JSON.stringify(Prefs.whitelist)); |
+ } |
} |
-function removeFromNetErrorPage(window) |
-{ |
- let overlay = window.document.getElementById("url-fixer-section"); |
- if (overlay) |
- overlay.parentNode.removeChild(overlay); |
-} |
+let info = require("info"); |
+let processScript = info.addonRoot + "lib/child/typoNetError.js?" + Math.random(); |
+let messageManager = Cc["@mozilla.org/parentprocessmessagemanager;1"] |
+ .getService(Ci.nsIProcessScriptLoader) |
+ .QueryInterface(Ci.nsIMessageListenerManager) |
+ .QueryInterface(Ci.nsIMessageBroadcaster); |
+messageManager.loadProcessScript(processScript, true); |
-function parseURL(url) |
-{ |
- if (/^\s*((?:\w+:)?\/*(?:[^\/#]*@)?)([^\/:#]*)/.test(url)) |
- return [RegExp.$1, RegExp.$2.toLowerCase(), RegExp.rightContext]; |
- else |
- return [url, null, null]; |
-} |
+onShutdown.add(() => { |
+ messageManager.broadcastAsyncMessage("URLFixer:Shutdown", processScript); |
+ messageManager.removeDelayedProcessScript(processScript); |
+}); |
+ |
+messageManager.addMessageListener("URLFixer:GetNetErrorOverlay", |
+ getNetErrorOverlay); |
+messageManager.addMessageListener("URLFixer:UserCorrection", |
+ processCorrection); |
+onShutdown.add(() => { |
+ messageManager.removeMessageListener("URLFixer:GetNetErrorOverlay", |
+ getNetErrorOverlay); |
+ messageManager.removeMessageListener("URLFixer:UserCorrection", |
+ processCorrection); |
+}); |