| 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-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 isCustomFiltersLoaded = false; |
| 29 let {getMessage} = ext.i18n; | 29 let {getMessage} = ext.i18n; |
| 30 let customFiltersArray = []; | 30 let customFilters = []; |
| 31 let filterErrors = new Map([ | 31 let filterErrors = new Map([ |
| 32 ["synchronize_invalid_url", | 32 ["synchronize_invalid_url", |
| 33 "options_filterList_lastDownload_invalidURL"], | 33 "options_filterList_lastDownload_invalidURL"], |
| 34 ["synchronize_connection_error", | 34 ["synchronize_connection_error", |
| 35 "options_filterList_lastDownload_connectionError"], | 35 "options_filterList_lastDownload_connectionError"], |
| 36 ["synchronize_invalid_data", | 36 ["synchronize_invalid_data", |
| 37 "options_filterList_lastDownload_invalidData"], | 37 "options_filterList_lastDownload_invalidData"], |
| 38 ["synchronize_checksum_mismatch", | 38 ["synchronize_checksum_mismatch", |
| 39 "options_filterList_lastDownload_checksumMismatch"] | 39 "options_filterList_lastDownload_checksumMismatch"] |
| 40 ]); | 40 ]); |
| (...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 241 let error = filterErrors.get(item.downloadStatus); | 241 let error = filterErrors.get(item.downloadStatus); |
| 242 if (error) | 242 if (error) |
| 243 message.textContent = getMessage(error); | 243 message.textContent = getMessage(error); |
| 244 else | 244 else |
| 245 message.textContent = item.downloadStatus; | 245 message.textContent = item.downloadStatus; |
| 246 element.classList.add("show-message"); | 246 element.classList.add("show-message"); |
| 247 } | 247 } |
| 248 else if (item.lastDownload > 0) | 248 else if (item.lastDownload > 0) |
| 249 { | 249 { |
| 250 let lastUpdate = item.lastDownload * 1000; | 250 let lastUpdate = item.lastDownload * 1000; |
| 251 let lastUpdateLive = function() | 251 let sinceUpdate = Date.now() - lastUpdate; |
| 252 if (sinceUpdate > fullDayInMs) |
| 252 { | 253 { |
| 253 let sinceUpdate = Date.now() - lastUpdate; | 254 let lastUpdateDate = new Date(item.lastDownload * 1000); |
| 254 if (sinceUpdate > fullDayInMs) | 255 let monthName = lastUpdateDate.toLocaleString(undefined, |
| 255 { | 256 {month: "short"}); |
| 256 let lastUpdateDate = new Date(item.lastDownload * 1000); | 257 let day = lastUpdateDate.getDate(); |
| 257 let monthName = lastUpdateDate.toLocaleString(undefined, | 258 day = day < 10 ? "0" + day : day; |
| 258 {month: "short"}); | 259 lastUpdateElement.textContent = day + " " + monthName + " " + |
| 259 let day = lastUpdateDate.getDate(); | 260 lastUpdateDate.getFullYear(); |
| 260 day = day < 10 ? "0" + day : day; | 261 } |
| 261 lastUpdateElement.textContent = day + " " + monthName + " " + | 262 else if (sinceUpdate > hourInMs) |
| 262 lastUpdateDate.getFullYear(); | 263 { |
| 263 } | 264 lastUpdateElement.textContent = |
| 264 else if (sinceUpdate > hourInMs) | 265 getMessage("options_filterList_hours"); |
| 265 { | 266 } |
| 266 let placeholder = [Math.round(sinceUpdate / hourInMs)]; | 267 else if (sinceUpdate > minuteInMs) |
| 267 lastUpdateElement.textContent = | 268 { |
| 268 getMessage("options_filterList_hours", placeholder); | 269 lastUpdateElement.textContent = |
| 269 } | 270 getMessage("options_filterList_minutes"); |
| 270 else if (sinceUpdate > minuteInMs) | 271 } |
| 271 { | 272 else |
| 272 let placeholder = [Math.round(sinceUpdate / minuteInMs)]; | 273 { |
| 273 lastUpdateElement.textContent = | 274 lastUpdateElement.textContent = |
| 274 getMessage("options_filterList_minutes", placeholder); | 275 getMessage("options_filterList_now"); |
| 275 } | 276 } |
| 276 else | |
| 277 { | |
| 278 lastUpdateElement.textContent = | |
| 279 getMessage("options_filterList_now"); | |
| 280 } | |
| 281 }; | |
| 282 lastUpdateLive(); | |
| 283 element.classList.remove("show-message"); | 277 element.classList.remove("show-message"); |
| 284 } | 278 } |
| 285 } | 279 } |
| 286 | 280 |
| 287 let websiteElement = element.querySelector(".context-menu .website"); | 281 let websiteElement = element.querySelector(".context-menu .website"); |
| 288 if (websiteElement) | 282 if (websiteElement) |
| 289 { | 283 { |
| 290 if (item.homepage) | 284 if (item.homepage) |
| 291 websiteElement.setAttribute("href", item.homepage); | 285 websiteElement.setAttribute("href", item.homepage); |
| 292 else | 286 else |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 432 function updateFilter(filter) | 426 function updateFilter(filter) |
| 433 { | 427 { |
| 434 let match = filter.text.match(whitelistedDomainRegexp); | 428 let match = filter.text.match(whitelistedDomainRegexp); |
| 435 if (match && !filtersMap[filter.text]) | 429 if (match && !filtersMap[filter.text]) |
| 436 { | 430 { |
| 437 filter.title = match[1]; | 431 filter.title = match[1]; |
| 438 collections.whitelist.addItem(filter); | 432 collections.whitelist.addItem(filter); |
| 439 } | 433 } |
| 440 else | 434 else |
| 441 { | 435 { |
| 442 customFiltersArray.push(filter.text); | 436 customFilters.push(filter.text); |
| 443 if (isCustomFiltersLoaded) | 437 if (isCustomFiltersLoaded) |
| 444 updateCustomFiltersUi(); | 438 updateCustomFiltersUi(); |
| 445 } | 439 } |
| 446 | 440 |
| 447 filtersMap[filter.text] = filter; | 441 filtersMap[filter.text] = filter; |
| 448 } | 442 } |
| 449 | 443 |
| 450 function removeCustomFilter(text) | 444 function removeCustomFilter(text) |
| 451 { | 445 { |
| 452 let index = customFiltersArray.indexOf(text); | 446 let index = customFilters.indexOf(text); |
| 453 if (index >= 0) | 447 if (index >= 0) |
| 454 customFiltersArray.splice(index, 1); | 448 customFilters.splice(index, 1); |
| 455 | 449 |
| 456 updateCustomFiltersUi(); | 450 updateCustomFiltersUi(); |
| 457 } | 451 } |
| 458 | 452 |
| 459 function updateCustomFiltersUi() | 453 function updateCustomFiltersUi() |
| 460 { | 454 { |
| 461 let customFiltersListElement = E("custom-filters-raw"); | 455 let customFiltersListElement = E("custom-filters-raw"); |
| 462 customFiltersListElement.value = ""; | 456 customFiltersListElement.value = customFilters.join("\n"); |
| 463 customFiltersListElement.value = customFiltersArray.join("\n"); | |
| 464 } | 457 } |
| 465 | 458 |
| 466 function loadRecommendations() | 459 function loadRecommendations() |
| 467 { | 460 { |
| 468 fetch("subscriptions.xml") | 461 fetch("subscriptions.xml") |
| 469 .then((response) => | 462 .then((response) => |
| 470 { | 463 { |
| 471 return response.text(); | 464 return response.text(); |
| 472 }) | 465 }) |
| 473 .then((text) => | 466 .then((text) => |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 540 else | 533 else |
| 541 location.href = link; | 534 location.href = link; |
| 542 }); | 535 }); |
| 543 } | 536 } |
| 544 | 537 |
| 545 function switchTab(id) | 538 function switchTab(id) |
| 546 { | 539 { |
| 547 location.hash = id; | 540 location.hash = id; |
| 548 } | 541 } |
| 549 | 542 |
| 550 function execAction(action, element, event) | 543 function execAction(action, element) |
| 551 { | 544 { |
| 552 switch (action) | 545 switch (action) |
| 553 { | 546 { |
| 554 case "add-domain-exception": | 547 case "add-domain-exception": |
| 555 addWhitelistedDomain(); | 548 addWhitelistedDomain(); |
| 556 break; | 549 break; |
| 557 case "add-language-subscription": | 550 case "add-language-subscription": |
| 558 addEnableSubscription(findParentData(element, "access", false)); | 551 addEnableSubscription(findParentData(element, "access", false)); |
| 559 break; | 552 break; |
| 560 case "add-predefined-subscription": { | 553 case "add-predefined-subscription": { |
| 561 let dialog = E("dialog-content-predefined"); | 554 let dialog = E("dialog-content-predefined"); |
| 562 let title = dialog.querySelector("h3").textContent; | 555 let title = dialog.querySelector("h3").textContent; |
| 563 let url = dialog.querySelector(".url").textContent; | 556 let url = dialog.querySelector(".url").textContent; |
| 564 addEnableSubscription(url, title); | 557 addEnableSubscription(url, title); |
| 565 closeDialog(); | 558 closeDialog(); |
| 566 break; | 559 break; |
| 567 } | 560 } |
| 568 case "cancel-custom-filters": | 561 case "cancel-custom-filters": |
| 569 updateCustomFiltersUi(); | |
| 570 setCustomFiltersView("read"); | 562 setCustomFiltersView("read"); |
| 571 break; | 563 break; |
| 572 case "close-dialog": | 564 case "close-dialog": |
| 573 closeDialog(); | 565 closeDialog(); |
| 574 break; | 566 break; |
| 575 case "edit-custom-filters": | 567 case "edit-custom-filters": |
| 576 setCustomFiltersView("write"); | 568 setCustomFiltersView("write"); |
| 577 break; | 569 break; |
| 578 case "import-subscription": { | 570 case "import-subscription": { |
| 579 let url = E("blockingList-textbox").value; | 571 let url = E("blockingList-textbox").value; |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 658 ext.backgroundPage.sendMessage({ | 650 ext.backgroundPage.sendMessage({ |
| 659 type: "subscriptions.update", | 651 type: "subscriptions.update", |
| 660 url: findParentData(element, "access", false) | 652 url: findParentData(element, "access", false) |
| 661 }); | 653 }); |
| 662 break; | 654 break; |
| 663 } | 655 } |
| 664 } | 656 } |
| 665 | 657 |
| 666 function setCustomFiltersView(mode) | 658 function setCustomFiltersView(mode) |
| 667 { | 659 { |
| 668 let customFilters = E("custom-filters"); | 660 let customFiltersElement = E("custom-filters-raw"); |
| 669 let customFiltersListElement = E("custom-filters-raw"); | 661 updateCustomFiltersUi(); |
| 670 if (mode == "read") | 662 if (mode == "read") |
| 671 { | 663 { |
| 672 customFiltersListElement.disabled = true; | 664 customFiltersElement.disabled = true; |
| 673 if (!customFiltersListElement.value) | 665 if (!customFiltersElement.value) |
| 674 { | 666 { |
| 675 setCustomFiltersView("empty"); | 667 setCustomFiltersView("empty"); |
| 676 return; | 668 return; |
| 677 } | 669 } |
| 678 } | 670 } |
| 679 else if (mode == "write") | 671 else if (mode == "write") |
| 680 { | 672 { |
| 681 updateCustomFiltersUi(); | 673 customFiltersElement.disabled = false; |
| 682 customFiltersListElement.disabled = false; | 674 } |
| 683 } | 675 |
| 684 | 676 E("custom-filters").dataset.mode = mode; |
| 685 customFilters.dataset.mode = mode; | |
| 686 } | 677 } |
| 687 | 678 |
| 688 function onClick(e) | 679 function onClick(e) |
| 689 { | 680 { |
| 690 let context = document.querySelector(".show-context-menu"); | 681 let context = document.querySelector(".show-context-menu"); |
| 691 if (context) | 682 if (context) |
| 692 context.classList.remove("show-context-menu"); | 683 context.classList.remove("show-context-menu"); |
| 693 | 684 |
| 694 let actions = findParentData(e.target, "action", false); | 685 let actions = findParentData(e.target, "action", false); |
| 695 if (!actions) | 686 if (!actions) |
| 696 return; | 687 return; |
| 697 | 688 |
| 698 actions = actions.split(","); | 689 actions = actions.split(","); |
| 699 for (let action of actions) | 690 for (let action of actions) |
| 700 { | 691 { |
| 701 execAction(action, e.target, e); | 692 execAction(action, e.target); |
| 702 } | 693 } |
| 703 } | 694 } |
| 704 | 695 |
| 705 function getKey(e) | 696 function getKey(e) |
| 706 { | 697 { |
| 707 // e.keyCode has been deprecated so we attempt to use e.key | 698 // e.keyCode has been deprecated so we attempt to use e.key |
| 708 if ("key" in e) | 699 if ("key" in e) |
| 709 return e.key; | 700 return e.key; |
| 710 return getKey.keys[e.keyCode]; | 701 return getKey.keys[e.keyCode]; |
| 711 } | 702 } |
| (...skipping 26 matching lines...) Expand all Loading... |
| 738 { | 729 { |
| 739 if (key == "ArrowLeft" || key == "ArrowUp") | 730 if (key == "ArrowLeft" || key == "ArrowUp") |
| 740 element = element.previousElementSibling || container.lastElementChild; | 731 element = element.previousElementSibling || container.lastElementChild; |
| 741 else if (key == "ArrowRight" || key == "ArrowDown") | 732 else if (key == "ArrowRight" || key == "ArrowDown") |
| 742 element = element.nextElementSibling || container.firstElementChild; | 733 element = element.nextElementSibling || container.firstElementChild; |
| 743 } | 734 } |
| 744 | 735 |
| 745 let actions = container.getAttribute("data-action").split(","); | 736 let actions = container.getAttribute("data-action").split(","); |
| 746 for (let action of actions) | 737 for (let action of actions) |
| 747 { | 738 { |
| 748 execAction(action, element, e); | 739 execAction(action, element); |
| 749 } | 740 } |
| 750 } | 741 } |
| 751 | 742 |
| 752 function selectTabItem(tabId, container, focus) | 743 function selectTabItem(tabId, container, focus) |
| 753 { | 744 { |
| 754 // Show tab content | 745 // Show tab content |
| 755 document.body.setAttribute("data-tab", tabId); | 746 document.body.setAttribute("data-tab", tabId); |
| 756 | 747 |
| 757 // Select tab | 748 // Select tab |
| 758 let tabList = container.querySelector("[role='tablist']"); | 749 let tabList = container.querySelector("[role='tablist']"); |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 825 | 816 |
| 826 updateShareLink(); | 817 updateShareLink(); |
| 827 updateTooltips(); | 818 updateTooltips(); |
| 828 | 819 |
| 829 // Initialize interactive UI elements | 820 // Initialize interactive UI elements |
| 830 document.body.addEventListener("click", onClick, false); | 821 document.body.addEventListener("click", onClick, false); |
| 831 document.body.addEventListener("keyup", onKeyUp, false); | 822 document.body.addEventListener("keyup", onKeyUp, false); |
| 832 let placeholderValue = getMessage("options_dialog_language_find"); | 823 let placeholderValue = getMessage("options_dialog_language_find"); |
| 833 E("find-language").setAttribute("placeholder", placeholderValue); | 824 E("find-language").setAttribute("placeholder", placeholderValue); |
| 834 E("find-language").addEventListener("keyup", onFindLanguageKeyUp, false); | 825 E("find-language").addEventListener("keyup", onFindLanguageKeyUp, false); |
| 835 let exampleValue = getMessage("options_whitelist_placeholder_example", | 826 let exampleValue = getMessage("options_whitelist_placeholder_example", |
| 836 ["www.example.com"]); | 827 ["www.example.com"]); |
| 837 E("whitelisting-textbox").setAttribute("placeholder", exampleValue); | 828 E("whitelisting-textbox").setAttribute("placeholder", exampleValue); |
| 838 E("whitelisting-textbox").addEventListener("keyup", (e) => | 829 E("whitelisting-textbox").addEventListener("keyup", (e) => |
| 839 { | 830 { |
| 840 E("whitelisting-add-button").disabled = !e.target.value; | 831 E("whitelisting-add-button").disabled = !e.target.value; |
| 841 }, false); | 832 }, false); |
| 842 | 833 |
| 843 // Advanced tab | 834 // Advanced tab |
| 844 let customize = document.querySelectorAll("#customize li[data-pref]"); | 835 let customize = document.querySelectorAll("#customize li[data-pref]"); |
| 845 customize = Array.prototype.map.call(customize, (checkbox) => | 836 customize = Array.prototype.map.call(customize, (checkbox) => |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 999 ext.backgroundPage.sendMessage({ | 990 ext.backgroundPage.sendMessage({ |
| 1000 type: "filters.get", | 991 type: "filters.get", |
| 1001 subscriptionUrl: subscription.url | 992 subscriptionUrl: subscription.url |
| 1002 }, | 993 }, |
| 1003 (filters) => | 994 (filters) => |
| 1004 { | 995 { |
| 1005 for (let filter of filters) | 996 for (let filter of filters) |
| 1006 updateFilter(filter); | 997 updateFilter(filter); |
| 1007 | 998 |
| 1008 isCustomFiltersLoaded = true; | 999 isCustomFiltersLoaded = true; |
| 1009 updateCustomFiltersUi(); | |
| 1010 setCustomFiltersView("read"); | 1000 setCustomFiltersView("read"); |
| 1011 }); | 1001 }); |
| 1012 } | 1002 } |
| 1013 }); | 1003 }); |
| 1014 loadRecommendations(); | 1004 loadRecommendations(); |
| 1015 ext.backgroundPage.sendMessage({ | 1005 ext.backgroundPage.sendMessage({ |
| 1016 type: "prefs.get", | 1006 type: "prefs.get", |
| 1017 key: "subscriptions_exceptionsurl" | 1007 key: "subscriptions_exceptionsurl" |
| 1018 }, | 1008 }, |
| 1019 (url) => | 1009 (url) => |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1053 if (domain.value) | 1043 if (domain.value) |
| 1054 { | 1044 { |
| 1055 sendMessageHandleErrors({ | 1045 sendMessageHandleErrors({ |
| 1056 type: "filters.add", | 1046 type: "filters.add", |
| 1057 text: "@@||" + domain.value.toLowerCase() + "^$document" | 1047 text: "@@||" + domain.value.toLowerCase() + "^$document" |
| 1058 }); | 1048 }); |
| 1059 } | 1049 } |
| 1060 | 1050 |
| 1061 domain.value = ""; | 1051 domain.value = ""; |
| 1062 E("whitelisting-add-button").disabled = true; | 1052 E("whitelisting-add-button").disabled = true; |
| 1063 } | |
| 1064 | |
| 1065 function editCustomFilters() | |
| 1066 { | |
| 1067 if (!isCustomFiltersLoaded) | |
| 1068 { | |
| 1069 console.error("Custom filters are not loaded"); | |
| 1070 return; | |
| 1071 } | |
| 1072 | |
| 1073 E("custom-filters").classList.add("mode-edit"); | |
| 1074 let filterTexts = []; | |
| 1075 for (let customFilterItem of collections.customFilters.items) | |
| 1076 filterTexts.push(customFilterItem.text); | |
| 1077 E("custom-filters-raw").value = filterTexts.join("\n"); | |
| 1078 } | 1053 } |
| 1079 | 1054 |
| 1080 function addEnableSubscription(url, title, homepage) | 1055 function addEnableSubscription(url, title, homepage) |
| 1081 { | 1056 { |
| 1082 let messageType = null; | 1057 let messageType = null; |
| 1083 let knownSubscription = subscriptionsMap[url]; | 1058 let knownSubscription = subscriptionsMap[url]; |
| 1084 if (knownSubscription && knownSubscription.disabled == true) | 1059 if (knownSubscription && knownSubscription.disabled == true) |
| 1085 messageType = "subscriptions.toggle"; | 1060 messageType = "subscriptions.toggle"; |
| 1086 else | 1061 else |
| 1087 messageType = "subscriptions.add"; | 1062 messageType = "subscriptions.add"; |
| (...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1380 }); | 1355 }); |
| 1381 ext.backgroundPage.sendMessage({ | 1356 ext.backgroundPage.sendMessage({ |
| 1382 type: "subscriptions.listen", | 1357 type: "subscriptions.listen", |
| 1383 filter: ["added", "disabled", "homepage", "lastDownload", "removed", | 1358 filter: ["added", "disabled", "homepage", "lastDownload", "removed", |
| 1384 "title", "downloadStatus", "downloading"] | 1359 "title", "downloadStatus", "downloading"] |
| 1385 }); | 1360 }); |
| 1386 | 1361 |
| 1387 window.addEventListener("DOMContentLoaded", onDOMLoaded, false); | 1362 window.addEventListener("DOMContentLoaded", onDOMLoaded, false); |
| 1388 window.addEventListener("hashchange", onHashChange, false); | 1363 window.addEventListener("hashchange", onHashChange, false); |
| 1389 } | 1364 } |
| LEFT | RIGHT |