您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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