Left: | ||
Right: |
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-2015 Eyeo GmbH | 3 * Copyright (C) 2006-2015 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 20 matching lines...) Expand all Loading... | |
31 } | 31 } |
32 | 32 |
33 Collection.prototype.addItems = function() | 33 Collection.prototype.addItems = function() |
34 { | 34 { |
35 var length = Array.prototype.push.apply(this.items, arguments); | 35 var length = Array.prototype.push.apply(this.items, arguments); |
36 if (length == 0) | 36 if (length == 0) |
37 return; | 37 return; |
38 | 38 |
39 this.items.sort(function(a, b) | 39 this.items.sort(function(a, b) |
40 { | 40 { |
41 var aValue = (a.title || a.url || a.text).toLowerCase(); | 41 var aValue = (a.title || a.text || a.url).toLowerCase(); |
42 var bValue = (b.title || b.url || a.text).toLowerCase(); | 42 var bValue = (b.title || b.text || b.url).toLowerCase(); |
43 return aValue.localeCompare(bValue); | 43 return aValue.localeCompare(bValue); |
44 }); | 44 }); |
45 | 45 |
46 for (var j = 0; j < this.details.length; j++) | 46 for (var j = 0; j < this.details.length; j++) |
47 { | 47 { |
48 var table = E(this.details[j].id); | 48 var table = E(this.details[j].id); |
49 var template = table.querySelector("template"); | 49 var template = table.querySelector("template"); |
50 for (var i = 0; i < arguments.length; i++) | 50 for (var i = 0; i < arguments.length; i++) |
51 { | 51 { |
52 var item = arguments[i]; | 52 var item = arguments[i]; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
100 table.appendChild(template); | 100 table.appendChild(template); |
101 } | 101 } |
102 this.items.length = 0; | 102 this.items.length = 0; |
103 }; | 103 }; |
104 | 104 |
105 function onToggleSubscriptionClick(e) | 105 function onToggleSubscriptionClick(e) |
106 { | 106 { |
107 e.preventDefault(); | 107 e.preventDefault(); |
108 var subscriptionUrl = e.target.parentNode.getAttribute("data-access"); | 108 var subscriptionUrl = e.target.parentNode.getAttribute("data-access"); |
109 if (!e.target.checked) | 109 if (!e.target.checked) |
110 removeSubscription(subscriptionUrl); | 110 { |
111 ext.backgroundPage.sendMessage( | |
112 { | |
113 type: "subscriptions.remove", | |
114 url: subscriptionUrl | |
115 }); | |
116 } | |
111 else | 117 else |
112 addEnableSubscription(subscriptionUrl); | 118 addEnableSubscription(subscriptionUrl); |
113 } | 119 } |
114 | 120 |
115 function onAddLanguageSubscriptionClick(e) | 121 function onAddLanguageSubscriptionClick(e) |
116 { | 122 { |
117 e.preventDefault(); | 123 e.preventDefault(); |
118 var url = this.parentNode.getAttribute("data-access"); | 124 var url = this.parentNode.getAttribute("data-access"); |
119 addEnableSubscription(url); | 125 addEnableSubscription(url); |
120 } | 126 } |
121 | 127 |
122 function onRemoveFilterClick() | 128 function onRemoveFilterClick() |
123 { | 129 { |
124 var filter = this.parentNode.getAttribute("data-access"); | 130 var filter = this.parentNode.getAttribute("data-access"); |
125 removeFilter(filter); | 131 ext.backgroundPage.sendMessage( |
132 { | |
133 type: "filters.remove", | |
134 text: filter | |
135 }); | |
126 } | 136 } |
127 | 137 |
128 collections.popular = new Collection( | 138 collections.popular = new Collection( |
129 [ | 139 [ |
130 { | 140 { |
131 id: "recommend-list-table", | 141 id: "recommend-list-table", |
132 onClick: onToggleSubscriptionClick | 142 onClick: onToggleSubscriptionClick |
133 } | 143 } |
134 ]); | 144 ]); |
135 collections.langs = new Collection( | 145 collections.langs = new Collection( |
(...skipping 25 matching lines...) Expand all Loading... | |
161 { | 171 { |
162 id: "custom-list-table", | 172 id: "custom-list-table", |
163 onClick: onToggleSubscriptionClick | 173 onClick: onToggleSubscriptionClick |
164 } | 174 } |
165 ]); | 175 ]); |
166 collections.whitelist = new Collection( | 176 collections.whitelist = new Collection( |
167 [ | 177 [ |
168 { | 178 { |
169 id: "whitelisting-table", | 179 id: "whitelisting-table", |
170 onClick: onRemoveFilterClick | 180 onClick: onRemoveFilterClick |
181 } | |
182 ]); | |
183 collections.customFilters = new Collection( | |
184 [ | |
185 { | |
186 id: "custom-filters-table" | |
171 } | 187 } |
172 ]); | 188 ]); |
173 | 189 |
174 function updateSubscription(subscription) | 190 function updateSubscription(subscription) |
175 { | 191 { |
176 var subscriptionUrl = subscription.url; | 192 var subscriptionUrl = subscription.url; |
177 var knownSubscription = subscriptionsMap[subscriptionUrl]; | 193 var knownSubscription = subscriptionsMap[subscriptionUrl]; |
178 if (knownSubscription) | 194 if (knownSubscription) |
179 knownSubscription.disabled = subscription.disabled; | 195 knownSubscription.disabled = subscription.disabled; |
180 else | 196 else |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
259 } | 275 } |
260 } | 276 } |
261 | 277 |
262 function updateFilter(filter) | 278 function updateFilter(filter) |
263 { | 279 { |
264 var match = filter.text.match(/^@@\|\|([^\/:]+)\^\$document$/); | 280 var match = filter.text.match(/^@@\|\|([^\/:]+)\^\$document$/); |
265 if (match && !filtersMap[filter.text]) | 281 if (match && !filtersMap[filter.text]) |
266 { | 282 { |
267 filter.title = match[1]; | 283 filter.title = match[1]; |
268 collections.whitelist.addItems(filter); | 284 collections.whitelist.addItems(filter); |
269 filtersMap[filter.text] = filter | |
270 } | 285 } |
271 else | 286 else |
272 { | 287 collections.customFilters.addItems(filter); |
273 // TODO: add `filters[i].text` to list of custom filters | 288 |
274 } | 289 filtersMap[filter.text] = filter; |
275 } | 290 } |
276 | 291 |
277 function loadRecommendations() | 292 function loadRecommendations() |
278 { | 293 { |
279 var request = new XMLHttpRequest(); | 294 var request = new XMLHttpRequest(); |
280 request.open("GET", "subscriptions.xml", false); | 295 request.open("GET", "subscriptions.xml", false); |
281 request.addEventListener("load", function() | 296 request.addEventListener("load", function() |
282 { | 297 { |
283 var list = document.getElementById("subscriptionSelector"); | 298 var list = document.getElementById("subscriptionSelector"); |
284 var docElem = request.responseXML.documentElement; | 299 var docElem = request.responseXML.documentElement; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
323 { | 338 { |
324 if (!element) | 339 if (!element) |
325 return; | 340 return; |
326 | 341 |
327 if (element.hasAttribute("data-action")) | 342 if (element.hasAttribute("data-action")) |
328 break; | 343 break; |
329 | 344 |
330 element = element.parentElement; | 345 element = element.parentElement; |
331 } | 346 } |
332 | 347 |
333 switch (element.getAttribute("data-action")) | 348 var actions = element.getAttribute("data-action").split(","); |
334 { | 349 for (var i = 0; i < actions.length; i++) |
335 case "add-domain-exception": | 350 { |
336 addWhitelistedDomain(); | 351 switch (actions[i]) |
337 break; | 352 { |
338 case "add-predefined-subscription": | 353 case "add-domain-exception": |
339 var dialog = E("dialog-content-predefined"); | 354 addWhitelistedDomain(); |
340 var title = dialog.querySelector("h3").textContent; | 355 break; |
341 var url = dialog.querySelector(".url").textContent; | 356 case "add-predefined-subscription": |
342 addEnableSubscription(url, title); | 357 var dialog = E("dialog-content-predefined"); |
343 document.body.removeAttribute("data-dialog"); | 358 var title = dialog.querySelector("h3").textContent; |
344 break; | 359 var url = dialog.querySelector(".url").textContent; |
345 case "cancel-domain-exception": | 360 addEnableSubscription(url, title); |
346 E("whitelisting-textbox").value = ""; | 361 document.body.removeAttribute("data-dialog"); |
347 break; | 362 break; |
348 case "close-dialog": | 363 case "cancel-custom-filters": |
349 document.body.removeAttribute("data-dialog"); | 364 E("custom-filters").classList.remove("mode-edit"); |
saroyanm
2015/07/16 12:32:27
Strange, classList according to MDN is supported s
Sebastian Noack
2015/07/16 12:36:50
As I already told you in a different comment, it's
saroyanm
2015/07/16 12:41:01
Ahh right your reference to caniuse went out of my
| |
350 break; | 365 break; |
351 case "edit-custom-filters": | 366 case "cancel-domain-exception": |
352 editCustomFilters(); | 367 E("whitelisting-textbox").value = ""; |
353 break; | 368 break; |
354 case "import-subscription": | 369 case "close-dialog": |
355 var url = E("blockingList-textbox").value; | 370 document.body.removeAttribute("data-dialog"); |
356 addEnableSubscription(url); | 371 break; |
357 document.body.removeAttribute("data-dialog"); | 372 case "edit-custom-filters": |
358 break; | 373 E("custom-filters").classList.add("mode-edit"); |
359 case "open-dialog": | 374 editCustomFilters(); |
360 openDialog(element.getAttribute("data-dialog")); | 375 break; |
361 break; | 376 case "import-subscription": |
362 case "switch-tab": | 377 var url = E("blockingList-textbox").value; |
363 document.body.setAttribute("data-tab", | 378 addEnableSubscription(url); |
364 element.getAttribute("data-tab")); | 379 document.body.removeAttribute("data-dialog"); |
365 break; | 380 break; |
381 case "open-dialog": | |
382 openDialog(element.getAttribute("data-dialog")); | |
383 break; | |
384 case "save-custom-filters": | |
385 ext.backgroundPage.sendMessage( | |
386 { | |
387 type: "filters.importRaw", | |
388 text: E("custom-filters-raw").value | |
389 }); | |
390 E("custom-filters").classList.remove("mode-edit"); | |
391 break; | |
392 case "switch-tab": | |
393 document.body.setAttribute("data-tab", | |
394 element.getAttribute("data-tab")); | |
395 break; | |
396 } | |
366 } | 397 } |
367 } | 398 } |
368 | 399 |
369 function onDOMLoaded() | 400 function onDOMLoaded() |
370 { | 401 { |
371 var recommendationTemplate = document.querySelector("#recommend-list-table t emplate"); | 402 var recommendationTemplate = document.querySelector("#recommend-list-table t emplate"); |
372 var popularText = ext.i18n.getMessage("options_popular"); | 403 var popularText = ext.i18n.getMessage("options_popular"); |
373 recommendationTemplate.content.querySelector(".popular").textContent = popul arText; | 404 recommendationTemplate.content.querySelector(".popular").textContent = popul arText; |
374 var languagesTemplate = document.querySelector("#all-lang-table template"); | 405 var languagesTemplate = document.querySelector("#all-lang-table template"); |
375 var buttonText = ext.i18n.getMessage("options_button_add"); | 406 var buttonText = ext.i18n.getMessage("options_button_add"); |
376 languagesTemplate.content.querySelector(".button-add span").textContent = bu ttonText; | 407 languagesTemplate.content.querySelector(".button-add span").textContent = bu ttonText; |
377 | 408 |
378 populateLists(); | 409 populateLists(); |
379 | 410 |
380 function onFindLanguageKeyUp() | 411 function onFindLanguageKeyUp() |
381 { | 412 { |
382 var searchStyle = E("search-style"); | 413 var searchStyle = E("search-style"); |
383 if (!this.value) | 414 if (!this.value) |
384 searchStyle.innerHTML = ""; | 415 searchStyle.innerHTML = ""; |
385 else | 416 else |
386 searchStyle.innerHTML = "#all-lang-table li:not([data-search*=\"" + this .value.toLowerCase() + "\"]) { display: none; }"; | 417 searchStyle.innerHTML = "#all-lang-table li:not([data-search*=\"" + this .value.toLowerCase() + "\"]) { display: none; }"; |
387 } | 418 } |
388 | 419 |
420 function isEnterPressed(e) | |
421 { | |
422 // e.keyCode has been deprecated so we attempt to use e.key | |
423 if ("key" in e) | |
424 return e.key == "Enter"; | |
425 return e.keyCode == 13; // keyCode "13" corresponds to "Enter" | |
426 } | |
427 | |
389 // Initialize navigation sidebar | 428 // Initialize navigation sidebar |
390 ext.backgroundPage.sendMessage( | 429 ext.backgroundPage.sendMessage( |
391 { | 430 { |
392 type: "app.get", | 431 type: "app.get", |
393 what: "addonVersion" | 432 what: "addonVersion" |
394 }, | 433 }, |
395 function(addonVersion) | 434 function(addonVersion) |
396 { | 435 { |
397 E("abp-version").textContent = addonVersion; | 436 E("abp-version").textContent = addonVersion; |
398 }); | 437 }); |
399 getDocLink("releases", function(link) | 438 getDocLink("releases", function(link) |
400 { | 439 { |
401 E("link-version").setAttribute("href", link); | 440 E("link-version").setAttribute("href", link); |
402 }); | 441 }); |
403 | 442 |
404 getDocLink("contribute", function(link) | 443 getDocLink("contribute", function(link) |
405 { | 444 { |
406 document.querySelector("#tab-contribute a").setAttribute("href", link); | 445 document.querySelector("#tab-contribute a").setAttribute("href", link); |
407 }); | 446 }); |
408 | 447 |
409 updateShareLink(); | 448 updateShareLink(); |
410 | 449 |
411 // Initialize interactive UI elements | 450 // Initialize interactive UI elements |
412 document.body.addEventListener("click", onClick, false); | 451 document.body.addEventListener("click", onClick, false); |
413 var placeholderValue = ext.i18n.getMessage("options_dialog_language_find"); | 452 var placeholderValue = ext.i18n.getMessage("options_dialog_language_find"); |
414 E("find-language").setAttribute("placeholder", placeholderValue); | 453 E("find-language").setAttribute("placeholder", placeholderValue); |
415 E("find-language").addEventListener("keyup", onFindLanguageKeyUp, false); | 454 E("find-language").addEventListener("keyup", onFindLanguageKeyUp, false); |
416 E("whitelisting-textbox").addEventListener("keypress", function(e) | 455 E("whitelisting-textbox").addEventListener("keypress", function(e) |
417 { | 456 { |
418 // e.keyCode has been deprecated so we attempt to use e.key | 457 if (isEnterPressed(e)) |
419 // keyCode "13" corresponds to "Enter" | |
420 if ((e.key && e.key == "Enter") || (!e.key && e.keyCode == 13)) | |
421 addWhitelistedDomain(); | 458 addWhitelistedDomain(); |
422 }, false); | 459 }, false); |
460 | |
461 // Advanced tab | |
462 var filterTextbox = document.querySelector("#custom-filters-add input"); | |
463 placeholderValue = ext.i18n.getMessage("options_customFilters_textbox_placeh older"); | |
464 filterTextbox.setAttribute("placeholder", placeholderValue); | |
465 function addCustomFilters() | |
466 { | |
467 var filterText = filterTextbox.value; | |
468 ext.backgroundPage.sendMessage( | |
469 { | |
470 type: "filters.add", | |
471 text: filterText | |
472 }); | |
473 filterTextbox.value = ""; | |
474 } | |
475 E("custom-filters-add").addEventListener("submit", function(e) | |
476 { | |
477 e.preventDefault(); | |
478 addCustomFilters(); | |
479 }, false); | |
480 var customFilterEditButtons = document.querySelectorAll("#custom-filters-edi t-wrapper button"); | |
423 } | 481 } |
424 | 482 |
425 function openDialog(name) | 483 function openDialog(name) |
426 { | 484 { |
427 document.body.setAttribute("data-dialog", name); | 485 document.body.setAttribute("data-dialog", name); |
428 } | 486 } |
429 | 487 |
430 function populateLists() | 488 function populateLists() |
431 { | 489 { |
432 subscriptionsMap = Object.create(null); | 490 subscriptionsMap = Object.create(null); |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
492 type: "filters.add", | 550 type: "filters.add", |
493 text: "@@||" + domain.value.toLowerCase() + "^$document" | 551 text: "@@||" + domain.value.toLowerCase() + "^$document" |
494 }); | 552 }); |
495 } | 553 } |
496 | 554 |
497 domain.value = ""; | 555 domain.value = ""; |
498 } | 556 } |
499 | 557 |
500 function editCustomFilters() | 558 function editCustomFilters() |
501 { | 559 { |
502 //TODO: NYI | 560 var customFilterItems = collections.customFilters.items; |
561 var filterTexts = []; | |
562 for (var i = 0; i < customFilterItems.length; i++) | |
563 filterTexts.push(customFilterItems[i].text); | |
564 E("custom-filters-raw").value = filterTexts.join("\n"); | |
503 } | 565 } |
504 | 566 |
505 function getAcceptableAdsURL(callback) | 567 function getAcceptableAdsURL(callback) |
506 { | 568 { |
507 ext.backgroundPage.sendMessage( | 569 ext.backgroundPage.sendMessage( |
508 { | 570 { |
509 type: "prefs.get", | 571 type: "prefs.get", |
510 key: "subscriptions_exceptionsurl" | 572 key: "subscriptions_exceptionsurl" |
511 }, | 573 }, |
512 function(value) | 574 function(value) |
(...skipping 20 matching lines...) Expand all Loading... | |
533 url: url | 595 url: url |
534 }; | 596 }; |
535 if (title) | 597 if (title) |
536 message.title = title; | 598 message.title = title; |
537 if (homepage) | 599 if (homepage) |
538 message.homepage = homepage; | 600 message.homepage = homepage; |
539 | 601 |
540 ext.backgroundPage.sendMessage(message); | 602 ext.backgroundPage.sendMessage(message); |
541 } | 603 } |
542 | 604 |
543 function removeSubscription(url) | |
544 { | |
545 ext.backgroundPage.sendMessage( | |
546 { | |
547 type: "subscriptions.remove", | |
548 url: url | |
549 }); | |
550 } | |
551 | |
552 function removeFilter(filter) | |
553 { | |
554 ext.backgroundPage.sendMessage( | |
555 { | |
556 type: "filters.remove", | |
557 text: filter | |
558 }); | |
559 } | |
560 | |
561 function onFilterMessage(action, filter) | 605 function onFilterMessage(action, filter) |
562 { | 606 { |
563 switch (action) | 607 switch (action) |
564 { | 608 { |
565 case "added": | 609 case "added": |
566 updateFilter(filter); | 610 updateFilter(filter); |
567 updateShareLink(); | 611 updateShareLink(); |
568 break; | 612 break; |
569 case "loaded": | 613 case "loaded": |
570 populateLists(); | 614 populateLists(); |
571 break; | 615 break; |
572 case "removed": | 616 case "removed": |
573 var knownFilter = filtersMap[filter.text]; | 617 var knownFilter = filtersMap[filter.text]; |
574 collections.whitelist.removeItem(knownFilter); | 618 collections.whitelist.removeItem(knownFilter); |
619 collections.customFilters.removeItem(knownFilter); | |
575 delete filtersMap[filter.text]; | 620 delete filtersMap[filter.text]; |
576 updateShareLink(); | 621 updateShareLink(); |
577 break; | 622 break; |
578 } | 623 } |
579 } | 624 } |
580 | 625 |
581 function onSubscriptionMessage(action, subscription) | 626 function onSubscriptionMessage(action, subscription) |
582 { | 627 { |
583 switch (action) | 628 switch (action) |
584 { | 629 { |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
655 { | 700 { |
656 case "app.listen": | 701 case "app.listen": |
657 if (message.action == "addSubscription") | 702 if (message.action == "addSubscription") |
658 { | 703 { |
659 var subscription = message.args[0]; | 704 var subscription = message.args[0]; |
660 var dialog = E("dialog-content-predefined"); | 705 var dialog = E("dialog-content-predefined"); |
661 dialog.querySelector("h3").textContent = subscription.title || ""; | 706 dialog.querySelector("h3").textContent = subscription.title || ""; |
662 dialog.querySelector(".url").textContent = subscription.url; | 707 dialog.querySelector(".url").textContent = subscription.url; |
663 openDialog("predefined"); | 708 openDialog("predefined"); |
664 } | 709 } |
710 else if (message.action == "error") | |
711 { | |
712 alert(message.args.join("\n")); | |
713 } | |
665 break; | 714 break; |
666 case "filters.listen": | 715 case "filters.listen": |
667 onFilterMessage(message.action, message.args[0]); | 716 onFilterMessage(message.action, message.args[0]); |
668 break; | 717 break; |
669 case "subscriptions.listen": | 718 case "subscriptions.listen": |
670 onSubscriptionMessage(message.action, message.args[0]); | 719 onSubscriptionMessage(message.action, message.args[0]); |
671 break; | 720 break; |
672 } | 721 } |
673 }); | 722 }); |
674 | 723 |
675 ext.backgroundPage.sendMessage( | 724 ext.backgroundPage.sendMessage( |
676 { | 725 { |
677 type: "app.listen", | 726 type: "app.listen", |
678 filter: ["addSubscription"] | 727 filter: ["addSubscription", "error"] |
679 }); | 728 }); |
680 ext.backgroundPage.sendMessage( | 729 ext.backgroundPage.sendMessage( |
681 { | 730 { |
682 type: "filters.listen", | 731 type: "filters.listen", |
683 filter: ["added", "loaded", "removed"] | 732 filter: ["added", "loaded", "removed"] |
684 }); | 733 }); |
685 ext.backgroundPage.sendMessage( | 734 ext.backgroundPage.sendMessage( |
686 { | 735 { |
687 type: "subscriptions.listen", | 736 type: "subscriptions.listen", |
688 filter: ["added", "disabled", "homepage", "removed", "title"] | 737 filter: ["added", "disabled", "homepage", "removed", "title"] |
689 }); | 738 }); |
690 | 739 |
691 window.addEventListener("DOMContentLoaded", onDOMLoaded, false); | 740 window.addEventListener("DOMContentLoaded", onDOMLoaded, false); |
692 })(); | 741 })(); |
LEFT | RIGHT |