You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

popup.js 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /***********/
  2. /* HELPERS */
  3. /***********/
  4. /* Toggle the current state, as represented in the ASK object. (Nothing actually
  5. happens until the UI state is updated, killSticky() is called (if needed),
  6. and the new settings are saved in storage.)
  7. What “toggle the current state” actually means depends on the current mode.
  8. [1] In blacklist mode, toggleState() does one of the following:
  9. (a) (if stickies are not being killed on the current page) adds a matching
  10. pattern (if one is not already there), and removes all applicable
  11. exclusion patterns; or,
  12. (b) (if stickies are being killed on the current page) removes all
  13. applicable matching patterns.
  14. [2] In whitelist mode, toggleState() does one of the following:
  15. (a) (if stickies are not being killed on the current page) removes all
  16. applicable exclusion patterns; or,
  17. (b) (if stickies are being killed on the current page) adds an exclusion
  18. pattern.
  19. */
  20. function toggleState() {
  21. if (ASK.mode == "blacklist") {
  22. if (!ASK.pageMatched || ASK.pageExcluded) {
  23. /* In this case, stickies are NOT being killed. We must add a matching
  24. pattern, and remove all applicable exclusion patterns. */
  25. if (!ASK.pageMatched) {
  26. ASK.matchingPatterns.push(regExpForCurrentTab());
  27. }
  28. if (ASK.pageExcluded) {
  29. ASK.exclusionPatterns = ASK.exclusionPatterns.filter(pattern =>
  30. !ASK.activeTabLocation.match(new RegExp(pattern))
  31. );
  32. }
  33. } else {
  34. /* In this case, stickies ARE being killed. We must remove all
  35. applicable matching patterns. */
  36. ASK.matchingPatterns = ASK.matchingPatterns.filter(pattern =>
  37. !ASK.activeTabLocation.match(new RegExp(pattern))
  38. );
  39. }
  40. } else { // if whitelist mode
  41. if (ASK.pageExcluded) {
  42. /* In this case, stickies are NOT being killed. We must remove all
  43. applicable exclusion patterns. */
  44. ASK.exclusionPatterns = ASK.exclusionPatterns.filter(pattern =>
  45. !ASK.activeTabLocation.match(new RegExp(pattern))
  46. );
  47. } else {
  48. /* In this case, stickies ARE being killed. We must add an exclusion
  49. pattern. */
  50. ASK.exclusionPatterns.push(regExpForCurrentTab());
  51. }
  52. }
  53. recalculatePatternEffects();
  54. }
  55. function regExpForCurrentTab() {
  56. let a = document.createElement("A");
  57. a.href = ASK.activeTabLocation;
  58. return ".*" + a.host.replace(/\./g, "\\.") + ".*";
  59. }
  60. function recalculatePatternEffects() {
  61. ASK.pageMatched = false;
  62. ASK.pageExcluded = false;
  63. for (let pattern of ASK.matchingPatterns) {
  64. if (pattern &&
  65. !pattern.hasPrefix("#") &&
  66. ASK.activeTabLocation.match(new RegExp(pattern))) {
  67. ASK.pageMatched = true;
  68. break;
  69. }
  70. }
  71. for (let pattern of ASK.exclusionPatterns) {
  72. if (pattern &&
  73. !pattern.hasPrefix("#") &&
  74. ASK.activeTabLocation.match(new RegExp(pattern))) {
  75. ASK.pageExcluded = true;
  76. break;
  77. }
  78. }
  79. }
  80. function updateState(result) {
  81. ASK.mode = result.mode || "blacklist";
  82. ASK.matchingPatterns = (ASK.mode == "whitelist") ?
  83. [ ".*" ] :
  84. (typeof result.matchingPatterns != "undefined" ?
  85. result.matchingPatterns.split("\n") :
  86. [ ]);
  87. ASK.exclusionPatterns = typeof result.exclusionPatterns != "undefined" ?
  88. result.exclusionPatterns.split("\n") :
  89. [ ];
  90. recalculatePatternEffects();
  91. }
  92. function updateUIState() {
  93. let button = document.querySelector("button.main-button");
  94. let modeDisplay = document.querySelector(".mode-display");
  95. if (ASK.mode == "blacklist") {
  96. if (ASK.pageMatched && !ASK.pageExcluded) {
  97. button.classList.toggle("active", true);
  98. } else {
  99. button.classList.toggle("active", false);
  100. }
  101. button.classList.toggle("whitelist", false);
  102. modeDisplay.classList.toggle("whitelist", false);
  103. } else {
  104. if (!ASK.pageExcluded) {
  105. button.classList.toggle("active", false);
  106. } else {
  107. button.classList.toggle("active", true);
  108. }
  109. button.classList.toggle("whitelist", true);
  110. modeDisplay.classList.toggle("whitelist", true);
  111. }
  112. }
  113. /******************/
  114. /* INITIALIZATION */
  115. /******************/
  116. function initialize() {
  117. window.ASK = { };
  118. // Retrieve saved settings.
  119. chrome.tabs.query({currentWindow: true, active: true}, (tabs) => {
  120. ASK.activeTabLocation = tabs[0].url;
  121. chrome.storage.sync.get([ "matchingPatterns", "exclusionPatterns", "mode" ], (result) => {
  122. updateState(result);
  123. updateUIState();
  124. });
  125. });
  126. // Listener for main button.
  127. document.querySelector("button.main-button").addActivateEvent((event) => {
  128. toggleState();
  129. var changes = {
  130. "exclusionPatterns": ASK.exclusionPatterns.join("\n"),
  131. "mode": ASK.mode
  132. };
  133. if (ASK.mode == "blacklist")
  134. changes.matchingPatterns = ASK.matchingPatterns.join("\n");
  135. chrome.storage.sync.set(changes, () => {
  136. updateUIState();
  137. let reloadButton = document.querySelector("button.reload-button");
  138. if (ASK.pageMatched && !ASK.pageExcluded) {
  139. chrome.tabs.executeScript(null, { code: 'killSticky()' });
  140. reloadButton.classList.toggle("active", false);
  141. } else {
  142. reloadButton.classList.toggle("active", true);
  143. }
  144. });
  145. });
  146. // Listener for Options button.
  147. document.querySelector("button.options-button").addActivateEvent(() => {
  148. chrome.runtime.openOptionsPage(null);
  149. });
  150. // Listener for reload button (appears when sticky-killing is toggled OFF).
  151. document.querySelector("button.reload-button").addActivateEvent(() => {
  152. chrome.tabs.update(null, { url: ASK.activeTabLocation });
  153. window.close();
  154. });
  155. }
  156. initialize();