Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

options.js 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. // Listeners for Reset and Save buttons.
  104. document.querySelectorAll("button").forEach(button => {
  105. button.addActivateEvent((event) => {
  106. event.target.blur();
  107. if (button.classList.contains("save-button"))
  108. saveChanges();
  109. else
  110. resetChanges();
  111. });
  112. });
  113. // Listeners to activate Reset/Save buttons when textareas get input.
  114. document.querySelectorAll("textarea").forEach(textarea => {
  115. textarea.addEventListener("input", (event) => {
  116. setButtonsActive(true);
  117. });
  118. });
  119. // Listeners to auto-expand textareas on input.
  120. document.querySelectorAll("textarea").forEach(textarea => {
  121. textarea.addEventListener("input", (event) => {
  122. expandTextarea(textarea);
  123. });
  124. });
  125. // Listeners for mode select switch.
  126. document.querySelector("input[type='checkbox']").addEventListener("change", (event) => {
  127. modeSelectorInputReceived();
  128. });
  129. document.querySelectorAll(".mode-select-container span").forEach(span => {
  130. span.addActivateEvent((event) => {
  131. modeSelectorInputReceived();
  132. });
  133. });
  134. }
  135. initialize();