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

Side by Side Diff: popup.js

Issue 29570614: Issue 5028 - Use browser namespace (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Patch Set: Promisify Created Oct. 11, 2017, 3:22 a.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 <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 11 matching lines...) Expand all
22 22
23 const shareLinks = { 23 const shareLinks = {
24 facebook: ["https://www.facebook.com/dialog/feed", { 24 facebook: ["https://www.facebook.com/dialog/feed", {
25 app_id: "475542399197328", 25 app_id: "475542399197328",
26 link: shareURL, 26 link: shareURL,
27 redirect_uri: "https://www.facebook.com/", 27 redirect_uri: "https://www.facebook.com/",
28 ref: "adcounter", 28 ref: "adcounter",
29 name: messageMark, 29 name: messageMark,
30 actions: JSON.stringify([ 30 actions: JSON.stringify([
31 { 31 {
32 name: chrome.i18n.getMessage("stats_share_download"), 32 name: browser.i18n.getMessage("stats_share_download"),
33 link: shareURL 33 link: shareURL
34 } 34 }
35 ]) 35 ])
36 }], 36 }],
37 gplus: ["https://plus.google.com/share", { 37 gplus: ["https://plus.google.com/share", {
38 url: shareURL 38 url: shareURL
39 }], 39 }],
40 twitter: ["https://twitter.com/intent/tweet", { 40 twitter: ["https://twitter.com/intent/tweet", {
41 text: messageMark, 41 text: messageMark,
42 url: shareURL, 42 url: shareURL,
43 via: "AdblockPlus" 43 via: "AdblockPlus"
44 }] 44 }]
45 }; 45 };
46 46
47 let tab = null; 47 let tab = null;
48 48
49 function getPref(key, callback) 49 function getPref(key, callback)
50 { 50 {
51 chrome.runtime.sendMessage({type: "prefs.get", key}, callback); 51 browser.runtime.sendMessage({type: "prefs.get", key}, callback);
52 } 52 }
53 53
54 function setPref(key, value, callback) 54 function setPref(key, value, callback)
55 { 55 {
56 chrome.runtime.sendMessage({type: "prefs.set", key, value}, callback); 56 browser.runtime.sendMessage({type: "prefs.set", key, value}, callback);
57 } 57 }
58 58
59 function togglePref(key, callback) 59 function togglePref(key, callback)
60 { 60 {
61 chrome.runtime.sendMessage({type: "prefs.toggle", key}, callback); 61 browser.runtime.sendMessage({type: "prefs.toggle", key}, callback);
62 } 62 }
63 63
64 function isPageWhitelisted(callback) 64 function isPageWhitelisted(callback)
65 { 65 {
66 chrome.runtime.sendMessage({type: "filters.isWhitelisted", tab}, callback); 66 browser.runtime.sendMessage({type: "filters.isWhitelisted", tab}, callback);
67 } 67 }
68 68
69 function whenPageReady() 69 function whenPageReady()
70 { 70 {
71 return new Promise(resolve => 71 return new Promise(resolve =>
72 { 72 {
73 function onMessage(message, sender) 73 function onMessage(message, sender)
74 { 74 {
75 if (message.type == "composer.ready" && sender.page && 75 if (message.type == "composer.ready" && sender.page &&
76 sender.page.id == tab.id) 76 sender.page.id == tab.id)
77 { 77 {
78 chrome.runtime.onMessage.removeListener(onMessage); 78 browser.runtime.onMessage.removeListener(onMessage);
79 resolve(); 79 resolve();
80 } 80 }
81 } 81 }
82 82
83 chrome.runtime.onMessage.addListener(onMessage); 83 browser.runtime.onMessage.addListener(onMessage);
84 84
85 chrome.runtime.sendMessage({ 85 browser.runtime.sendMessage({
86 type: "composer.isPageReady", 86 type: "composer.isPageReady",
87 pageId: tab.id 87 pageId: tab.id
88 }, 88 },
89 ready => 89 ready =>
90 { 90 {
91 if (ready) 91 if (ready)
92 { 92 {
93 chrome.runtime.onMessage.removeListener(onMessage); 93 browser.runtime.onMessage.removeListener(onMessage);
94 resolve(); 94 resolve();
95 } 95 }
96 }); 96 });
97 }); 97 });
98 } 98 }
99 99
100 function toggleEnabled() 100 function toggleEnabled()
101 { 101 {
102 let disabled = document.body.classList.toggle("disabled"); 102 let disabled = document.body.classList.toggle("disabled");
103 chrome.runtime.sendMessage({ 103 browser.runtime.sendMessage({
104 type: disabled ? "filters.whitelist" : "filters.unwhitelist", 104 type: disabled ? "filters.whitelist" : "filters.unwhitelist",
105 tab 105 tab
106 }); 106 });
107 } 107 }
108 108
109 function activateClickHide() 109 function activateClickHide()
110 { 110 {
111 document.body.classList.add("clickhide-active"); 111 document.body.classList.add("clickhide-active");
112 chrome.tabs.sendMessage(tab.id, { 112 browser.tabs.sendMessage(tab.id, {
113 type: "composer.content.startPickingElement" 113 type: "composer.content.startPickingElement"
114 }); 114 });
115 115
116 // Close the popup after a few seconds, so user doesn't have to 116 // Close the popup after a few seconds, so user doesn't have to
117 activateClickHide.timeout = window.setTimeout(window.close, 5000); 117 activateClickHide.timeout = window.setTimeout(window.close, 5000);
118 } 118 }
119 119
120 function cancelClickHide() 120 function cancelClickHide()
121 { 121 {
122 if (activateClickHide.timeout) 122 if (activateClickHide.timeout)
123 { 123 {
124 window.clearTimeout(activateClickHide.timeout); 124 window.clearTimeout(activateClickHide.timeout);
125 activateClickHide.timeout = null; 125 activateClickHide.timeout = null;
126 } 126 }
127 document.body.classList.remove("clickhide-active"); 127 document.body.classList.remove("clickhide-active");
128 chrome.tabs.sendMessage(tab.id, {type: "composer.content.finished"}); 128 browser.tabs.sendMessage(tab.id, {type: "composer.content.finished"});
129 } 129 }
130 130
131 function toggleCollapse(event) 131 function toggleCollapse(event)
132 { 132 {
133 let collapser = event.currentTarget; 133 let collapser = event.currentTarget;
134 let collapsible = document.getElementById(collapser.dataset.collapsible); 134 let collapsible = document.getElementById(collapser.dataset.collapsible);
135 collapsible.classList.toggle("collapsed"); 135 collapsible.classList.toggle("collapsed");
136 togglePref(collapser.dataset.option); 136 togglePref(collapser.dataset.option);
137 } 137 }
138 138
139 function getDocLinks(notification) 139 function getDocLinks(notification)
140 { 140 {
141 if (!notification.links) 141 if (!notification.links)
142 return Promise.resolve([]); 142 return Promise.resolve([]);
143 143
144 return Promise.all( 144 return Promise.all(
145 notification.links.map(link => 145 notification.links.map(link =>
146 { 146 {
147 return new Promise((resolve, reject) => 147 return new Promise((resolve, reject) =>
148 { 148 {
149 chrome.runtime.sendMessage({ 149 browser.runtime.sendMessage({
150 type: "app.get", 150 type: "app.get",
151 what: "doclink", 151 what: "doclink",
152 link 152 link
153 }, resolve); 153 }, resolve);
154 }); 154 });
155 }) 155 })
156 ); 156 );
157 } 157 }
158 158
159 function insertMessage(element, text, links) 159 function insertMessage(element, text, links)
(...skipping 24 matching lines...) Expand all
184 function createShareLink(network, blockedCount) 184 function createShareLink(network, blockedCount)
185 { 185 {
186 let url = shareLinks[network][0]; 186 let url = shareLinks[network][0];
187 let params = shareLinks[network][1]; 187 let params = shareLinks[network][1];
188 188
189 let querystring = []; 189 let querystring = [];
190 for (let key in params) 190 for (let key in params)
191 { 191 {
192 let value = params[key]; 192 let value = params[key];
193 if (value == messageMark) 193 if (value == messageMark)
194 value = chrome.i18n.getMessage("stats_share_message", blockedCount); 194 value = browser.i18n.getMessage("stats_share_message", blockedCount);
195 querystring.push( 195 querystring.push(
196 encodeURIComponent(key) + "=" + encodeURIComponent(value) 196 encodeURIComponent(key) + "=" + encodeURIComponent(value)
197 ); 197 );
198 } 198 }
199 return url + "?" + querystring.join("&"); 199 return url + "?" + querystring.join("&");
200 } 200 }
201 201
202 function updateStats() 202 function updateStats()
203 { 203 {
204 let statsPage = document.getElementById("stats-page"); 204 let statsPage = document.getElementById("stats-page");
205 chrome.runtime.sendMessage({ 205 browser.runtime.sendMessage({
206 type: "stats.getBlockedPerPage", 206 type: "stats.getBlockedPerPage",
207 tab 207 tab
208 }, 208 },
209 blockedPage => 209 blockedPage =>
210 { 210 {
211 ext.i18n.setElementText(statsPage, "stats_label_page", 211 ext.i18n.setElementText(statsPage, "stats_label_page",
212 [blockedPage.toLocaleString()]); 212 [blockedPage.toLocaleString()]);
213 }); 213 });
214 214
215 let statsTotal = document.getElementById("stats-total"); 215 let statsTotal = document.getElementById("stats-total");
216 getPref("blocked_total", blockedTotal => 216 getPref("blocked_total", blockedTotal =>
217 { 217 {
218 ext.i18n.setElementText(statsTotal, "stats_label_total", 218 ext.i18n.setElementText(statsTotal, "stats_label_total",
219 [blockedTotal.toLocaleString()]); 219 [blockedTotal.toLocaleString()]);
220 }); 220 });
221 } 221 }
222 222
223 function share(event) 223 function share(event)
224 { 224 {
225 getPref("blocked_total", blockedTotal => 225 getPref("blocked_total", blockedTotal =>
226 { 226 {
227 // Easter Egg 227 // Easter Egg
228 if (blockedTotal <= 9000 || blockedTotal >= 10000) 228 if (blockedTotal <= 9000 || blockedTotal >= 10000)
229 { 229 {
230 blockedTotal = blockedTotal.toLocaleString(); 230 blockedTotal = blockedTotal.toLocaleString();
231 } 231 }
232 else 232 else
233 { 233 {
234 blockedTotal = chrome.i18n.getMessage("stats_over", 234 blockedTotal = browser.i18n.getMessage("stats_over",
235 (9000).toLocaleString()); 235 (9000).toLocaleString());
236 } 236 }
237 237
238 chrome.tabs.create({ 238 browser.tabs.create({
239 url: createShareLink(event.target.dataset.social, blockedTotal) 239 url: createShareLink(event.target.dataset.social, blockedTotal)
240 }); 240 });
241 }); 241 });
242 } 242 }
243 243
244 function toggleIconNumber() 244 function toggleIconNumber()
245 { 245 {
246 togglePref("show_statsinicon", showStatsInIcon => 246 togglePref("show_statsinicon", showStatsInIcon =>
247 { 247 {
248 document.getElementById("show-iconnumber").setAttribute( 248 document.getElementById("show-iconnumber").setAttribute(
249 "aria-checked", showStatsInIcon 249 "aria-checked", showStatsInIcon
250 ); 250 );
251 }); 251 });
252 } 252 }
253 253
254 document.addEventListener("DOMContentLoaded", () => 254 document.addEventListener("DOMContentLoaded", () =>
255 { 255 {
256 chrome.tabs.query({active: true, lastFocusedWindow: true}, tabs => 256 browser.tabs.query({active: true, lastFocusedWindow: true}, tabs =>
257 { 257 {
258 if (tabs.length > 0) 258 if (tabs.length > 0)
259 tab = {id: tabs[0].id, url: tabs[0].url}; 259 tab = {id: tabs[0].id, url: tabs[0].url};
260 260
261 let urlProtocol = tab && tab.url && new URL(tab.url).protocol; 261 let urlProtocol = tab && tab.url && new URL(tab.url).protocol;
262 262
263 // Mark page as 'local' to hide non-relevant elements 263 // Mark page as 'local' to hide non-relevant elements
264 if (urlProtocol != "http:" && urlProtocol != "https:") 264 if (urlProtocol != "http:" && urlProtocol != "https:")
265 { 265 {
266 document.body.classList.add("local"); 266 document.body.classList.add("local");
(...skipping 12 matching lines...) Expand all
279 // whether it has cached filters. If so, ask the user whether she 279 // whether it has cached filters. If so, ask the user whether she
280 // wants those filters. Otherwise, we are in default state. 280 // wants those filters. Otherwise, we are in default state.
281 if (tab) 281 if (tab)
282 { 282 {
283 isPageWhitelisted(whitelisted => 283 isPageWhitelisted(whitelisted =>
284 { 284 {
285 if (whitelisted) 285 if (whitelisted)
286 document.body.classList.add("disabled"); 286 document.body.classList.add("disabled");
287 }); 287 });
288 288
289 chrome.tabs.sendMessage(tab.id, { 289 browser.tabs.sendMessage(tab.id, {
290 type: "composer.content.getState" 290 type: "composer.content.getState"
291 }, 291 },
292 response => 292 response =>
293 { 293 {
294 if (response && response.active) 294 if (response && response.active)
295 document.body.classList.add("clickhide-active"); 295 document.body.classList.add("clickhide-active");
296 }); 296 });
297 } 297 }
298 298
299 updateStats(); 299 updateStats();
300 document.getElementById("stats-container").removeAttribute("hidden"); 300 document.getElementById("stats-container").removeAttribute("hidden");
301 }); 301 });
302 302
303 document.getElementById("enabled").addEventListener( 303 document.getElementById("enabled").addEventListener(
304 "click", toggleEnabled 304 "click", toggleEnabled
305 ); 305 );
306 document.getElementById("clickhide").addEventListener( 306 document.getElementById("clickhide").addEventListener(
307 "click", activateClickHide 307 "click", activateClickHide
308 ); 308 );
309 document.getElementById("clickhide-cancel").addEventListener( 309 document.getElementById("clickhide-cancel").addEventListener(
310 "click", cancelClickHide 310 "click", cancelClickHide
311 ); 311 );
312 document.getElementById("options").addEventListener("click", () => 312 document.getElementById("options").addEventListener("click", () =>
313 { 313 {
314 chrome.runtime.sendMessage({type: "app.open", what: "options"}); 314 browser.runtime.sendMessage({type: "app.open", what: "options"});
315 window.close(); 315 window.close();
316 }); 316 });
317 317
318 // Set up collapsing of menu items 318 // Set up collapsing of menu items
319 for (let collapser of document.getElementsByClassName("collapse")) 319 for (let collapser of document.getElementsByClassName("collapse"))
320 { 320 {
321 collapser.addEventListener("click", toggleCollapse); 321 collapser.addEventListener("click", toggleCollapse);
322 getPref(collapser.dataset.option, value => 322 getPref(collapser.dataset.option, value =>
323 { 323 {
324 if (value) 324 if (value)
(...skipping 12 matching lines...) Expand all
337 showIconNumber.setAttribute("aria-checked", showStatsInIcon); 337 showIconNumber.setAttribute("aria-checked", showStatsInIcon);
338 }); 338 });
339 showIconNumber.addEventListener("click", toggleIconNumber); 339 showIconNumber.addEventListener("click", toggleIconNumber);
340 document.querySelector("label[for='show-iconnumber']").addEventListener( 340 document.querySelector("label[for='show-iconnumber']").addEventListener(
341 "click", toggleIconNumber 341 "click", toggleIconNumber
342 ); 342 );
343 }); 343 });
344 344
345 window.addEventListener("load", () => 345 window.addEventListener("load", () =>
346 { 346 {
347 chrome.runtime.sendMessage({ 347 browser.runtime.sendMessage({
348 type: "notifications.get", 348 type: "notifications.get",
349 displayMethod: "popup" 349 displayMethod: "popup"
350 }, notification => 350 }, notification =>
351 { 351 {
352 if (!notification) 352 if (!notification)
353 return; 353 return;
354 354
355 let titleElement = document.getElementById("notification-title"); 355 let titleElement = document.getElementById("notification-title");
356 let messageElement = document.getElementById("notification-message"); 356 let messageElement = document.getElementById("notification-message");
357 357
358 titleElement.textContent = notification.texts.title; 358 titleElement.textContent = notification.texts.title;
359 359
360 getDocLinks(notification).then(docLinks => 360 getDocLinks(notification).then(docLinks =>
361 { 361 {
362 insertMessage(messageElement, notification.texts.message, docLinks); 362 insertMessage(messageElement, notification.texts.message, docLinks);
363 363
364 messageElement.addEventListener("click", event => 364 messageElement.addEventListener("click", event =>
365 { 365 {
366 let link = event.target; 366 let link = event.target;
367 while (link && link != messageElement && link.localName != "a") 367 while (link && link != messageElement && link.localName != "a")
368 link = link.parentNode; 368 link = link.parentNode;
369 if (!link) 369 if (!link)
370 return; 370 return;
371 event.preventDefault(); 371 event.preventDefault();
372 event.stopPropagation(); 372 event.stopPropagation();
373 chrome.tabs.create({url: link.href}); 373 browser.tabs.create({url: link.href});
374 }); 374 });
375 }); 375 });
376 376
377 let notificationElement = document.getElementById("notification"); 377 let notificationElement = document.getElementById("notification");
378 notificationElement.className = notification.type; 378 notificationElement.className = notification.type;
379 notificationElement.hidden = false; 379 notificationElement.hidden = false;
380 notificationElement.addEventListener("click", event => 380 notificationElement.addEventListener("click", event =>
381 { 381 {
382 if (event.target.id == "notification-close") 382 if (event.target.id == "notification-close")
383 notificationElement.classList.add("closing"); 383 notificationElement.classList.add("closing");
384 else if (event.target.id == "notification-optout" || 384 else if (event.target.id == "notification-optout" ||
385 event.target.id == "notification-hide") 385 event.target.id == "notification-hide")
386 { 386 {
387 if (event.target.id == "notification-optout") 387 if (event.target.id == "notification-optout")
388 setPref("notifications_ignoredcategories", true); 388 setPref("notifications_ignoredcategories", true);
389 389
390 notificationElement.hidden = true; 390 notificationElement.hidden = true;
391 notification.onClicked(); 391 notification.onClicked();
392 } 392 }
393 }, true); 393 }, true);
394 }); 394 });
395 }); 395 });
OLDNEW
« polyfill.js ('K') | « popup.html ('k') | qunit/tests/prefs.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld