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

Side by Side Diff: lib/typoFixer.js

Issue 8559070: Integrated URL Fixer into Adblock Plus (Closed)
Patch Set: Created Oct. 12, 2012, 2:18 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
« no previous file with comments | « lib/typoCollector.js ('k') | lib/typoNetError.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 Cu.import("resource://gre/modules/Services.jsm");
6 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
7
8 let {Prefs} = require("prefs");
9 let {WindowObserver} = require("windowObserver");
10 let {getSchemeCorrection, isKnownScheme, getDomainCorrection, getDomainReferral, onWhitelistEntryAdded} = require("typoRules");
11 let {processTypedDomain, processDomainCorrection, processFalsePositive} = requir e("typoCollector");
12 let appIntegration = require("typoAppIntegration");
13 let netError = require("typoNetError");
14
15 // Attach our handlers to all browser windows
16 new WindowObserver(
17 {
18 applyToWindow: function(window)
19 {
20 if (!appIntegration.isKnownWindow(window))
21 return;
22
23 netError.applyToWindow(window);
24 appIntegration.applyToWindow(window, correctURL);
25 },
26
27 removeFromWindow: function(window)
28 {
29 if (!appIntegration.isKnownWindow(window))
30 return;
31
32 netError.removeFromWindow(window);
33 appIntegration.removeFromWindow(window);
34 }
35 });
36
37 function parseURL(url)
38 {
39 if (/^\s*((?:\w+:)?\/*(?:[^\/#]*@)?)([^\/:#]*)/.test(url))
40 return [RegExp.$1, RegExp.$2.toLowerCase(), RegExp.rightContext];
41 else
42 return [url, null, null];
43 }
44
45 function isIPAddress(domain)
46 {
47 try
48 {
49 Services.eTLD.getBaseDomainFromHost(domain);
50 return false;
51 }
52 catch (e)
53 {
54 return (e.result == Cr.NS_ERROR_HOST_IS_IP_ADDRESS);
55 }
56 }
57
58 function correctURL(window, value)
59 {
60 let hasCorrection = false;
61
62 value = value.trim();
63 if (value.length == 0)
64 return null;
65
66 // Replace backslashes
67 value = value.replace(/\\/g, "/");
68
69 // Does the URL scheme need correcting?
70 if (/^([^\/]+)(\/.*)/.test(value))
71 {
72 let scheme = RegExp.$1;
73 let suffix = RegExp.$2;
74 let correction = getSchemeCorrection(scheme)
75 if (correction != scheme)
76 {
77 value = correction + suffix;
78 hasCorrection = true;
79 }
80 }
81
82 // Ignore URL schemes that we don't know
83 if (/^([\w\-]+:)/.test(value) && !isKnownScheme(RegExp.$1))
84 return null;
85
86 // Ignore search keywords and such
87 if ("getShortcutOrURI" in window && window.getShortcutOrURI(value) != value)
88 return null;
89
90 // Spaces before the first slash or period is probably a quick search
91 if (/^[^\/\.\s]+\s/.test(value))
92 return null;
93
94 let [prefix, domain, suffix] = parseURL(value);
95 if (!domain)
96 return null;
97
98 let oldDomain = domain;
99 if (!isIPAddress(domain))
100 {
101 processTypedDomain(domain);
102
103 let newDomain = getDomainCorrection(domain);
104 if (newDomain != domain)
105 {
106 processDomainCorrection(domain, newDomain);
107 domain = newDomain;
108 hasCorrection = true;
109
110 let referral = getDomainReferral(domain.replace(/^www\./, ""));
111 if (referral)
112 {
113 // We need to add a query string parameter when sending users to this do main
114 let anchorIndex = suffix.indexOf("#");
115 let anchor = "";
116 if (anchorIndex >= 0)
117 {
118 anchor = suffix.substr(anchorIndex);
119 suffix = suffix.substr(0, anchorIndex);
120 }
121
122 let queryIndex = suffix.indexOf("?");
123 if (queryIndex >= 0)
124 {
125 if (!/&$/.test(suffix))
126 suffix += "&";
127 suffix += referral;
128 }
129 else
130 {
131 if (suffix.indexOf("/") < 0)
132 suffix += "/";
133 suffix += "?" + referral;
134 }
135
136 suffix += anchor;
137 }
138 }
139 }
140
141 if (!hasCorrection)
142 return null;
143
144 // Show infobar to inform and ask about correction
145 let [message, yes, no] = getInfobarTexts();
146 message = message.replace(/\?1\?/g, prefix+domain);
147 let buttons = [
148 {
149 label: yes,
150 accessKey: null,
151 callback: function()
152 {
153 // Yes: Do nothing
154 }
155 },
156 {
157 label: no,
158 accessKey: null,
159 callback: function()
160 {
161 // No: Add to list of corrections (ignore)
162 let entry = oldDomain.replace(/^www\./, "");
163 Prefs.whitelist[entry] = true;
164 onWhitelistEntryAdded(entry);
165 Prefs.whitelist = JSON.parse(JSON.stringify(Prefs.whitelist));
166
167 appIntegration.loadURI(window, value);
168 processFalsePositive(domain, oldDomain);
169 }
170 }
171 ];
172 // We need to have persistence being set to 1 due to redirect which happens af terwards
173 appIntegration.openInfobar(window, "url-fixer-infobar-askafter", message, butt ons, 1);
174
175 require("typoSurvey").incrementCorrectionsCounter();
176
177 return prefix + domain + suffix;
178 }
179
180 let stringBundle = null;
181
182 function getInfobarTexts()
183 {
184 // Randomize URI to work around bug 719376
185 if (!stringBundle)
186 stringBundle = Services.strings.createBundle("chrome://" + require("info").a ddonName + "/locale/typo.properties?" + Math.random());
187 let result = [
188 stringBundle.GetStringFromName("urlfixer.isItCorrect"),
189 stringBundle.GetStringFromName("urlfixer.yes"),
190 stringBundle.GetStringFromName("urlfixer.no")
191 ];
192
193 getInfobarTexts = function() result;
194 return getInfobarTexts();
195 }
OLDNEW
« no previous file with comments | « lib/typoCollector.js ('k') | lib/typoNetError.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld