Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /***********/
  2. /* HELPERS */
  3. /***********/
  4. /* Toggle the current state, as represented in the AKS 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 (AKS.mode == "blacklist") {
  22. if (!AKS.pageMatched || AKS.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 (!AKS.pageMatched) {
  26. addPatternForCurrentTab(AKS.matchingPatterns);
  27. }
  28. if (AKS.pageExcluded) {
  29. disablePatternsForCurrentTab(AKS.exclusionPatterns);
  30. }
  31. } else {
  32. /* In this case, stickies ARE being killed. We must remove all
  33. applicable matching patterns. */
  34. disablePatternsForCurrentTab(AKS.matchingPatterns);
  35. }
  36. } else { // if whitelist mode
  37. if (AKS.pageExcluded) {
  38. /* In this case, stickies are NOT being killed. We must remove all
  39. applicable exclusion patterns. */
  40. disablePatternsForCurrentTab(AKS.exclusionPatterns);
  41. } else {
  42. /* In this case, stickies ARE being killed. We must add an exclusion
  43. pattern. */
  44. addPatternForCurrentTab(AKS.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 (AKS.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()) + " - " + AKS.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] && AKS.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 = AKS.activeTabLocation;
  83. return ".*" + a.host.replace(/\./g, "\\.") + ".*";
  84. }
  85. function recalculatePatternEffects() {
  86. AKS.pageMatched = false;
  87. AKS.pageExcluded = false;
  88. for (let pattern of AKS.matchingPatterns) {
  89. if (pattern &&
  90. !pattern.hasPrefix("#") &&
  91. AKS.activeTabLocation.match(new RegExp(pattern))) {
  92. AKS.pageMatched = true;
  93. break;
  94. }
  95. }
  96. for (let pattern of AKS.exclusionPatterns) {
  97. if (pattern &&
  98. !pattern.hasPrefix("#") &&
  99. AKS.activeTabLocation.match(new RegExp(pattern))) {
  100. AKS.pageExcluded = true;
  101. break;
  102. }
  103. }
  104. }
  105. function updateState(result) {
  106. AKS.mode = result.mode || "blacklist";
  107. AKS.matchingPatterns = (AKS.mode == "whitelist") ?
  108. [ ".*" ] :
  109. (typeof result.matchingPatterns != "undefined" ?
  110. result.matchingPatterns.split("\n") :
  111. [ ]);
  112. AKS.exclusionPatterns = typeof result.exclusionPatterns != "undefined" ?
  113. result.exclusionPatterns.split("\n") :
  114. [ ];
  115. recalculatePatternEffects();
  116. }
  117. function updateUIState() {
  118. let mainButton = document.querySelector("button.main-button");
  119. let info = document.querySelector(".info");
  120. let statusLabel = document.querySelector(".info .status-display");
  121. var active, whitelist;
  122. if (AKS.mode == "blacklist") {
  123. if (AKS.pageMatched && !AKS.pageExcluded) {
  124. active = true;
  125. } else {
  126. active = false;
  127. }
  128. } else {
  129. if (!AKS.pageExcluded) {
  130. active = false;
  131. } else {
  132. active = true;
  133. }
  134. }
  135. mainButton.classList.toggle("active", active);
  136. statusLabel.classList.toggle("active", active);
  137. mainButton.classList.toggle("whitelist", (AKS.mode == "whitelist"));
  138. info.classList.toggle("whitelist", (AKS.mode == "whitelist"));
  139. }
  140. /******************/
  141. /* INITIALIZATION */
  142. /******************/
  143. function initialize() {
  144. window.AKS = { };
  145. // Update version.
  146. document.querySelector(".info-header .version").innerHTML = chrome.runtime.getManifest().version;
  147. // Retrieve saved settings.
  148. chrome.tabs.query({currentWindow: true, active: true}, (tabs) => {
  149. AKS.activeTabLocation = tabs[0].url;
  150. chrome.storage.sync.get([ "matchingPatterns", "exclusionPatterns", "mode" ], (result) => {
  151. updateState(result);
  152. updateUIState();
  153. });
  154. });
  155. // Listener for main button.
  156. document.querySelector("button.main-button").addActivateEvent((event) => {
  157. toggleState();
  158. var changes = {
  159. "exclusionPatterns": AKS.exclusionPatterns.join("\n"),
  160. "mode": AKS.mode
  161. };
  162. if (AKS.mode == "blacklist")
  163. changes.matchingPatterns = AKS.matchingPatterns.join("\n");
  164. chrome.storage.sync.set(changes, () => {
  165. updateUIState();
  166. let reloadButton = document.querySelector("button.reload-button");
  167. if (AKS.pageMatched && !AKS.pageExcluded) {
  168. chrome.tabs.executeScript(null, { code: 'killSticky()' });
  169. reloadButton.classList.toggle("active", false);
  170. } else {
  171. reloadButton.classList.toggle("active", true);
  172. }
  173. });
  174. });
  175. // Listener for Options button.
  176. document.querySelector("button.options-button").addActivateEvent(() => {
  177. chrome.runtime.openOptionsPage(null);
  178. });
  179. // Listener for the Help button.
  180. document.querySelector("button.help-button").addActivateEvent(() => {
  181. // TODO: code!
  182. });
  183. // Listener for reload button (appears when sticky-killing is toggled OFF).
  184. document.querySelector("button.reload-button").addActivateEvent(() => {
  185. chrome.tabs.update(null, { url: AKS.activeTabLocation });
  186. window.close();
  187. });
  188. }
  189. initialize();