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-2017 eyeo GmbH | 3 * Copyright (C) 2006-2017 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 /* globals checkShareResource, getDocLink, i18nFormatDateTime, openSharePopup, | 18 /* globals checkShareResource, getDocLink, i18nFormatDateTime, openSharePopup, |
19 E */ | 19 E */ |
20 | 20 |
21 "use strict"; | 21 "use strict"; |
22 | 22 |
23 { | 23 { |
24 let subscriptionsMap = Object.create(null); | 24 let subscriptionsMap = Object.create(null); |
25 let filtersMap = Object.create(null); | 25 let filtersMap = Object.create(null); |
26 let collections = Object.create(null); | 26 let collections = Object.create(null); |
27 let acceptableAdsUrl = null; | 27 let acceptableAdsUrl = null; |
28 let isCustomFiltersLoaded = false; | |
28 let {getMessage} = ext.i18n; | 29 let {getMessage} = ext.i18n; |
29 let filterErrors = new Map([ | 30 let filterErrors = new Map([ |
30 ["synchronize_invalid_url", | 31 ["synchronize_invalid_url", |
31 "options_filterList_lastDownload_invalidURL"], | 32 "options_filterList_lastDownload_invalidURL"], |
32 ["synchronize_connection_error", | 33 ["synchronize_connection_error", |
33 "options_filterList_lastDownload_connectionError"], | 34 "options_filterList_lastDownload_connectionError"], |
34 ["synchronize_invalid_data", | 35 ["synchronize_invalid_data", |
35 "options_filterList_lastDownload_invalidData"], | 36 "options_filterList_lastDownload_invalidData"], |
36 ["synchronize_checksum_mismatch", | 37 ["synchronize_checksum_mismatch", |
37 "options_filterList_lastDownload_checksumMismatch"] | 38 "options_filterList_lastDownload_checksumMismatch"] |
(...skipping 428 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
466 else | 467 else |
467 location.href = link; | 468 location.href = link; |
468 }); | 469 }); |
469 } | 470 } |
470 | 471 |
471 function switchTab(id) | 472 function switchTab(id) |
472 { | 473 { |
473 location.hash = id; | 474 location.hash = id; |
474 } | 475 } |
475 | 476 |
477 function execAction(action, key, element) | |
Thomas Greiner
2017/05/05 14:33:05
`key` is only used for keyboard-initiated actions
saroyanm
2017/05/16 10:36:42
Done.
| |
478 { | |
479 switch (action) | |
480 { | |
481 case "add-domain-exception": | |
482 addWhitelistedDomain(); | |
483 break; | |
484 case "add-language-subscription": | |
485 addEnableSubscription(findParentData(element, "access", false)); | |
486 break; | |
487 case "add-predefined-subscription": { | |
488 let dialog = E("dialog-content-predefined"); | |
489 let title = dialog.querySelector("h3").textContent; | |
490 let url = dialog.querySelector(".url").textContent; | |
491 addEnableSubscription(url, title); | |
492 closeDialog(); | |
493 break; | |
494 } | |
495 case "cancel-custom-filters": | |
496 E("custom-filters").classList.remove("mode-edit"); | |
497 break; | |
498 case "cancel-domain-exception": | |
499 E("whitelisting-textbox").value = ""; | |
500 document.querySelector("#whitelisting .controls").classList | |
501 .remove("mode-edit"); | |
502 break; | |
503 case "close-dialog": | |
504 closeDialog(); | |
505 break; | |
506 case "edit-custom-filters": | |
507 editCustomFilters(); | |
508 break; | |
509 case "edit-domain-exception": | |
510 document.querySelector("#whitelisting .controls").classList | |
511 .add("mode-edit"); | |
512 E("whitelisting-textbox").focus(); | |
513 break; | |
514 case "import-subscription": { | |
515 let url = E("blockingList-textbox").value; | |
516 addEnableSubscription(url); | |
517 closeDialog(); | |
518 break; | |
519 } | |
520 case "open-context-menu": { | |
521 let listItem = findParentData(element, "access", true); | |
522 if (listItem && !listItem.classList.contains("show-context-menu")) | |
523 listItem.classList.add("show-context-menu"); | |
524 break; | |
525 } | |
526 case "open-dialog": { | |
527 let dialog = findParentData(element, "dialog", false); | |
528 openDialog(dialog); | |
529 break; | |
530 } | |
531 case "open-doclink": { | |
532 let doclink = findParentData(element, "doclink", false); | |
533 openDocLink(doclink); | |
534 break; | |
535 } | |
536 case "remove-filter": | |
537 ext.backgroundPage.sendMessage({ | |
538 type: "filters.remove", | |
539 text: findParentData(element, "access", false) | |
540 }); | |
541 break; | |
542 case "remove-subscription": | |
543 ext.backgroundPage.sendMessage({ | |
544 type: "subscriptions.remove", | |
545 url: findParentData(element, "access", false) | |
546 }); | |
547 break; | |
548 case "save-custom-filters": | |
549 sendMessageHandleErrors({ | |
550 type: "filters.importRaw", | |
551 text: E("custom-filters-raw").value, | |
552 removeExisting: true | |
553 }, | |
554 () => | |
555 { | |
556 E("custom-filters").classList.remove("mode-edit"); | |
557 }); | |
558 break; | |
559 case "switch-tab": | |
560 if (key == "Enter") | |
561 { | |
562 let tabId = findParentData(element, "tab", false); | |
563 switchTab(tabId); | |
564 } | |
565 else if (element.hasAttribute("aria-selected")) | |
566 { | |
567 let container = findParentData(element, "action", true); | |
568 if (key == "ArrowLeft" || key == "ArrowUp") | |
569 { | |
570 element = element.previousElementSibling || | |
571 container.lastElementChild; | |
572 } | |
573 else if (key == "ArrowRight" || key == "ArrowDown") | |
574 { | |
575 element = element.nextElementSibling || | |
576 container.firstElementChild; | |
577 } | |
578 let tabId = findParentData(element, "tab", false); | |
579 switchTab(tabId); | |
580 } | |
581 break; | |
582 case "toggle-disable-subscription": | |
583 ext.backgroundPage.sendMessage({ | |
584 type: "subscriptions.toggle", | |
585 keepInstalled: true, | |
586 url: findParentData(element, "access", false) | |
587 }); | |
588 break; | |
589 case "toggle-pref": | |
590 ext.backgroundPage.sendMessage({ | |
591 type: "prefs.toggle", | |
592 key: findParentData(element, "pref", false) | |
593 }); | |
594 break; | |
595 case "toggle-remove-subscription": | |
596 let subscriptionUrl = findParentData(element, "access", false); | |
597 if (element.getAttribute("aria-checked") == "true") | |
Thomas Greiner
2017/05/05 14:33:05
Did you verify that this still works? The reason w
saroyanm
2017/05/16 10:36:42
It does work, until we introduce child element to
| |
598 { | |
599 ext.backgroundPage.sendMessage({ | |
600 type: "subscriptions.remove", | |
601 url: subscriptionUrl | |
602 }); | |
603 } | |
604 else | |
605 addEnableSubscription(subscriptionUrl); | |
606 break; | |
607 case "update-all-subscriptions": | |
608 ext.backgroundPage.sendMessage({ | |
609 type: "subscriptions.update" | |
610 }); | |
611 break; | |
612 case "update-subscription": | |
613 ext.backgroundPage.sendMessage({ | |
614 type: "subscriptions.update", | |
615 url: findParentData(element, "access", false) | |
616 }); | |
617 break; | |
618 } | |
619 } | |
620 | |
476 function onClick(e) | 621 function onClick(e) |
477 { | 622 { |
478 let context = document.querySelector(".show-context-menu"); | 623 let context = document.querySelector(".show-context-menu"); |
479 if (context) | 624 if (context) |
480 context.classList.remove("show-context-menu"); | 625 context.classList.remove("show-context-menu"); |
481 | 626 |
482 let element = findParentData(e.target, "action", true); | 627 let actions = findParentData(e.target, "action", false); |
483 if (!element) | 628 if (!actions) |
484 return; | 629 return; |
485 | 630 |
486 let actions = element.getAttribute("data-action").split(","); | 631 actions = actions.split(","); |
487 for (let action of actions) | 632 for (let action of actions) |
488 { | 633 { |
489 switch (action) | 634 execAction(action, "Enter", e.target); |
490 { | |
491 case "add-domain-exception": | |
492 addWhitelistedDomain(); | |
493 break; | |
494 case "add-predefined-subscription": { | |
495 let dialog = E("dialog-content-predefined"); | |
496 let title = dialog.querySelector("h3").textContent; | |
497 let url = dialog.querySelector(".url").textContent; | |
498 addEnableSubscription(url, title); | |
499 closeDialog(); | |
500 break; | |
501 } | |
502 case "cancel-custom-filters": | |
503 E("custom-filters").classList.remove("mode-edit"); | |
504 break; | |
505 case "cancel-domain-exception": | |
506 E("whitelisting-textbox").value = ""; | |
507 document.querySelector("#whitelisting .controls").classList | |
508 .remove("mode-edit"); | |
509 break; | |
510 case "close-dialog": | |
511 closeDialog(); | |
512 break; | |
513 case "edit-custom-filters": | |
514 E("custom-filters").classList.add("mode-edit"); | |
515 editCustomFilters(); | |
516 break; | |
517 case "edit-domain-exception": | |
518 document.querySelector("#whitelisting .controls").classList | |
519 .add("mode-edit"); | |
520 E("whitelisting-textbox").focus(); | |
521 break; | |
522 case "import-subscription": { | |
523 let url = E("blockingList-textbox").value; | |
524 addEnableSubscription(url); | |
525 closeDialog(); | |
526 break; | |
527 } | |
528 case "open-dialog": { | |
529 let dialog = findParentData(element, "dialog", false); | |
530 openDialog(dialog); | |
531 break; | |
532 } | |
533 case "open-doclink": { | |
534 let doclink = findParentData(element, "doclink", false); | |
535 openDocLink(doclink); | |
536 break; | |
537 } | |
538 case "save-custom-filters": | |
539 sendMessageHandleErrors({ | |
540 type: "filters.importRaw", | |
541 text: E("custom-filters-raw").value, | |
542 removeExisting: true | |
543 }, | |
544 () => | |
545 { | |
546 E("custom-filters").classList.remove("mode-edit"); | |
547 }); | |
548 break; | |
549 case "switch-tab": { | |
550 let tabId = findParentData(e.target, "tab", false); | |
551 switchTab(tabId); | |
552 break; | |
553 } | |
554 case "toggle-pref": | |
555 ext.backgroundPage.sendMessage({ | |
556 type: "prefs.toggle", | |
557 key: findParentData(element, "pref", false) | |
558 }); | |
559 break; | |
560 case "update-all-subscriptions": | |
561 ext.backgroundPage.sendMessage({ | |
562 type: "subscriptions.update" | |
563 }); | |
564 break; | |
565 case "open-context-menu": { | |
566 let listItem = findParentData(element, "access", true); | |
567 if (listItem != context) | |
568 listItem.classList.add("show-context-menu"); | |
569 break; | |
570 } | |
571 case "update-subscription": | |
572 ext.backgroundPage.sendMessage({ | |
573 type: "subscriptions.update", | |
574 url: findParentData(element, "access", false) | |
575 }); | |
576 break; | |
577 case "remove-subscription": | |
578 ext.backgroundPage.sendMessage({ | |
579 type: "subscriptions.remove", | |
580 url: findParentData(element, "access", false) | |
581 }); | |
582 break; | |
583 case "toggle-remove-subscription": { | |
584 let subscriptionUrl = findParentData(element, "access", false); | |
585 if (element.getAttribute("aria-checked") == "true") | |
586 { | |
587 ext.backgroundPage.sendMessage({ | |
588 type: "subscriptions.remove", | |
589 url: subscriptionUrl | |
590 }); | |
591 } | |
592 else | |
593 addEnableSubscription(subscriptionUrl); | |
594 break; | |
595 } | |
596 case "toggle-disable-subscription": | |
597 ext.backgroundPage.sendMessage({ | |
598 type: "subscriptions.toggle", | |
599 keepInstalled: true, | |
600 url: findParentData(element, "access", false) | |
601 }); | |
602 break; | |
603 case "add-language-subscription": | |
604 addEnableSubscription(findParentData(element, "access", false)); | |
605 break; | |
606 case "remove-filter": | |
607 ext.backgroundPage.sendMessage({ | |
608 type: "filters.remove", | |
609 text: findParentData(element, "access", false) | |
610 }); | |
611 break; | |
612 } | |
613 } | 635 } |
614 } | 636 } |
615 | 637 |
616 function getKey(e) | 638 function getKey(e) |
617 { | 639 { |
618 // e.keyCode has been deprecated so we attempt to use e.key | 640 // e.keyCode has been deprecated so we attempt to use e.key |
619 if ("key" in e) | 641 if ("key" in e) |
620 return e.key; | 642 return e.key; |
621 return getKey.keys[e.keyCode]; | 643 return getKey.keys[e.keyCode]; |
622 } | 644 } |
(...skipping 15 matching lines...) Expand all Loading... | |
638 return; | 660 return; |
639 | 661 |
640 let container = findParentData(element, "action", true); | 662 let container = findParentData(element, "action", true); |
641 if (!container || !container.hasAttribute("data-keys")) | 663 if (!container || !container.hasAttribute("data-keys")) |
642 return; | 664 return; |
643 | 665 |
644 let keys = container.getAttribute("data-keys").split(" "); | 666 let keys = container.getAttribute("data-keys").split(" "); |
645 if (keys.indexOf(key) < 0) | 667 if (keys.indexOf(key) < 0) |
646 return; | 668 return; |
647 | 669 |
648 switch (container.getAttribute("data-action")) | 670 let actions = container.getAttribute("data-action").split(","); |
671 for (let action of actions) | |
649 { | 672 { |
650 case "add-domain-exception": | 673 execAction(action, key, element); |
651 addWhitelistedDomain(); | |
652 break; | |
653 case "open-doclink": | |
654 let doclink = findParentData(element, "doclink", false); | |
655 openDocLink(doclink); | |
656 break; | |
657 case "switch-tab": | |
658 if (key == "Enter") | |
659 { | |
660 let tabId = findParentData(element, "tab", false); | |
661 switchTab(tabId); | |
662 } | |
663 else if (element.hasAttribute("aria-selected")) | |
664 { | |
665 if (key == "ArrowLeft" || key == "ArrowUp") | |
666 { | |
667 element = element.previousElementSibling || | |
668 container.lastElementChild; | |
669 } | |
670 else if (key == "ArrowRight" || key == "ArrowDown") | |
671 { | |
672 element = element.nextElementSibling || | |
673 container.firstElementChild; | |
674 } | |
675 | |
676 let tabId = findParentData(element, "tab", false); | |
677 switchTab(tabId); | |
678 } | |
679 break; | |
680 } | 674 } |
681 } | 675 } |
682 | 676 |
683 function selectTabItem(tabId, container, focus) | 677 function selectTabItem(tabId, container, focus) |
684 { | 678 { |
685 // Show tab content | 679 // Show tab content |
686 document.body.setAttribute("data-tab", tabId); | 680 document.body.setAttribute("data-tab", tabId); |
687 | 681 |
688 // Select tab | 682 // Select tab |
689 let tabList = container.querySelector("[role='tablist']"); | 683 let tabList = container.querySelector("[role='tablist']"); |
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
938 for (let subscription of subscriptions) | 932 for (let subscription of subscriptions) |
939 { | 933 { |
940 ext.backgroundPage.sendMessage({ | 934 ext.backgroundPage.sendMessage({ |
941 type: "filters.get", | 935 type: "filters.get", |
942 subscriptionUrl: subscription.url | 936 subscriptionUrl: subscription.url |
943 }, | 937 }, |
944 (filters) => | 938 (filters) => |
945 { | 939 { |
946 for (let filter of filters) | 940 for (let filter of filters) |
947 updateFilter(filter); | 941 updateFilter(filter); |
942 | |
943 isCustomFiltersLoaded = true; | |
948 }); | 944 }); |
949 } | 945 } |
950 }); | 946 }); |
951 loadRecommendations(); | 947 loadRecommendations(); |
952 ext.backgroundPage.sendMessage({ | 948 ext.backgroundPage.sendMessage({ |
953 type: "prefs.get", | 949 type: "prefs.get", |
954 key: "subscriptions_exceptionsurl" | 950 key: "subscriptions_exceptionsurl" |
955 }, | 951 }, |
956 (url) => | 952 (url) => |
957 { | 953 { |
(...skipping 27 matching lines...) Expand all Loading... | |
985 }); | 981 }); |
986 } | 982 } |
987 | 983 |
988 domain.value = ""; | 984 domain.value = ""; |
989 document.querySelector("#whitelisting .controls") | 985 document.querySelector("#whitelisting .controls") |
990 .classList.remove("mode-edit"); | 986 .classList.remove("mode-edit"); |
991 } | 987 } |
992 | 988 |
993 function editCustomFilters() | 989 function editCustomFilters() |
994 { | 990 { |
991 if (!isCustomFiltersLoaded) | |
992 { | |
993 console.error("Custom filters are not loaded"); | |
994 return; | |
995 } | |
996 | |
997 E("custom-filters").classList.add("mode-edit"); | |
995 let filterTexts = []; | 998 let filterTexts = []; |
996 for (let customFilterItem of collections.customFilters.items) | 999 for (let customFilterItem of collections.customFilters.items) |
997 filterTexts.push(customFilterItem.text); | 1000 filterTexts.push(customFilterItem.text); |
998 E("custom-filters-raw").value = filterTexts.join("\n"); | 1001 E("custom-filters-raw").value = filterTexts.join("\n"); |
999 } | 1002 } |
1000 | 1003 |
1001 function addEnableSubscription(url, title, homepage) | 1004 function addEnableSubscription(url, title, homepage) |
1002 { | 1005 { |
1003 let messageType = null; | 1006 let messageType = null; |
1004 let knownSubscription = subscriptionsMap[url]; | 1007 let knownSubscription = subscriptionsMap[url]; |
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1297 }); | 1300 }); |
1298 ext.backgroundPage.sendMessage({ | 1301 ext.backgroundPage.sendMessage({ |
1299 type: "subscriptions.listen", | 1302 type: "subscriptions.listen", |
1300 filter: ["added", "disabled", "homepage", "lastDownload", "removed", | 1303 filter: ["added", "disabled", "homepage", "lastDownload", "removed", |
1301 "title", "downloadStatus", "downloading"] | 1304 "title", "downloadStatus", "downloading"] |
1302 }); | 1305 }); |
1303 | 1306 |
1304 window.addEventListener("DOMContentLoaded", onDOMLoaded, false); | 1307 window.addEventListener("DOMContentLoaded", onDOMLoaded, false); |
1305 window.addEventListener("hashchange", onHashChange, false); | 1308 window.addEventListener("hashchange", onHashChange, false); |
1306 } | 1309 } |
OLD | NEW |