LEFT | RIGHT |
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-2016 Eyeo GmbH | 3 * Copyright (C) 2006-2016 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 (function() |
20 { | 21 { |
21 function EventEmitter() | 22 function EventEmitter() |
22 { | 23 { |
23 this._listeners = Object.create(null); | 24 this._listeners = Object.create(null); |
24 } | 25 } |
25 EventEmitter.prototype = { | 26 EventEmitter.prototype = { |
26 on(name, listener) | 27 on(name, listener) |
27 { | 28 { |
28 if (name in this._listeners) | 29 if (name in this._listeners) |
29 this._listeners[name].push(listener); | 30 this._listeners[name].push(listener); |
30 else | 31 else |
31 this._listeners[name] = [listener]; | 32 this._listeners[name] = [listener]; |
32 }, | 33 }, |
33 off(name, listener) | 34 off(name, listener) |
34 { | 35 { |
35 let listeners = this._listeners[name]; | 36 let listeners = this._listeners[name]; |
36 if (listeners) | 37 if (listeners) |
37 { | 38 { |
38 let idx = listeners.indexOf(listener); | 39 let idx = listeners.indexOf(listener); |
39 if (idx != -1) | 40 if (idx != -1) |
40 listeners.splice(idx, 1); | 41 listeners.splice(idx, 1); |
41 } | 42 } |
42 }, | 43 }, |
43 emit(name, ...args) | 44 emit(name, ...args) |
44 { | 45 { |
45 let listeners = this._listeners[name]; | 46 let listeners = this._listeners[name]; |
46 if (listeners) | 47 if (listeners) |
47 { | 48 { |
48 for (let i = 0; i < listeners.length; i++) | 49 for (let listener of listeners) |
49 listeners[i](...args); | 50 listener(...args); |
50 } | 51 } |
51 } | 52 } |
52 }; | 53 }; |
53 | 54 |
54 function updateFromURL(data) | 55 function updateFromURL(data) |
55 { | 56 { |
56 if (window.location.search) | 57 if (window.location.search) |
57 { | 58 { |
58 let params = window.location.search.substr(1).split("&"); | 59 let params = window.location.search.substr(1).split("&"); |
59 for (let i = 0; i < params.length; i++) | 60 |
60 { | 61 for (let param of params) |
61 let parts = params[i].split("=", 2); | 62 { |
| 63 let parts = param.split("=", 2); |
62 if (parts.length == 2 && parts[0] in data) | 64 if (parts.length == 2 && parts[0] in data) |
63 data[parts[0]] = decodeURIComponent(parts[1]); | 65 data[parts[0]] = decodeURIComponent(parts[1]); |
64 } | 66 } |
65 } | 67 } |
66 } | 68 } |
67 | 69 |
68 let params = { | 70 let params = { |
69 blockedURLs: "", | 71 blockedURLs: "", |
70 filterlistsReinitialized: false, | 72 filterlistsReinitialized: false, |
71 addSubscription: false, | 73 addSubscription: false, |
(...skipping 23 matching lines...) Expand all Loading... |
95 }; | 97 }; |
96 | 98 |
97 modules.prefs = {Prefs: new EventEmitter()}; | 99 modules.prefs = {Prefs: new EventEmitter()}; |
98 let prefs = { | 100 let prefs = { |
99 notifications_ignoredcategories: (params.showNotificationUI) ? ["*"] : [], | 101 notifications_ignoredcategories: (params.showNotificationUI) ? ["*"] : [], |
100 notifications_showui: params.showNotificationUI, | 102 notifications_showui: params.showNotificationUI, |
101 shouldShowBlockElementMenu: true, | 103 shouldShowBlockElementMenu: true, |
102 show_devtools_panel: true, | 104 show_devtools_panel: true, |
103 subscriptions_exceptionsurl: "https://easylist-downloads.adblockplus.org/exc
eptionrules.txt" | 105 subscriptions_exceptionsurl: "https://easylist-downloads.adblockplus.org/exc
eptionrules.txt" |
104 }; | 106 }; |
105 Object.keys(prefs).forEach(key => | 107 for (let key of Object.keys(prefs)) |
106 { | 108 { |
107 Object.defineProperty(modules.prefs.Prefs, key, { | 109 Object.defineProperty(modules.prefs.Prefs, key, { |
108 get() | 110 get() |
109 { | 111 { |
110 return prefs[key]; | 112 return prefs[key]; |
111 }, | 113 }, |
112 set(value) | 114 set(value) |
113 { | 115 { |
114 prefs[key] = value; | 116 prefs[key] = value; |
115 modules.prefs.Prefs.emit(key); | 117 modules.prefs.Prefs.emit(key); |
116 } | 118 } |
117 }); | 119 }); |
118 }); | 120 } |
119 | 121 |
120 modules.notification = { | 122 modules.notification = { |
121 Notification: { | 123 Notification: { |
122 toggleIgnoreCategory(category) | 124 toggleIgnoreCategory(category) |
123 { | 125 { |
124 let categories = prefs.notifications_ignoredcategories; | 126 let categories = prefs.notifications_ignoredcategories; |
125 let index = categories.indexOf(category); | 127 let index = categories.indexOf(category); |
126 if (index == -1) | 128 if (index == -1) |
127 categories.push(category); | 129 categories.push(category); |
128 else | 130 else |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 return subscriptions; | 201 return subscriptions; |
200 }, | 202 }, |
201 | 203 |
202 get knownSubscriptions() | 204 get knownSubscriptions() |
203 { | 205 { |
204 return knownSubscriptions; | 206 return knownSubscriptions; |
205 }, | 207 }, |
206 | 208 |
207 addSubscription(subscription) | 209 addSubscription(subscription) |
208 { | 210 { |
209 let {fromURL} = modules.subscriptionClasses.Subscription; | 211 let {fromURL} = Subscription; |
210 let {FilterStorage} = modules.filterStorage; | 212 let {FilterStorage} = modules.filterStorage; |
211 | 213 |
212 if (!(subscription.url in FilterStorage.knownSubscriptions)) | 214 if (!(subscription.url in FilterStorage.knownSubscriptions)) |
213 { | 215 { |
214 knownSubscriptions[subscription.url] = fromURL(subscription.url); | 216 knownSubscriptions[subscription.url] = fromURL(subscription.url); |
215 modules.filterNotifier.FilterNotifier.emit("subscription.added", | 217 modules.filterNotifier.FilterNotifier.emit("subscription.added", |
216 subscription); | 218 subscription); |
217 } | 219 } |
218 }, | 220 }, |
219 | 221 |
220 removeSubscription(subscription) | 222 removeSubscription(subscription) |
221 { | 223 { |
222 let {FilterStorage} = modules.filterStorage; | 224 let {FilterStorage} = modules.filterStorage; |
223 | 225 |
224 if (subscription.url in FilterStorage.knownSubscriptions) | 226 if (subscription.url in FilterStorage.knownSubscriptions) |
225 { | 227 { |
226 delete knownSubscriptions[subscription.url]; | 228 delete knownSubscriptions[subscription.url]; |
227 modules.filterNotifier.FilterNotifier.emit("subscription.removed", | 229 modules.filterNotifier.FilterNotifier.emit("subscription.removed", |
228 subscription); | 230 subscription); |
229 } | 231 } |
230 }, | 232 }, |
231 | 233 |
232 addFilter(filter) | 234 addFilter(filter) |
233 { | 235 { |
234 for (let i = 0; i < customSubscription.filters.length; i++) | 236 for (let customFilter of customSubscription.filters) |
235 { | 237 { |
236 if (customSubscription.filters[i].text == filter.text) | 238 if (customFilter.text == filter.text) |
237 return; | 239 return; |
238 } | 240 } |
239 customSubscription.filters.push(filter); | 241 customSubscription.filters.push(filter); |
240 modules.filterNotifier.FilterNotifier.emit("filter.added", filter); | 242 modules.filterNotifier.FilterNotifier.emit("filter.added", filter); |
241 }, | 243 }, |
242 | 244 |
243 removeFilter(filter) | 245 removeFilter(filter) |
244 { | 246 { |
245 for (let i = 0; i < customSubscription.filters.length; i++) | 247 for (let i = 0; i < customSubscription.filters.length; i++) |
246 { | 248 { |
247 if (customSubscription.filters[i].text == filter.text) | 249 if (customSubscription.filters[i].text == filter.text) |
248 { | 250 { |
249 customSubscription.filters.splice(i, 1); | 251 customSubscription.filters.splice(i, 1); |
250 modules.filterNotifier.FilterNotifier.emit("filter.removed", | 252 modules.filterNotifier.FilterNotifier.emit("filter.removed", |
251 filter); | 253 filter); |
252 return; | 254 return; |
253 } | 255 } |
254 } | 256 } |
255 } | 257 } |
256 } | 258 } |
257 }; | 259 }; |
258 | 260 |
259 function Filter(text) | 261 function Filter(text) |
260 { | 262 { |
261 this.text = text; | 263 this.text = text; |
262 this.disabled = false; | 264 this.disabled = false; |
263 } | 265 } |
264 Filter.fromText = text => | 266 Filter.fromText = (text) => new Filter(text); |
265 { | |
266 return new modules.filterClasses.Filter(text); | |
267 }; | |
268 | 267 |
269 function BlockingFilter() | 268 function BlockingFilter() |
270 { | 269 { |
271 } | 270 } |
272 | 271 |
273 function RegExpFilter() | 272 function RegExpFilter() |
274 { | 273 { |
275 } | 274 } |
276 RegExpFilter.typeMap = Object.create(null); | 275 RegExpFilter.typeMap = Object.create(null); |
277 | 276 |
(...skipping 10 matching lines...) Expand all Loading... |
288 if (params.filterError) | 287 if (params.filterError) |
289 return {error: "Invalid filter"}; | 288 return {error: "Invalid filter"}; |
290 return {filter: modules.filterClasses.Filter.fromText(text)}; | 289 return {filter: modules.filterClasses.Filter.fromText(text)}; |
291 }, | 290 }, |
292 parseFilters(text) | 291 parseFilters(text) |
293 { | 292 { |
294 if (params.filterError) | 293 if (params.filterError) |
295 return {errors: ["Invalid filter"]}; | 294 return {errors: ["Invalid filter"]}; |
296 return { | 295 return { |
297 filters: text.split("\n") | 296 filters: text.split("\n") |
298 .filter(filter => !!filter) | 297 .filter((filter) => !!filter) |
299 .map(modules.filterClasses.Filter.fromText), | 298 .map(modules.filterClasses.Filter.fromText), |
300 errors: [] | 299 errors: [] |
301 }; | 300 }; |
302 } | 301 } |
303 }; | 302 }; |
304 | 303 |
305 modules.synchronizer = { | 304 modules.synchronizer = { |
306 Synchronizer: { | 305 Synchronizer: { |
307 _downloading: false, | 306 _downloading: false, |
308 execute(subscription, manual) | 307 execute(subscription, manual) |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
355 updateFromURL(modules.info); | 354 updateFromURL(modules.info); |
356 | 355 |
357 modules.subscriptionInit = { | 356 modules.subscriptionInit = { |
358 reinitialized: params.filterlistsReinitialized | 357 reinitialized: params.filterlistsReinitialized |
359 }; | 358 }; |
360 | 359 |
361 modules.messaging = { | 360 modules.messaging = { |
362 port: new EventEmitter() | 361 port: new EventEmitter() |
363 }; | 362 }; |
364 | 363 |
365 window.addEventListener("message", event => | 364 window.addEventListener("message", (event) => |
366 { | 365 { |
367 if (event.data.type != "message") | 366 if (event.data.type != "message") |
368 return; | 367 return; |
369 let message = event.data.payload; | 368 let message = event.data.payload; |
370 let {messageId} = event.data; | 369 let {messageId} = event.data; |
371 let sender = { | 370 let sender = { |
372 page: new ext.Page(event.source) | 371 page: new ext.Page(event.source) |
373 }; | 372 }; |
374 | 373 |
375 let listeners = modules.messaging.port._listeners[message.type]; | 374 let listeners = modules.messaging.port._listeners[message.type]; |
376 if (!listeners) | 375 if (!listeners) |
377 return; | 376 return; |
378 | 377 |
379 function reply(responseMessage) | 378 function reply(responseMessage) |
380 { | 379 { |
381 event.source.postMessage({ | 380 event.source.postMessage({ |
382 type: "response", | 381 type: "response", |
383 messageId, | 382 messageId, |
384 payload: responseMessage | 383 payload: responseMessage |
385 }, "*"); | 384 }, "*"); |
386 } | 385 } |
387 | 386 |
388 for (let listener of listeners) | 387 for (let listener of listeners) |
389 { | 388 { |
390 let response = listener(message, sender); | 389 let response = listener(message, sender); |
391 if (response && typeof response.then == "function") | 390 if (response && typeof response.then == "function") |
392 { | 391 { |
393 response.then( | 392 response.then( |
394 reply, | 393 reply, |
395 reason => | 394 (reason) => |
396 { | 395 { |
397 console.error(reason); | 396 console.error(reason); |
398 reply(undefined); | 397 reply(undefined); |
399 } | 398 } |
400 ); | 399 ); |
401 } | 400 } |
402 else if (typeof response != "undefined") | 401 else if (typeof response != "undefined") |
| 402 { |
403 reply(response); | 403 reply(response); |
| 404 } |
404 } | 405 } |
405 }); | 406 }); |
406 | 407 |
407 window.Services = { | 408 window.Services = { |
408 vc: { | 409 vc: { |
409 compare(v1, v2) | 410 compare(v1, v2) |
410 { | 411 { |
411 return parseFloat(v1) - parseFloat(v2); | 412 return parseFloat(v1) - parseFloat(v2); |
412 } | 413 } |
413 } | 414 } |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
462 payload: { | 463 payload: { |
463 title: "Custom subscription", | 464 title: "Custom subscription", |
464 url: "http://example.com/custom.txt", | 465 url: "http://example.com/custom.txt", |
465 confirm: true, | 466 confirm: true, |
466 type: "subscriptions.add" | 467 type: "subscriptions.add" |
467 } | 468 } |
468 }, "*"); | 469 }, "*"); |
469 }, 1000); | 470 }, 1000); |
470 } | 471 } |
471 | 472 |
472 ext.devtools.onCreated.addListener(panel => | 473 ext.devtools.onCreated.addListener((panel) => |
473 { | 474 { |
474 // blocked request | 475 // blocked request |
475 panel.sendMessage({ | 476 panel.sendMessage({ |
476 type: "add-record", | 477 type: "add-record", |
477 request: { | 478 request: { |
478 url: "http://adserver.example.com/ad_banner.png", | 479 url: "http://adserver.example.com/ad_banner.png", |
479 type: "IMAGE", | 480 type: "IMAGE", |
480 docDomain: "example.com" | 481 docDomain: "example.com" |
481 }, | 482 }, |
482 filter: { | 483 filter: { |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
538 docDomain: "example.com" | 539 docDomain: "example.com" |
539 }, | 540 }, |
540 filter: { | 541 filter: { |
541 text: "||example.com/some-annoying-popup$popup", | 542 text: "||example.com/some-annoying-popup$popup", |
542 whitelisted: false, | 543 whitelisted: false, |
543 userDefined: true, | 544 userDefined: true, |
544 subscription: null | 545 subscription: null |
545 } | 546 } |
546 }); | 547 }); |
547 }); | 548 }); |
548 } | 549 }()); |
LEFT | RIGHT |