Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /**********/
  2. /* EVENTS */
  3. /**********/
  4. /* Triggered when a click is received by the mode selector switch, or by
  5. either of the two labels (“Blacklist” and “Whitelist”).
  6. */
  7. function modeSelectorInputReceived() {
  8. toggleModeSelectorState();
  9. // Activate the “Reset” and “Save” buttons, since changes have been made.
  10. setButtonsActive(true);
  11. }
  12. /***********/
  13. /* HELPERS */
  14. /***********/
  15. /* Toggles all UI state associated with mode selection. Called on input to
  16. the mode selector (see modeSelectorInputReceived()), OR when changes are
  17. reset (see resetChanges()).
  18. */
  19. function toggleModeSelectorState(newMode) {
  20. /* Update state of the checkbox itself, and the container div. */
  21. let container = document.querySelector(".mode-select-container");
  22. let checkbox = document.querySelector("input#whitelist-mode");
  23. newMode = newMode || (container.classList.contains("whitelist") ? "blacklist" : "whitelist");
  24. container.classList.toggle("whitelist", (newMode == "whitelist"));
  25. checkbox.checked = (newMode == "whitelist");
  26. /* Enable both spans (the “Blacklist” and “Whitelist” labels, then disable
  27. just the label associated with the now-enabled mode. */
  28. container.querySelectorAll("span").forEach(span => {
  29. span.classList.toggle("disabled", false);
  30. });
  31. document.querySelector(`.${newMode}-mode-label`).classList.toggle("disabled", true);
  32. /* In whitelist mode, the matching patterns textarea must be disabled,
  33. since the matching patterns list is treated as containing only the
  34. single pattern “.*”. An overlay is shown, to indicate this. */
  35. document.querySelector("div#matchingPatterns").classList.toggle("disabled", (newMode == "whitelist"));
  36. document.querySelector("div#matchingPatterns textarea").disabled = (newMode == "whitelist");
  37. }
  38. /* Saves the entered state in storage.
  39. De-activate the “Reset” and “Save” buttons.
  40. */
  41. function saveChanges() {
  42. let matchingPatterns = document.querySelector("#matchingPatterns textarea").value;
  43. let exclusionPatterns = document.querySelector("#exclusionPatterns textarea").value;
  44. let mode = document.querySelector("input#whitelist-mode").checked ? "whitelist" : "blacklist";
  45. chrome.storage.sync.set({
  46. "matchingPatterns": matchingPatterns,
  47. "exclusionPatterns": exclusionPatterns,
  48. "mode": mode
  49. }, () => { setButtonsActive(false); });
  50. }
  51. /* Retrieves the saved state from storage and reset the UI to reflect it,
  52. discarding the user’s changes.
  53. */
  54. function resetChanges(callback) {
  55. chrome.storage.sync.get([ "matchingPatterns", "exclusionPatterns", "mode" ], (result) => {
  56. AKS.matchingPatterns = result.matchingPatterns || "";
  57. AKS.exclusionPatterns = result.exclusionPatterns || ""
  58. AKS.mode = result.mode || "blacklist";
  59. document.querySelector("#matchingPatterns textarea").value = AKS.matchingPatterns;
  60. document.querySelector("#exclusionPatterns textarea").value = AKS.exclusionPatterns;
  61. toggleModeSelectorState(AKS.mode);
  62. // De-activate the “Reset” and “Save” buttons.
  63. setButtonsActive(false);
  64. // Expand all textareas.
  65. document.querySelectorAll("textarea").forEach(textarea => {
  66. expandTextarea(textarea);
  67. });
  68. // Trigger provided callback, if any.
  69. if (callback) callback();
  70. });
  71. }
  72. /* Activates or de-activates the “Reset” and “Save” buttons. Called when
  73. changes are made (to activate) or when changes are saved or reset (to
  74. de-activate).
  75. */
  76. function setButtonsActive(active) {
  77. document.querySelectorAll("button").forEach(button => {
  78. button.disabled = !active;
  79. });
  80. }
  81. /* Expands a textarea to show all its contents (up to a maximum height which is
  82. set via CSS).
  83. */
  84. function expandTextarea(textarea) {
  85. let totalBorderHeight = 2;
  86. if (textarea.clientHeight == textarea.scrollHeight + totalBorderHeight) return;
  87. requestAnimationFrame(() => {
  88. textarea.style.height = 'auto';
  89. textarea.style.height = textarea.scrollHeight + totalBorderHeight + 'px';
  90. });
  91. }
  92. /******************/
  93. /* INITIALIZATION */
  94. /******************/
  95. function initialize() {
  96. window.AKS = { };
  97. /* Retrieve saved settings from storage, and set all UI elements to their
  98. proper state. Then, focus the first active textarea. */
  99. resetChanges(() => {
  100. let divToFocus = (AKS.mode == "whitelist") ? "#exclusionPatterns" : "#matchingPatterns";
  101. document.querySelector(`${divToFocus} textarea`).focus();
  102. });
  103. // Update version.
  104. document.querySelector(".bottom-info .version").innerHTML = chrome.runtime.getManifest().version;
  105. // Listeners for Reset and Save buttons.
  106. document.querySelectorAll("button").forEach(button => {
  107. button.addActivateEvent((event) => {
  108. event.target.blur();
  109. if (button.classList.contains("save-button"))
  110. saveChanges();
  111. else
  112. resetChanges();
  113. });
  114. });
  115. // Listeners to activate Reset/Save buttons when textareas get input.
  116. document.querySelectorAll("textarea").forEach(textarea => {
  117. textarea.addEventListener("input", (event) => {
  118. setButtonsActive(true);
  119. });
  120. });
  121. // Listeners to auto-expand textareas on input.
  122. document.querySelectorAll("textarea").forEach(textarea => {
  123. textarea.addEventListener("input", (event) => {
  124. expandTextarea(textarea);
  125. });
  126. });
  127. // Listeners for mode select switch.
  128. document.querySelector("input[type='checkbox']").addEventListener("change", (event) => {
  129. modeSelectorInputReceived();
  130. });
  131. document.querySelectorAll(".mode-select-container span").forEach(span => {
  132. span.addActivateEvent((event) => {
  133. modeSelectorInputReceived();
  134. });
  135. });
  136. }
  137. initialize();