Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: lib/typoNetError.js

Issue 8948027: Ported changes from Adblock Plus integration back into URL Fixer (Closed)
Patch Set: Created Jan. 14, 2013, 10:03 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « lib/typoFixer.js ('k') | lib/typoRules.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/typoNetError.js
===================================================================
new file mode 100644
--- /dev/null
+++ b/lib/typoNetError.js
@@ -0,0 +1,138 @@
+/*
+ * This file is part of the URL Fixer,
+ * Copyright (C) 2006-2012 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/>.
+ */
+
+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)
+{
+ 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);
+ },
+
+ 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 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();
+
+ 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);
+}
+
+function removeFromNetErrorPage(window)
+{
+ let overlay = window.document.getElementById("url-fixer-section");
+ if (overlay)
+ overlay.parentNode.removeChild(overlay);
+}
+
+function parseURL(url)
+{
+ if (/^\s*((?:\w+:)?\/*(?:[^\/#]*@)?)([^\/:#]*)/.test(url))
+ return [RegExp.$1, RegExp.$2.toLowerCase(), RegExp.rightContext];
+ else
+ return [url, null, null];
+}
« no previous file with comments | « lib/typoFixer.js ('k') | lib/typoRules.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld