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

Delta Between Two Patch Sets: popup.js

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

Powered by Google App Engine
This is Rietveld