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

Delta Between Two Patch Sets: chrome/content/ui/firstRun.js

Issue 10585038: First-run page (revisited) (Closed)
Left Patch Set: Implemented behavior of remaining buttons on Chrome; Added changelog and data corruption warning Created May 24, 2013, 10:09 a.m.
Right Patch Set: Created May 28, 2013, 2:09 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « chrome/content/ui/firstRun.html ('k') | chrome/content/ui/i18n.js » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 /* 1 /*
2 * This file is part of Adblock Plus <http://adblockplus.org/>, 2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2013 Eyeo GmbH 3 * Copyright (C) 2006-2013 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus 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 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus 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 Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 "use strict"; 18 "use strict";
19 19
20 (function() 20 (function()
21 { 21 {
22 var shade; 22 var shade;
23 var scrollTimer; 23 var scrollTimer;
24 24
25 // Determine platform
26 var userAgent = "";
27 if (navigator.userAgent.indexOf("Gecko/") > -1)
28 userAgent = "firefox";
29 else if (navigator.userAgent.indexOf("Chrome/") > -1)
30 userAgent = "chrome";
31
32 if (userAgent !== "")
33 document.documentElement.className = userAgent;
Wladimir Palant 2013/05/27 11:02:00 Please never do user agent detection, use feature
Thomas Greiner 2013/05/27 13:03:16 I wanted to but the problem is that Firefox' utils
Wladimir Palant 2013/05/27 14:10:37 Feel free to replace let by var everywhere in util
Thomas Greiner 2013/05/27 16:39:15 Done.
34
35 // Load subscriptions for features 25 // Load subscriptions for features
36 var featureSubscriptions = {}; 26 var featureSubscriptions = [
37 (function() 27 {
38 { 28 feature: "malware",
39 var request = new XMLHttpRequest(); 29 homepage: "http://malwaredomains.com/",
40 request.open("GET", "featureSubscriptions.xml", false); 30 title: "Malware Domains",
41 request.send(); 31 url: "https://easylist-downloads.adblockplus.org/malwaredomains_full.txt"
42 var subscriptions = request.responseXML.getElementsByTagName("subscription") ; 32 },
43 for (var i = 0, len = subscriptions.length; i < len; i++) 33 {
44 { 34 feature: "social",
45 var subscription = subscriptions[i]; 35 homepage: "https://www.fanboy.co.nz/",
46 featureSubscriptions[subscription.getAttribute("feature")] = { 36 title: "Fanboy's Social Blocking List",
47 "url": subscription.getAttribute("url"), 37 url: "https://easylist-downloads.adblockplus.org/fanboy-social.txt"
48 "title": subscription.getAttribute("title") 38 },
49 }; 39 {
50 } 40 feature: "tracking",
51 })(); 41 homepage: "https://easylist.adblockplus.org/",
52 42 title: "EasyPrivacy",
Wladimir Palant 2013/05/27 11:02:00 How about hardcoding the value of featureSubscript
Thomas Greiner 2013/05/27 13:03:16 Done.
53 // Determine scripts to load 43 url: "https://easylist-downloads.adblockplus.org/easyprivacy.txt"
54 var scripts = []; 44 }
55 if (userAgent == "chrome") 45 ];
56 { 46
57 var backgroundPage = chrome.extension.getBackgroundPage(); 47 function onDOMLoaded()
58 var require = backgroundPage.require; 48 {
59 }
60 else if (userAgent == "firefox")
61 {
62 scripts.push("utils.js");
63 }
64 scripts.push("i18n.js");
Wladimir Palant 2013/05/27 11:02:00 This shouldn't be necessary. As I said above, just
Thomas Greiner 2013/05/27 16:39:15 Done.
65
66 function loadScripts()
67 {
68 var scriptName = scripts.shift();
69 var script = document.createElement("script");
70 script.type = (userAgent == "firefox") ? "application/x-javascript;version=1 .7" : "text/javascript";
71 script.addEventListener("load", (scripts.length == 0) ? onScriptsLoaded : lo adScripts, false);
72 script.src = scriptName;
73 document.head.appendChild(script);
74 }
75
76 function onScriptsLoaded()
77 {
78 var isFirstRun = (userAgent == "chrome" && backgroundPage.isFirstRun)
79 || (userAgent == "firefox" && !UI.firstRunDone);
Wladimir Palant 2013/05/27 11:02:00 I think we can safely drop the update scenario now
Thomas Greiner 2013/05/27 13:03:16 Done.
80
81 if (userAgent == "chrome")
82 {
83 window.Synchronizer = require("synchronizer").Synchronizer;
84 window.Utils = require("utils").Utils;
85 window.Prefs = require("prefs").Prefs;
86 window.FilterStorage = require("filterStorage").FilterStorage;
87 window.FilterNotifier = require("filterNotifier").FilterNotifier;
88
89 var subscriptionClasses = require("subscriptionClasses");
90 window.Subscription = subscriptionClasses.Subscription;
91 window.DownloadableSubscription = subscriptionClasses.DownloadableSubscrip tion;
92 window.Filter = require("filterClasses").Filter;
Wladimir Palant 2013/05/27 11:02:00 This should be in the Chrome variant of utils.js.
Thomas Greiner 2013/05/27 16:39:15 Done.
93 }
94
95 // Set up page title
96 var titleId = isFirstRun ? "firstRun_title_install" : "firstRun_title_update ";
97 var pageTitle = i18n.getMessage(titleId);
98 document.title = document.getElementById("title-main").textContent = pageTit le;
99
100 // Only show changelog link on the update page
101 if (isFirstRun)
102 document.getElementById("title-changelog").style.display = "none";
Wladimir Palant 2013/05/27 11:02:00 See above, the update case is irrelevant by now an
Thomas Greiner 2013/05/27 13:03:16 Done.
103
104 // Show warning if data corruption was detected 49 // Show warning if data corruption was detected
105 // TODO: check in Firefox 50 if (typeof backgroundPage != "undefined" && backgroundPage.seenDataCorruptio n)
Thomas Greiner 2013/05/24 10:19:12 ignore the TODO here :)
106 if (userAgent == "chrome" && backgroundPage.seenDataCorruption) 51 {
Wladimir Palant 2013/05/27 11:02:00 Please change that into (typeof backgroundPage !=
Thomas Greiner 2013/05/27 13:03:16 Done.
107 { 52 E("dataCorruptionWarning").removeAttribute("hidden");
108 document.getElementById("dataCorruptionWarning").removeAttribute("hidden") ; 53 setLinks("dataCorruptionWarning", Utils.getDocLink("knownIssuesChrome_filt erstorage"));
109 setLinks("dataCorruptionWarning", getDocLink("knownIssuesChrome_filterstor age")); 54 }
110 } 55
111 56 // Set up URL
112 // Set up URLs 57 setLinks("acceptableAdsExplanation", Utils.getDocLink("acceptable_ads_criter ia"), openFilters);
113 var versionId; 58
114 var platformId; 59 shade = E("shade");
115 if (userAgent == "firefox")
116 {
117 versionId = Utils.addonVersion.match(/^[0-9\.]+/)[0].replace(/\./g, "");
118 platformId = "firefox";
119 }
120 else if (userAgent == "chrome")
121 {
122 versionId = chrome.app.getDetails().version.split(".").slice(0, 2).join("" );
123 platformId = "google-chrome";
124 }
125 setLinks("title-changelog", "https://adblockplus.org/releases/adblock-plus-" + versionId + "-for-" + platformId + "-released");
126 setLinks("acceptableAdsExplanation", getDocLink("acceptable_ads_criteria"), openFilters);
127
128 shade = document.getElementById("shade");
129 shade.addEventListener("mouseover", scrollPage, false); 60 shade.addEventListener("mouseover", scrollPage, false);
130 shade.addEventListener("mouseout", stopScroll, false); 61 shade.addEventListener("mouseout", stopScroll, false);
131 62
132 // Set up typo feature 63 // Set up typo feature
133 if (userAgent == "firefox") 64 if (require("typoBootstrap"))
Wladimir Palant 2013/05/27 11:02:00 Please don't assume that only Firefox has the typo
Thomas Greiner 2013/05/27 13:03:16 Done.
134 { 65 {
135 var typoSettings = document.createElement("script"); 66 var featureTypo = E("feature-typo");
136 typoSettings.type = "application/x-javascript;version=1.7"; 67 featureTypo.removeAttribute("hidden");
137 typoSettings.src = "typoSettings.js"; 68
138 document.head.appendChild(typoSettings);
139
140 var toggleTypo = document.getElementById("toggle-typo");
141 updateToggleButton("typo", Prefs.correctTypos); 69 updateToggleButton("typo", Prefs.correctTypos);
142 Prefs.addListener(function(name) 70
71 var listener = function(name)
143 { 72 {
144 if (name == "correctTypos") 73 if (name == "correctTypos")
145 updateToggleButton("typo", Prefs.correctTypos); 74 updateToggleButton("typo", Prefs.correctTypos);
146 }); 75 }
147 toggleTypo.addEventListener("click", function(event) 76 Prefs.addListener(listener);
148 { 77 window.addEventListener("unload", function(event)
149 TypoActions.setEnabled(!Prefs.correctTypos); 78 {
79 Prefs.removeListener(listener);
150 }, false); 80 }, false);
81
82 E("toggle-typo").addEventListener("click", toggleTypoCorrectionEnabled, fa lse);
151 } 83 }
152 84
153 // Set up feature buttons linked to subscriptions 85 // Set up feature buttons linked to subscriptions
154 setToggleSubscriptionButton("malware"); 86 featureSubscriptions.forEach(setToggleSubscriptionButton);
155 setToggleSubscriptionButton("social"); 87 var filterListener = function(action)
156 setToggleSubscriptionButton("tracking"); 88 {
Wladimir Palant 2013/05/27 11:02:00 How about: featureSubscriptions.forEach(setToggle
Thomas Greiner 2013/05/27 13:03:16 Done.
89 if (/^subscription\.(added|removed|disabled)$/.test(action))
90 {
91 for (var i = 0; i < featureSubscriptions.length; i++)
92 {
93 var featureSubscription = featureSubscriptions[i];
94 updateToggleButton(featureSubscription.feature, isSubscriptionEnabled( featureSubscription));
95 }
96 }
97 }
98 FilterNotifier.addListener(filterListener);
99 window.addEventListener("unload", function(event)
100 {
101 FilterNotifier.removeListener(filterListener);
102 }, false);
157 103
158 window.addEventListener("resize", onWindowResize, false); 104 window.addEventListener("resize", onWindowResize, false);
159 document.addEventListener("scroll", onScroll, false); 105 document.addEventListener("scroll", onScroll, false);
160 106
161 onWindowResize(); 107 onWindowResize();
162 108
163 initSocialLinks(null); 109 initSocialLinks(null);
164 } 110 }
165 111
166 function onScroll() 112 function onScroll()
167 { 113 {
168 var currentHeight = document.documentElement.scrollTop + document.body.scrol lTop + document.documentElement.clientHeight; 114 var currentHeight = document.documentElement.scrollTop + document.body.scrol lTop + document.documentElement.clientHeight;
169 shade.style.opacity = (document.documentElement.scrollHeight == currentHeigh t) ? "0.0" : "0.5"; 115 shade.style.opacity = (document.documentElement.scrollHeight == currentHeigh t) ? "0.0" : "0.5";
170 } 116 }
171 117
172 function onWindowResize() 118 function onWindowResize()
173 { 119 {
174 onScroll(); 120 onScroll();
175 } 121 }
176 122
177 function getSubscription(featureSubscription) 123 function toggleTypoCorrectionEnabled()
178 { 124 {
179 var subscriptions = FilterStorage.subscriptions; 125 Prefs.correctTypos = !Prefs.correctTypos;
180 for (var i = 0, len = subscriptions.length; i < len; i++)
181 {
182 var subscription = subscriptions[i];
183 if (subscription.url == featureSubscription.url && subscription instanceof DownloadableSubscription)
184 return subscription;
185 }
186
187 return null;
188 } 126 }
189 127
190 function isSubscriptionEnabled(featureSubscription) 128 function isSubscriptionEnabled(featureSubscription)
191 { 129 {
192 var subscription = getSubscription(featureSubscription); 130 return featureSubscription.url in FilterStorage.knownSubscriptions
193 return subscription != null && !subscription.disabled; 131 && !Subscription.fromURL(featureSubscription.url).disabled;
Wladimir Palant 2013/05/27 11:02:00 FilterStorage keeps a lookup table for listed subs
Thomas Greiner 2013/05/27 13:03:16 Done.
194 } 132 }
195 133
196 function setToggleSubscriptionButton(feature) 134 function setToggleSubscriptionButton(featureSubscription)
197 { 135 {
198 var featureSubscription = featureSubscriptions[feature]; 136 var feature = featureSubscription.feature;
199 137
200 var element = document.getElementById("toggle-" + feature); 138 var element = E("toggle-" + feature);
201 updateToggleButton(feature, isSubscriptionEnabled(featureSubscription)); 139 updateToggleButton(feature, isSubscriptionEnabled(featureSubscription));
202 FilterNotifier.addListener(function(action)
203 {
204 if (/^(filter|subscription)\.(added|removed|disabled)$/.test(action))
205 updateToggleButton(feature, isSubscriptionEnabled(featureSubscription));
Wladimir Palant 2013/05/27 11:02:00 How about adding a single listener that will updat
Thomas Greiner 2013/05/27 13:03:16 Not removing the listeners was a clear oversight,
Thomas Greiner 2013/05/27 16:39:15 Readded with unload.
206 });
207 element.addEventListener("click", function(event) 140 element.addEventListener("click", function(event)
208 { 141 {
209 var subscription = getSubscription(featureSubscription); 142 var subscription = Subscription.fromURL(featureSubscription.url);
210 if (subscription == null) 143 if (isSubscriptionEnabled(featureSubscription))
211 { 144 FilterStorage.removeSubscription(subscription);
212 subscription = Subscription.fromURL(featureSubscription.url); 145 else
146 {
213 subscription.disabled = false; 147 subscription.disabled = false;
214 subscription.title = featureSubscription.title; 148 subscription.title = featureSubscription.title;
215 subscription.homepage = featureSubscription.homepage; 149 subscription.homepage = featureSubscription.homepage;
216 FilterStorage.addSubscription(subscription); 150 FilterStorage.addSubscription(subscription);
217 if (subscription instanceof DownloadableSubscription && !subscription.la stDownload) 151 if (!subscription.lastDownload)
218 Synchronizer.execute(subscription); 152 Synchronizer.execute(subscription);
219 } 153 }
220 else if (!subscription.disabled)
221 FilterStorage.removeSubscription(subscription);
222 else
223 subscription.disabled = false;
Wladimir Palant 2013/05/27 11:02:00 The logic should be simpler here, either it's curr
Thomas Greiner 2013/05/27 13:03:16 Done.
224 }, false); 154 }, false);
225 } 155 }
226 156
227 function scrollPage() 157 function scrollPage()
228 { 158 {
229 if (scrollTimer) 159 if (scrollTimer)
230 stopScroll(); 160 stopScroll();
231 161
232 scrollTimer = setInterval(function() 162 scrollTimer = setInterval(function()
233 { 163 {
234 window.scrollBy(0, 5); 164 window.scrollBy(0, 5);
235 }, 20); 165 }, 20);
236 } 166 }
237 167
238 function stopScroll() 168 function stopScroll()
239 { 169 {
240 clearTimeout(scrollTimer); 170 clearTimeout(scrollTimer);
241 scrollTimer = null; 171 scrollTimer = null;
242 } 172 }
243 173
244 function openSharePopup(url) 174 function openSharePopup(url)
245 { 175 {
246 var iframe = document.getElementById("share-popup"); 176 var iframe = E("share-popup");
247 var glassPane = document.getElementById("glass-pane"); 177 var glassPane = E("glass-pane");
248 var popupMessageReceived = false; 178 var popupMessageReceived = false;
249 179
250 var popupMessageListener = function(event) 180 var popupMessageListener = function(event)
251 { 181 {
252 var originFilter = Filter.fromText("||adblockplus.org^"); 182 var originFilter = Filter.fromText("||adblockplus.org^");
253 if (!originFilter.matches(event.origin, "OTHER", null, null)) 183 if (!originFilter.matches(event.origin, "OTHER", null, null))
254 return; 184 return;
255 185
256 var width = event.data.width; 186 var width = event.data.width;
257 var height = event.data.height; 187 var height = event.data.height;
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
290 220
291 iframe.src = url; 221 iframe.src = url;
292 glassPane.className = "visible"; 222 glassPane.className = "visible";
293 } 223 }
294 224
295 function initSocialLinks(variant) 225 function initSocialLinks(variant)
296 { 226 {
297 var networks = ["twitter", "facebook", "gplus"]; 227 var networks = ["twitter", "facebook", "gplus"];
298 networks.forEach(function(network) 228 networks.forEach(function(network)
299 { 229 {
300 var link = document.getElementById("share-" + network); 230 var link = E("share-" + network);
301 link.addEventListener("click", function(e) 231 link.addEventListener("click", function(e)
302 { 232 {
303 e.preventDefault(); 233 e.preventDefault();
304 openSharePopup(getDocLink("share-" + network) + "&variant=" + variant); 234 openSharePopup(Utils.getDocLink("share-" + network) + "&variant=" + vari ant);
305 }, false); 235 }, false);
306 }); 236 });
307 } 237 }
308 238
309 function setLinks(id) 239 function setLinks(id)
310 { 240 {
311 var element = document.getElementById(id); 241 var element = E(id);
312 if (!element) 242 if (!element)
313 return; 243 return;
314 244
315 var links = element.getElementsByTagName("a"); 245 var links = element.getElementsByTagName("a");
316 for (var i = 0; i < links.length; i++) 246 for (var i = 0; i < links.length; i++)
317 { 247 {
318 if (typeof arguments[i + 1] == "string") 248 if (typeof arguments[i + 1] == "string")
319 { 249 {
320 links[i].href = arguments[i + 1]; 250 links[i].href = arguments[i + 1];
321 links[i].setAttribute("target", "_blank"); 251 links[i].setAttribute("target", "_blank");
322 } 252 }
323 else if (typeof arguments[i + 1] == "function") 253 else if (typeof arguments[i + 1] == "function")
324 { 254 {
325 links[i].href = "javascript:void(0);"; 255 links[i].href = "javascript:void(0);";
326 links[i].addEventListener("click", arguments[i + 1], false); 256 links[i].addEventListener("click", arguments[i + 1], false);
327 } 257 }
328 } 258 }
329 } 259 }
330 260
331 function getDocLink(page, anchor)
332 {
333 return Prefs.documentation_link
334 .replace(/%LINK%/g, page)
335 .replace(/%LANG%/g, Utils.appLocale) + (anchor ? "#" + anchor : "");
336 }
Wladimir Palant 2013/05/27 11:02:00 The anchor parameter is unnecessary. This is dupl
337
338 function openFilters() 261 function openFilters()
339 { 262 {
340 if (userAgent == "firefox") 263 if (typeof UI != "undefined")
341 UI.openFiltersDialog(); 264 UI.openFiltersDialog();
342 else if (userAgent == "chrome") 265 else
343 { 266 {
344 backgroundPage.openOptions(); 267 backgroundPage.openOptions();
Wladimir Palant 2013/05/27 11:02:00 Again, please don't go by user agents here: if (t
Thomas Greiner 2013/05/27 13:03:16 Done.
345 } 268 }
346 } 269 }
347 270
348 function updateToggleButton(feature, isEnabled) 271 function updateToggleButton(feature, isEnabled)
349 { 272 {
350 var button = document.getElementById("toggle-" + feature); 273 var button = E("toggle-" + feature);
351 button.className = isEnabled ? "disable" : "enable"; 274 button.className = isEnabled ? "disable" : "enable";
352 button.textContent = i18n.getMessage(isEnabled ? "firstRun_action_disable" : "firstRun_action_enable"); 275 button.textContent = i18n.getMessage(isEnabled ? "firstRun_action_disable" : "firstRun_action_enable");
353 } 276 }
354 277
355 document.addEventListener("DOMContentLoaded", loadScripts, false); 278 document.addEventListener("DOMContentLoaded", onDOMLoaded, false);
356 })(); 279 })();
LEFTRIGHT

Powered by Google App Engine
This is Rietveld