Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

popup.js 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. addPatternForCurrentTab(ASK.matchingPatterns);
  27. }
  28. if (ASK.pageExcluded) {
  29. disablePatternsForCurrentTab(ASK.exclusionPatterns);
  30. }
  31. } else {
  32. /* In this case, stickies ARE being killed. We must remove all
  33. applicable matching patterns. */
  34. disablePatternsForCurrentTab(ASK.matchingPatterns);
  35. }
  36. } else { // if whitelist mode
  37. if (ASK.pageExcluded) {
  38. /* In this case, stickies are NOT being killed. We must remove all
  39. applicable exclusion patterns. */
  40. disablePatternsForCurrentTab(ASK.exclusionPatterns);
  41. } else {
  42. /* In this case, stickies ARE being killed. We must add an exclusion
  43. pattern. */
  44. addPatternForCurrentTab(ASK.exclusionPatterns);
  45. }
  46. }
  47. recalculatePatternEffects();
  48. }
  49. function addPatternForCurrentTab(patterns) {
  50. /* First, we see if there are already any existing but commented-out
  51. patterns that match the active tab. If so, we enable them all. */
  52. var existingPatternsFound = false;
  53. for (var i = 0; i < patterns.length; i++) {
  54. if (patterns[i] && patterns[i].hasPrefix("#")) {
  55. // Strip the leading comment characters and whitespace.
  56. let uncommentedPattern = patterns[i].replace(/^#[#\s]*/, "");
  57. // Check if the pattern matches the active tab’s URL.
  58. if (ASK.activeTabLocation.match(new RegExp(uncommentedPattern))) {
  59. // If so, replace the commented pattern.
  60. patterns[i] = uncommentedPattern;
  61. existingPatternsFound = true;
  62. }
  63. }
  64. }
  65. if (existingPatternsFound) return;
  66. /* If no existing but commented-out patterns have been found, then we’ve
  67. got to add a new pattern. */
  68. let dtf = new Intl.DateTimeFormat([],
  69. { month: 'short', day: 'numeric', year: 'numeric', hour: 'numeric', minute: 'numeric', second: 'numeric' });
  70. patterns.push("## " + dtf.format(new Date()) + " - " + ASK.activeTabLocation);
  71. patterns.push(regExpForCurrentTab());
  72. patterns.push("");
  73. }
  74. function disablePatternsForCurrentTab(patterns) {
  75. for (var i = 0; i < patterns.length; i++) {
  76. if (patterns[i] && ASK.activeTabLocation.match(new RegExp(patterns[i])))
  77. patterns[i] = "# " + patterns[i];
  78. }
  79. }
  80. function regExpForCurrentTab() {
  81. let a = document.createElement("A");
  82. a.href = ASK.activeTabLocation;
  83. return ".*" + a.host.replace(/\./g, "\\.") + ".*";
  84. }
  85. function recalculatePatternEffects() {
  86. ASK.pageMatched = false;
  87. ASK.pageExcluded = false;
  88. for (let pattern of ASK.matchingPatterns) {
  89. if (pattern &&
  90. !pattern.hasPrefix("#") &&
  91. ASK.activeTabLocation.match(new RegExp(pattern))) {
  92. ASK.pageMatched = true;
  93. break;
  94. }
  95. }
  96. for (let pattern of ASK.exclusionPatterns) {
  97. if (pattern &&
  98. !pattern.hasPrefix("#") &&
  99. ASK.activeTabLocation.match(new RegExp(pattern))) {
  100. ASK.pageExcluded = true;
  101. break;
  102. }
  103. }
  104. }
  105. function updateState(result) {
  106. ASK.mode = result.mode || "blacklist";
  107. ASK.matchingPatterns = (ASK.mode == "whitelist") ?
  108. [ ".*" ] :
  109. (typeof result.matchingPatterns != "undefined" ?
  110. result.matchingPatterns.split("\n") :
  111. [ ]);
  112. ASK.exclusionPatterns = typeof result.exclusionPatterns != "undefined" ?
  113. result.exclusionPatterns.split("\n") :
  114. [ ];
  115. recalculatePatternEffects();
  116. }
  117. function updateUIState() {
  118. let button = document.querySelector("button.main-button");
  119. let modeDisplay = document.querySelector(".mode-display");
  120. if (ASK.mode == "blacklist") {
  121. if (ASK.pageMatched && !ASK.pageExcluded) {
  122. button.classList.toggle("active", true);
  123. } else {
  124. button.classList.toggle("active", false);
  125. }
  126. button.classList.toggle("whitelist", false);
  127. modeDisplay.classList.toggle("whitelist", false);
  128. } else {
  129. if (!ASK.pageExcluded) {
  130. button.classList.toggle("active", false);
  131. } else {
  132. button.classList.toggle("active", true);
  133. }
  134. button.classList.toggle("whitelist", true);
  135. modeDisplay.classList.toggle("whitelist", true);
  136. }
  137. }
  138. /******************/
  139. /* INITIALIZATION */
  140. /******************/
  141. function initialize() {
  142. window.ASK = { };
  143. // Retrieve saved settings.
  144. chrome.tabs.query({currentWindow: true, active: true}, (tabs) => {
  145. ASK.activeTabLocation = tabs[0].url;
  146. chrome.storage.sync.get([ "matchingPatterns", "exclusionPatterns", "mode" ], (result) => {
  147. updateState(result);
  148. updateUIState();
  149. });
  150. });
  151. // Listener for main button.
  152. document.querySelector("button.main-button").addActivateEvent((event) => {
  153. toggleState();
  154. var changes = {
  155. "exclusionPatterns": ASK.exclusionPatterns.join("\n"),
  156. "mode": ASK.mode
  157. };
  158. if (ASK.mode == "blacklist")
  159. changes.matchingPatterns = ASK.matchingPatterns.join("\n");
  160. chrome.storage.sync.set(changes, () => {
  161. updateUIState();
  162. let reloadButton = document.querySelector("button.reload-button");
  163. if (ASK.pageMatched && !ASK.pageExcluded) {
  164. chrome.tabs.executeScript(null, { code: 'killSticky()' });
  165. reloadButton.classList.toggle("active", false);
  166. } else {
  167. reloadButton.classList.toggle("active", true);
  168. }
  169. });
  170. });
  171. // Listener for Options button.
  172. document.querySelector("button.options-button").addActivateEvent(() => {
  173. chrome.runtime.openOptionsPage(null);
  174. });
  175. // Listener for reload button (appears when sticky-killing is toggled OFF).
  176. document.querySelector("button.reload-button").addActivateEvent(() => {
  177. chrome.tabs.update(null, { url: ASK.activeTabLocation });
  178. window.close();
  179. });
  180. }
  181. initialize();