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

Side by Side Diff: popup.js

Issue 29570774: Issue 5593 - Merge notification.js and stats.js into popup.js (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Patch Set: Created Oct. 9, 2017, 5:39 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
« popup.html ('K') | « popup.html ('k') | stats.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH 3 * Copyright (C) 2006-present 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
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 { 63 {
64 if (ready) 64 if (ready)
65 { 65 {
66 chrome.runtime.onMessage.removeListener(onMessage); 66 chrome.runtime.onMessage.removeListener(onMessage);
67 resolve(); 67 resolve();
68 } 68 }
69 }); 69 });
70 }); 70 });
71 } 71 }
72 72
73 function onLoad() 73 function onLoad()
Manish Jethani 2017/10/10 05:08:56 onLoad has been inlined, i.e. it's an anonymous fu
74 { 74 {
75 chrome.tabs.query({active: true, lastFocusedWindow: true}, tabs => 75 chrome.tabs.query({active: true, lastFocusedWindow: true}, tabs =>
76 { 76 {
77 if (tabs.length > 0) 77 if (tabs.length > 0)
78 tab = {id: tabs[0].id, url: tabs[0].url}; 78 tab = {id: tabs[0].id, url: tabs[0].url};
79 79
80 let urlProtocol = tab && tab.url && new URL(tab.url).protocol; 80 let urlProtocol = tab && tab.url && new URL(tab.url).protocol;
81 81
82 // Mark page as 'local' to hide non-relevant elements 82 // Mark page as 'local' to hide non-relevant elements
83 if (urlProtocol != "http:" && urlProtocol != "https:") 83 if (urlProtocol != "http:" && urlProtocol != "https:")
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
180 180
181 function toggleCollapse(event) 181 function toggleCollapse(event)
182 { 182 {
183 let collapser = event.currentTarget; 183 let collapser = event.currentTarget;
184 let collapsible = document.getElementById(collapser.dataset.collapsible); 184 let collapsible = document.getElementById(collapser.dataset.collapsible);
185 collapsible.classList.toggle("collapsed"); 185 collapsible.classList.toggle("collapsed");
186 togglePref(collapser.dataset.option); 186 togglePref(collapser.dataset.option);
187 } 187 }
188 188
189 document.addEventListener("DOMContentLoaded", onLoad, false); 189 document.addEventListener("DOMContentLoaded", onLoad, false);
190
191 /* Notifications */
192
193 (function()
Manish Jethani 2017/10/09 17:43:24 The logic inside these anonymous functions needs t
Sebastian Noack 2017/10/09 19:29:46 Yeah, please, go ahead.
Manish Jethani 2017/10/10 05:08:56 Done.
194 {
195 function getDocLinks(notification)
196 {
197 if (!notification.links)
198 return Promise.resolve([]);
199
200 return Promise.all(
201 notification.links.map(link =>
202 {
203 return new Promise((resolve, reject) =>
204 {
205 chrome.runtime.sendMessage({
206 type: "app.get",
207 what: "doclink",
208 link
209 }, resolve);
210 });
211 })
212 );
213 }
214
215 function insertMessage(element, text, links)
216 {
217 let match = /^(.*?)<(a|strong)>(.*?)<\/\2>(.*)$/.exec(text);
218 if (!match)
219 {
220 element.appendChild(document.createTextNode(text));
221 return;
222 }
223
224 let before = match[1];
225 let tagName = match[2];
226 let value = match[3];
227 let after = match[4];
228
229 insertMessage(element, before, links);
230
231 let newElement = document.createElement(tagName);
232 if (tagName == "a" && links && links.length)
233 newElement.href = links.shift();
234 insertMessage(newElement, value, links);
235 element.appendChild(newElement);
236
237 insertMessage(element, after, links);
238 }
239
240 window.addEventListener("load", () =>
241 {
242 chrome.runtime.sendMessage({
243 type: "notifications.get",
244 displayMethod: "popup"
245 }, notification =>
246 {
247 if (!notification)
248 return;
249
250 let titleElement = document.getElementById("notification-title");
251 let messageElement = document.getElementById("notification-message");
252
253 titleElement.textContent = notification.texts.title;
254
255 getDocLinks(notification).then(docLinks =>
256 {
257 insertMessage(messageElement, notification.texts.message, docLinks);
258
259 messageElement.addEventListener("click", event =>
260 {
261 let link = event.target;
262 while (link && link != messageElement && link.localName != "a")
263 link = link.parentNode;
264 if (!link)
265 return;
266 event.preventDefault();
267 event.stopPropagation();
268 chrome.tabs.create({url: link.href});
269 });
270 });
271
272 let notificationElement = document.getElementById("notification");
273 notificationElement.className = notification.type;
274 notificationElement.hidden = false;
275 notificationElement.addEventListener("click", event =>
276 {
277 if (event.target.id == "notification-close")
278 notificationElement.classList.add("closing");
279 else if (event.target.id == "notification-optout" ||
280 event.target.id == "notification-hide")
281 {
282 if (event.target.id == "notification-optout")
283 setPref("notifications_ignoredcategories", true);
284
285 notificationElement.hidden = true;
286 notification.onClicked();
287 }
288 }, true);
289 });
290 }, false);
291 }());
292
293 /* Stats */
294
295 (function()
296 {
297 let currentTab;
298 const shareURL = "https://adblockplus.org/";
299
300 let messageMark = {};
301 let shareLinks = {
302 facebook: ["https://www.facebook.com/dialog/feed", {
303 app_id: "475542399197328",
304 link: shareURL,
305 redirect_uri: "https://www.facebook.com/",
306 ref: "adcounter",
307 name: messageMark,
308 actions: JSON.stringify([
309 {
310 name: chrome.i18n.getMessage("stats_share_download"),
311 link: shareURL
312 }
313 ])
314 }],
315 gplus: ["https://plus.google.com/share", {
316 url: shareURL
317 }],
318 twitter: ["https://twitter.com/intent/tweet", {
319 text: messageMark,
320 url: shareURL,
321 via: "AdblockPlus"
322 }]
323 };
324
325 function createShareLink(network, blockedCount)
326 {
327 let url = shareLinks[network][0];
328 let params = shareLinks[network][1];
329
330 let querystring = [];
331 for (let key in params)
332 {
333 let value = params[key];
334 if (value == messageMark)
335 value = chrome.i18n.getMessage("stats_share_message", blockedCount);
336 querystring.push(
337 encodeURIComponent(key) + "=" + encodeURIComponent(value)
338 );
339 }
340 return url + "?" + querystring.join("&");
341 }
342
343 function onLoad()
344 {
345 document.getElementById("share-box").addEventListener("click", share,
346 false);
347 let showIconNumber = document.getElementById("show-iconnumber");
348 getPref("show_statsinicon", showStatsInIcon =>
349 {
350 showIconNumber.setAttribute("aria-checked", showStatsInIcon);
351 });
352 showIconNumber.addEventListener("click", toggleIconNumber, false);
353 document.querySelector("label[for='show-iconnumber']").addEventListener(
354 "click", toggleIconNumber, false
355 );
356
357 // Update stats
358 chrome.tabs.query({active: true, lastFocusedWindow: true}, tabs =>
359 {
360 currentTab = tabs[0];
361 updateStats();
362
363 document.getElementById("stats-container").removeAttribute("hidden");
364 });
365 }
366
367 function updateStats()
368 {
369 let statsPage = document.getElementById("stats-page");
370 chrome.runtime.sendMessage({
371 type: "stats.getBlockedPerPage",
372 tab: currentTab
Manish Jethani 2017/10/10 05:08:56 Note: There's no currentTab anymore, it's just tab
373 },
374 blockedPage =>
375 {
376 ext.i18n.setElementText(statsPage, "stats_label_page",
377 [blockedPage.toLocaleString()]);
378 });
379
380 let statsTotal = document.getElementById("stats-total");
381 getPref("blocked_total", blockedTotal =>
382 {
383 ext.i18n.setElementText(statsTotal, "stats_label_total",
384 [blockedTotal.toLocaleString()]);
385 });
386 }
387
388 function share(ev)
389 {
390 getPref("blocked_total", blockedTotal =>
391 {
392 // Easter Egg
393 if (blockedTotal <= 9000 || blockedTotal >= 10000)
394 {
395 blockedTotal = blockedTotal.toLocaleString();
396 }
397 else
398 {
399 blockedTotal = chrome.i18n.getMessage("stats_over",
400 (9000).toLocaleString());
401 }
402
403 chrome.tabs.create({
404 url: createShareLink(ev.target.dataset.social, blockedTotal)
405 });
406 });
407 }
408
409 function toggleIconNumber()
410 {
411 togglePref("show_statsinicon", showStatsInIcon =>
412 {
413 document.getElementById("show-iconnumber").setAttribute(
414 "aria-checked", showStatsInIcon
415 );
416 });
417 }
418
419 document.addEventListener("DOMContentLoaded", onLoad, false);
420 }());
OLDNEW
« popup.html ('K') | « popup.html ('k') | stats.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld