Left: | ||
Right: |
OLD | NEW |
---|---|
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/"; | |
Manish Jethani
2017/10/10 05:08:56
These from stats.js are now constants here.
| |
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: chrome.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 chrome.runtime.sendMessage({type: "prefs.get", key}, callback); | 51 chrome.runtime.sendMessage({type: "prefs.get", key}, callback); |
25 } | 52 } |
26 | 53 |
27 function setPref(key, value, callback) | 54 function setPref(key, value, callback) |
28 { | 55 { |
29 chrome.runtime.sendMessage({type: "prefs.set", key, value}, callback); | 56 chrome.runtime.sendMessage({type: "prefs.set", key, value}, callback); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
63 { | 90 { |
64 if (ready) | 91 if (ready) |
65 { | 92 { |
66 chrome.runtime.onMessage.removeListener(onMessage); | 93 chrome.runtime.onMessage.removeListener(onMessage); |
67 resolve(); | 94 resolve(); |
68 } | 95 } |
69 }); | 96 }); |
70 }); | 97 }); |
71 } | 98 } |
72 | 99 |
73 function onLoad() | 100 function toggleEnabled() |
101 { | |
102 let disabled = document.body.classList.toggle("disabled"); | |
103 chrome.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 chrome.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 chrome.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) | |
Manish Jethani
2017/10/10 05:08:56
This is from notification.js
| |
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 chrome.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) | |
Manish Jethani
2017/10/10 05:08:56
This is from stats.js
| |
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 = chrome.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 chrome.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(ev) | |
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 = chrome.i18n.getMessage("stats_over", | |
235 (9000).toLocaleString()); | |
236 } | |
237 | |
238 chrome.tabs.create({ | |
239 url: createShareLink(ev.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", () => | |
74 { | 255 { |
75 chrome.tabs.query({active: true, lastFocusedWindow: true}, tabs => | 256 chrome.tabs.query({active: true, lastFocusedWindow: true}, tabs => |
76 { | 257 { |
77 if (tabs.length > 0) | 258 if (tabs.length > 0) |
78 tab = {id: tabs[0].id, url: tabs[0].url}; | 259 tab = {id: tabs[0].id, url: tabs[0].url}; |
79 | 260 |
80 let urlProtocol = tab && tab.url && new URL(tab.url).protocol; | 261 let urlProtocol = tab && tab.url && new URL(tab.url).protocol; |
81 | 262 |
82 // Mark page as 'local' to hide non-relevant elements | 263 // Mark page as 'local' to hide non-relevant elements |
83 if (urlProtocol != "http:" && urlProtocol != "https:") | 264 if (urlProtocol != "http:" && urlProtocol != "https:") |
(...skipping 23 matching lines...) Expand all Loading... | |
107 | 288 |
108 chrome.tabs.sendMessage(tab.id, { | 289 chrome.tabs.sendMessage(tab.id, { |
109 type: "composer.content.getState" | 290 type: "composer.content.getState" |
110 }, | 291 }, |
111 response => | 292 response => |
112 { | 293 { |
113 if (response && response.active) | 294 if (response && response.active) |
114 document.body.classList.add("clickhide-active"); | 295 document.body.classList.add("clickhide-active"); |
115 }); | 296 }); |
116 } | 297 } |
298 | |
299 updateStats(); | |
Manish Jethani
2017/10/10 05:08:56
These two lines from the previously separate onLoa
| |
300 document.getElementById("stats-container").removeAttribute("hidden"); | |
117 }); | 301 }); |
118 | 302 |
119 document.getElementById("enabled").addEventListener( | 303 document.getElementById("enabled").addEventListener( |
120 "click", toggleEnabled, false | 304 "click", toggleEnabled, false |
121 ); | 305 ); |
122 document.getElementById("clickhide").addEventListener( | 306 document.getElementById("clickhide").addEventListener( |
123 "click", activateClickHide, false | 307 "click", activateClickHide, false |
124 ); | 308 ); |
125 document.getElementById("clickhide-cancel").addEventListener( | 309 document.getElementById("clickhide-cancel").addEventListener( |
126 "click", cancelClickHide, false | 310 "click", cancelClickHide, false |
(...skipping 11 matching lines...) Expand all Loading... | |
138 getPref(collapser.dataset.option, value => | 322 getPref(collapser.dataset.option, value => |
139 { | 323 { |
140 if (value) | 324 if (value) |
141 { | 325 { |
142 document.getElementById( | 326 document.getElementById( |
143 collapser.dataset.collapsible | 327 collapser.dataset.collapsible |
144 ).classList.remove("collapsed"); | 328 ).classList.remove("collapsed"); |
145 } | 329 } |
146 }); | 330 }); |
147 } | 331 } |
148 } | |
149 | 332 |
150 function toggleEnabled() | 333 document.getElementById("share-box").addEventListener("click", share, |
Manish Jethani
2017/10/10 05:08:56
onLoad code from stats.js
| |
334 false); | |
335 let showIconNumber = document.getElementById("show-iconnumber"); | |
336 getPref("show_statsinicon", showStatsInIcon => | |
337 { | |
338 showIconNumber.setAttribute("aria-checked", showStatsInIcon); | |
339 }); | |
340 showIconNumber.addEventListener("click", toggleIconNumber, false); | |
341 document.querySelector("label[for='show-iconnumber']").addEventListener( | |
342 "click", toggleIconNumber, false | |
343 ); | |
344 }, false); | |
345 | |
346 window.addEventListener("load", () => | |
Manish Jethani
2017/10/10 05:08:56
Nothing has changed in the window's "load" handler
| |
151 { | 347 { |
152 let disabled = document.body.classList.toggle("disabled"); | |
153 chrome.runtime.sendMessage({ | 348 chrome.runtime.sendMessage({ |
154 type: disabled ? "filters.whitelist" : "filters.unwhitelist", | 349 type: "notifications.get", |
155 tab | 350 displayMethod: "popup" |
351 }, notification => | |
352 { | |
353 if (!notification) | |
354 return; | |
355 | |
356 let titleElement = document.getElementById("notification-title"); | |
357 let messageElement = document.getElementById("notification-message"); | |
358 | |
359 titleElement.textContent = notification.texts.title; | |
360 | |
361 getDocLinks(notification).then(docLinks => | |
362 { | |
363 insertMessage(messageElement, notification.texts.message, docLinks); | |
364 | |
365 messageElement.addEventListener("click", event => | |
366 { | |
367 let link = event.target; | |
368 while (link && link != messageElement && link.localName != "a") | |
369 link = link.parentNode; | |
370 if (!link) | |
371 return; | |
372 event.preventDefault(); | |
373 event.stopPropagation(); | |
374 chrome.tabs.create({url: link.href}); | |
375 }); | |
376 }); | |
377 | |
378 let notificationElement = document.getElementById("notification"); | |
379 notificationElement.className = notification.type; | |
380 notificationElement.hidden = false; | |
381 notificationElement.addEventListener("click", event => | |
382 { | |
383 if (event.target.id == "notification-close") | |
384 notificationElement.classList.add("closing"); | |
385 else if (event.target.id == "notification-optout" || | |
386 event.target.id == "notification-hide") | |
387 { | |
388 if (event.target.id == "notification-optout") | |
389 setPref("notifications_ignoredcategories", true); | |
390 | |
391 notificationElement.hidden = true; | |
392 notification.onClicked(); | |
393 } | |
394 }, true); | |
156 }); | 395 }); |
157 } | 396 }, false); |
158 | |
159 function activateClickHide() | |
160 { | |
161 document.body.classList.add("clickhide-active"); | |
162 chrome.tabs.sendMessage(tab.id, { | |
163 type: "composer.content.startPickingElement" | |
164 }); | |
165 | |
166 // Close the popup after a few seconds, so user doesn't have to | |
167 activateClickHide.timeout = window.setTimeout(window.close, 5000); | |
168 } | |
169 | |
170 function cancelClickHide() | |
171 { | |
172 if (activateClickHide.timeout) | |
173 { | |
174 window.clearTimeout(activateClickHide.timeout); | |
175 activateClickHide.timeout = null; | |
176 } | |
177 document.body.classList.remove("clickhide-active"); | |
178 chrome.tabs.sendMessage(tab.id, {type: "composer.content.finished"}); | |
179 } | |
180 | |
181 function toggleCollapse(event) | |
182 { | |
183 let collapser = event.currentTarget; | |
184 let collapsible = document.getElementById(collapser.dataset.collapsible); | |
185 collapsible.classList.toggle("collapsed"); | |
186 togglePref(collapser.dataset.option); | |
187 } | |
188 | |
189 document.addEventListener("DOMContentLoaded", onLoad, false); | |
OLD | NEW |