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

Side by Side Diff: lib/typoNetError.js

Issue 8948027: Ported changes from Adblock Plus integration back into URL Fixer (Closed)
Patch Set: Created Nov. 23, 2012, 3:28 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * This file is part of the URL Fixer,
3 * Copyright (C) 2006-2012 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 let {Prefs} = require("prefs");
19 let appIntegration = require("typoAppIntegration");
20 let {onWhitelistEntryRemoved} = require("typoRules");
21 let {processUserCorrection} = require("typoCollector");
22
23 // Load HTML code to add to network error pages
24 let netErrorOverlay = null;
25 let request = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.ns IXMLHttpRequest);
26 request.open("GET", "chrome://" + require("info").addonName + "/content/netError .xhtml");
27 request.addEventListener("load", function(event)
28 {
29 netErrorOverlay = event.target.responseXML;
30 }, false);
31 request.send(null);
32
33 exports.applyToWindow = applyToWindow;
Wladimir Palant 2012/12/13 16:07:00 How about netError uses its own WindowObserver so
Thomas Greiner 2013/01/14 10:06:41 Done.
34 function applyToWindow(window)
35 {
36 let browser = appIntegration.getBrowser(window);
37 if (browser)
38 browser.addEventListener("DOMContentLoaded", handlePageLoad, false);
39 }
40
41 exports.removeFromWindow = removeFromWindow;
42 function removeFromWindow(window)
43 {
44 let browser = appIntegration.getBrowser(window);
45 if (browser)
46 {
47 browser.removeEventListener("DOMContentLoaded", handlePageLoad, false);
48 if (browser.browsers)
49 {
50 for (let i = 0; i < browser.browsers.length; i++)
51 {
52 let contentWnd = browser.browsers[i].contentWindow;
53 if (contentWnd.document.documentURI.indexOf("about:neterror?") == 0)
54 removeFromNetErrorPage(contentWnd);
55 }
56 }
57 }
58 }
59
60 function handlePageLoad(event)
61 {
62 let document = event.target;
63 if (document.documentURI.indexOf("about:neterror?") != 0 ||
64 document.documentURI.indexOf("e=netOffline") > 0 ||
65 document.documentURI.indexOf("e=notCached") > 0)
66 {
67 return;
68 }
69
70 if (!netErrorOverlay || document.getElementById("url-fixer-section"))
71 return;
72
73 let container = document.getElementById("errorPageContainer");
74 if (!container)
75 return;
76
77 container.appendChild(netErrorOverlay.documentElement.cloneNode(true));
78
79 let textField = document.getElementById("url-fixer-intention");
80 textField.value = document.defaultView.location.href;
81
82 let retryButton = document.getElementById("url-fixer-retry");
83 retryButton.addEventListener("click", function()
84 {
85 let newURL = textField.value.replace(/^\s+/, "").replace(/\s+$/, "");
86 if (!newURL.length)
87 return;
88
89 let [prefix, newHost, suffix] = parseURL(newURL);
90 let oldHost = document.defaultView.location.hostname.toLowerCase();
91
92 processUserCorrection(oldHost, newHost);
93
94 if (newHost.indexOf("www.") == 0 && oldHost.indexOf("www.") == 0)
95 {
96 // Ignore www. prefix if they both start with it
97 newHost = newHost.substr(4);
98 oldHost = oldHost.substr(4);
99 }
100 if (oldHost && newHost != oldHost)
101 {
102 Prefs.custom_replace[oldHost] = newHost;
103 Prefs.custom_replace = JSON.parse(JSON.stringify(Prefs.custom_replace));
104 }
105
106 // Remove from whitelist
107 if (oldHost in Prefs.whitelist)
108 {
109 delete Prefs.whitelist[oldHost];
110 onWhitelistEntryRemoved(oldHost);
111 Prefs.whitelist = JSON.parse(JSON.stringify(Prefs.whitelist));
112 }
113
114 document.defaultView.location.replace(newURL);
115 }, false);
116 }
117
118 function removeFromNetErrorPage(window)
119 {
120 let overlay = window.document.getElementById("url-fixer-section");
121 if (overlay)
122 overlay.parentNode.removeChild(overlay);
123 }
124
125 function parseURL(url)
126 {
127 if (/^\s*((?:\w+:)?\/*(?:[^\/#]*@)?)([^\/:#]*)/.test(url))
128 return [RegExp.$1, RegExp.$2.toLowerCase(), RegExp.rightContext];
129 else
130 return [url, null, null];
131 }
OLDNEW

Powered by Google App Engine
This is Rietveld