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

Side by Side Diff: chrome/content/ui/firstRun.js

Issue 10585038: First-run page (revisited) (Closed)
Patch Set: Applied proposed changes (except Chrome-specific utils.js due to uncertainty) Created May 27, 2013, 12:58 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
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 function init() 18 "use strict";
19
20 (function()
19 { 21 {
20 generateLinkText(E("changeDescription")); 22 var shade;
21 23 var scrollTimer;
22 for each (let subscription in FilterStorage.subscriptions) 24
23 { 25 // Determine platform
24 if (subscription instanceof DownloadableSubscription && subscription.url != Prefs.subscriptions_exceptionsurl && !subscription.disabled) 26 var userAgent = "";
25 { 27 if (navigator.userAgent.indexOf("Gecko/") > -1)
26 E("listName").textContent = subscription.title; 28 userAgent = "firefox";
27 29 else if (navigator.userAgent.indexOf("Chrome/") > -1)
28 let link = E("listHomepage"); 30 userAgent = "chrome";
29 link.setAttribute("href", subscription.homepage); 31
30 link.setAttribute("title", subscription.homepage); 32 if (userAgent !== "")
31 33 document.documentElement.className = userAgent;
32 E("listNameContainer").removeAttribute("hidden"); 34
33 E("listNone").setAttribute("hidden", "true"); 35 // Load subscriptions for features
34 break; 36 var featureSubscriptions = [
35 } 37 {
36 } 38 feature: "malware",
37 39 title: "Malware Domains",
38 if (FilterStorage.subscriptions.some(function(s) s.url == Prefs.subscriptions_ exceptionsurl)) 40 url: "https://easylist-downloads.adblockplus.org/malwaredomains_full.txt"
Wladimir Palant 2013/05/27 14:10:37 Please add the homepage field as well - I noticed
Thomas Greiner 2013/05/27 16:39:16 Done.
39 E("acceptableAds").removeAttribute("hidden"); 41 },
40 } 42 {
41 43 feature: "social",
42 function generateLinkText(element) 44 title: "Fanboy's Social Blocking List",
43 { 45 url: "https://easylist-downloads.adblockplus.org/fanboy-social.txt"
44 let template = element.getAttribute("_textTemplate"); 46 },
45 47 {
46 let [, beforeLink, linkText, afterLink] = /(.*)\[link\](.*)\[\/link\](.*)/.exe c(template) || [null, "", template, ""]; 48 feature: "tracking",
47 while (element.firstChild && element.firstChild.nodeType != Node.ELEMENT_NODE) 49 title: "EasyPrivacy",
48 element.removeChild(element.firstChild); 50 url: "https://easylist-downloads.adblockplus.org/easyprivacy.txt"
49 while (element.lastChild && element.lastChild.nodeType != Node.ELEMENT_NODE) 51 }
50 element.removeChild(element.lastChild); 52 ];
51 if (!element.firstChild) 53
52 return; 54 // Determine scripts to load
53 55 var scripts = [];
54 element.firstChild.textContent = linkText; 56 if (userAgent == "chrome")
55 element.insertBefore(document.createTextNode(beforeLink), element.firstChild); 57 {
56 element.appendChild(document.createTextNode(afterLink)); 58 var backgroundPage = chrome.extension.getBackgroundPage();
57 } 59 var require = backgroundPage.require;
58 60 }
59 function openFilters() 61 else if (userAgent == "firefox")
60 { 62 {
61 if (Utils.isFennec) 63 scripts.push("utils.js");
62 { 64 }
63 let topWnd = window.QueryInterface(Ci.nsIInterfaceRequestor) 65 scripts.push("i18n.js");
64 .getInterface(Ci.nsIWebNavigation) 66
65 .QueryInterface(Ci.nsIDocShellTreeItem) 67 function loadScripts()
66 .rootTreeItem 68 {
67 .QueryInterface(Ci.nsIInterfaceRequestor) 69 var scriptName = scripts.shift();
68 .getInterface(Ci.nsIDOMWindow); 70 var script = document.createElement("script");
69 if (topWnd.wrappedJSObject) 71 script.type = (userAgent == "firefox") ? "application/x-javascript;version=1 .7" : "text/javascript";
70 topWnd = topWnd.wrappedJSObject; 72 script.addEventListener("load", (scripts.length == 0) ? onScriptsLoaded : lo adScripts, false);
71 73 script.src = scriptName;
72 // window.close() closes the entire window (bug 642604), make sure to close 74 document.head.appendChild(script);
73 // only a single tab instead. 75 }
74 if ("BrowserUI" in topWnd) 76
75 { 77 function onScriptsLoaded()
76 topWnd.BrowserUI.showPanel("addons-container"); 78 {
77 function showOptions() 79 if (userAgent == "chrome")
78 { 80 {
79 if (!topWnd.ExtensionsView.getElementForAddon(Utils.addonID)) 81 window.Synchronizer = require("synchronizer").Synchronizer;
80 Utils.runAsync(showOptions); 82 window.Utils = require("utils").Utils;
81 else 83 window.Prefs = require("prefs").Prefs;
82 topWnd.ExtensionsView.showOptions(Utils.addonID); 84 window.FilterStorage = require("filterStorage").FilterStorage;
83 } 85
84 showOptions(); 86 var subscriptionClasses = require("subscriptionClasses");
85 } 87 window.Subscription = subscriptionClasses.Subscription;
86 } 88 window.DownloadableSubscription = subscriptionClasses.DownloadableSubscrip tion;
87 else 89 window.Filter = require("filterClasses").Filter;
88 UI.openFiltersDialog(); 90 }
89 } 91
92 // Show warning if data corruption was detected
93 if (typeof backgroundPage != "undefined" && backgroundPage.seenDataCorruptio n)
94 {
95 document.getElementById("dataCorruptionWarning").removeAttribute("hidden") ;
Wladimir Palant 2013/05/27 14:10:37 utils.js in Firefox defines a E("dataCorruptionWar
Thomas Greiner 2013/05/27 16:39:16 Done.
96 setLinks("dataCorruptionWarning", getDocLink("knownIssuesChrome_filterstor age"));
97 }
98
99 // Set up URLs
100 var versionId;
101 var platformId;
102 if (userAgent == "firefox")
103 {
104 versionId = Utils.addonVersion.match(/^[0-9\.]+/)[0].replace(/\./g, "");
105 platformId = "firefox";
106 }
107 else if (userAgent == "chrome")
108 {
109 versionId = chrome.app.getDetails().version.split(".").slice(0, 2).join("" );
110 platformId = "google-chrome";
111 }
112 setLinks("title-changelog", "https://adblockplus.org/releases/adblock-plus-" + versionId + "-for-" + platformId + "-released");
113 setLinks("acceptableAdsExplanation", getDocLink("acceptable_ads_criteria"), openFilters);
114
115 shade = document.getElementById("shade");
116 shade.addEventListener("mouseover", scrollPage, false);
117 shade.addEventListener("mouseout", stopScroll, false);
118
119 // Set up typo feature
120 if (require("typoBootstrap"))
121 {
122 var toggleTypo = document.getElementById("toggle-typo");
123 updateToggleButton("typo", Prefs.correctTypos);
124 Prefs.addListener(function(name)
125 {
126 if (name == "correctTypos")
127 updateToggleButton("typo", Prefs.correctTypos);
128 });
Wladimir Palant 2013/05/27 14:10:37 This listener needs to be removed on unload.
Thomas Greiner 2013/05/27 16:39:16 Done.
129 toggleTypo.addEventListener("click", function(event)
130 {
131 setTypoCorrectionEnabled(!Prefs.correctTypos);
Wladimir Palant 2013/05/27 14:10:37 How about Prefs.correctTypos = !Prefs.correctTypos
Thomas Greiner 2013/05/27 16:39:16 True! :) Done.
132 }, false);
133 }
134
135 // Set up feature buttons linked to subscriptions
136 featureSubscriptions.forEach(setToggleSubscriptionButton);
137
138 window.addEventListener("resize", onWindowResize, false);
139 document.addEventListener("scroll", onScroll, false);
140
141 onWindowResize();
142
143 initSocialLinks(null);
144 }
145
146 function onScroll()
147 {
148 var currentHeight = document.documentElement.scrollTop + document.body.scrol lTop + document.documentElement.clientHeight;
149 shade.style.opacity = (document.documentElement.scrollHeight == currentHeigh t) ? "0.0" : "0.5";
150 }
151
152 function onWindowResize()
153 {
154 onScroll();
155 }
156
157 function setTypoCorrectionEnabled(enable)
158 {
159 Prefs.correctTypos = enable;
160 }
161
162 function isSubscriptionEnabled(featureSubscription)
163 {
164 return featureSubscription.url in FilterStorage.knownSubscriptions
165 && !Subscription.fromURL(featureSubscription.url).disabled;
166 }
167
168 function setToggleSubscriptionButton(featureSubscription)
169 {
170 var feature = featureSubscription.feature;
171
172 var element = document.getElementById("toggle-" + feature);
173 updateToggleButton(feature, isSubscriptionEnabled(featureSubscription));
174 element.addEventListener("click", function(event)
175 {
176 var subscription = Subscription.fromURL(featureSubscription.url);
177 if (isSubscriptionEnabled(featureSubscription))
178 FilterStorage.removeSubscription(subscription);
179 else
180 {
181 subscription.disabled = false;
182 subscription.title = featureSubscription.title;
183 subscription.homepage = featureSubscription.homepage;
184 FilterStorage.addSubscription(subscription);
185 if (!subscription.lastDownload)
186 Synchronizer.execute(subscription);
187 }
188 }, false);
189 }
190
191 function scrollPage()
192 {
193 if (scrollTimer)
194 stopScroll();
195
196 scrollTimer = setInterval(function()
197 {
198 window.scrollBy(0, 5);
199 }, 20);
200 }
201
202 function stopScroll()
203 {
204 clearTimeout(scrollTimer);
205 scrollTimer = null;
206 }
207
208 function openSharePopup(url)
209 {
210 var iframe = document.getElementById("share-popup");
211 var glassPane = document.getElementById("glass-pane");
212 var popupMessageReceived = false;
213
214 var popupMessageListener = function(event)
215 {
216 var originFilter = Filter.fromText("||adblockplus.org^");
217 if (!originFilter.matches(event.origin, "OTHER", null, null))
218 return;
219
220 var width = event.data.width;
221 var height = event.data.height;
222 iframe.width = width;
223 iframe.height = height;
224 iframe.style.marginTop = -height/2 + "px";
225 iframe.style.marginLeft = -width/2 + "px";
226 popupMessageReceived = true;
227 window.removeEventListener("message", popupMessageListener);
228 };
229 // Firefox requires last parameter to be true to be triggered by unprivilege d pages
230 window.addEventListener("message", popupMessageListener, false, true);
231
232 var popupLoadListener = function()
233 {
234 if (popupMessageReceived)
235 {
236 iframe.className = "visible";
237
238 var popupCloseListener = function()
239 {
240 iframe.className = glassPane.className = "";
241 document.removeEventListener("click", popupCloseListener);
242 };
243 document.addEventListener("click", popupCloseListener, false);
244 }
245 else
246 {
247 glassPane.className = "";
248 window.removeEventListener("message", popupMessageListener);
249 }
250
251 iframe.removeEventListener("load", popupLoadListener);
252 };
253 iframe.addEventListener("load", popupLoadListener, false);
254
255 iframe.src = url;
256 glassPane.className = "visible";
257 }
258
259 function initSocialLinks(variant)
260 {
261 var networks = ["twitter", "facebook", "gplus"];
262 networks.forEach(function(network)
263 {
264 var link = document.getElementById("share-" + network);
265 link.addEventListener("click", function(e)
266 {
267 e.preventDefault();
268 openSharePopup(getDocLink("share-" + network) + "&variant=" + variant);
269 }, false);
270 });
271 }
272
273 function setLinks(id)
274 {
275 var element = document.getElementById(id);
276 if (!element)
277 return;
278
279 var links = element.getElementsByTagName("a");
280 for (var i = 0; i < links.length; i++)
281 {
282 if (typeof arguments[i + 1] == "string")
283 {
284 links[i].href = arguments[i + 1];
285 links[i].setAttribute("target", "_blank");
286 }
287 else if (typeof arguments[i + 1] == "function")
288 {
289 links[i].href = "javascript:void(0);";
290 links[i].addEventListener("click", arguments[i + 1], false);
291 }
292 }
293 }
294
295 function getDocLink(page, anchor)
296 {
297 return Prefs.documentation_link
298 .replace(/%LINK%/g, page)
299 .replace(/%LANG%/g, Utils.appLocale) + (anchor ? "#" + anchor : "");
300 }
Wladimir Palant 2013/05/27 14:10:37 Comment not addressed (in addition to utils.js & C
Thomas Greiner 2013/05/27 16:39:16 Done.
301
302 function openFilters()
303 {
304 if (typeof UI != "undefined")
305 UI.openFiltersDialog();
306 else
307 {
308 backgroundPage.openOptions();
309 }
310 }
311
312 function updateToggleButton(feature, isEnabled)
313 {
314 var button = document.getElementById("toggle-" + feature);
315 button.className = isEnabled ? "disable" : "enable";
316 button.textContent = i18n.getMessage(isEnabled ? "firstRun_action_disable" : "firstRun_action_enable");
317 }
318
319 document.addEventListener("DOMContentLoaded", loadScripts, false);
320 })();
OLDNEW

Powered by Google App Engine
This is Rietveld