| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- /*******************/
- /* EVENT LISTENERS */
- /*******************/
-
-
- /***********/
- /* HELPERS */
- /***********/
-
- /* Toggle the current state, as represented in the ASK object. (Nothing actually
- happens until the UI state is updated, killSticky() is called (if needed),
- and the new settings are saved in storage.)
-
- What “toggle the current state” actually means depends on the current mode.
-
- [1] In blacklist mode, toggleState() does one of the following:
-
- (a) (if stickies are not being killed on the current page) adds a matching
- pattern (if one is not already there), and removes all applicable
- exclusion patterns; or,
- (b) (if stickies are being killed on the current page) removes all
- applicable matching patterns.
-
- [2] In whitelist mode, toggleState() does one of the following:
-
- (a) (if stickies are not being killed on the current page) removes all
- applicable exclusion patterns; or,
- (b) (if stickies are being killed on the current page) adds an exclusion
- pattern.
- */
- function toggleState() {
- if (ASK.mode == "blacklist") {
- if (!ASK.pageMatched || ASK.pageExcluded) {
- /* In this case, stickies are NOT being killed. We must add a matching
- pattern, and remove all applicable exclusion patterns. */
- if (!ASK.pageMatched) {
- ASK.matchingPatterns.push(regExpForCurrentTab());
- }
- if (ASK.pageExcluded) {
- ASK.exclusionPatterns = ASK.exclusionPatterns.filter(pattern =>
- !ASK.activeTabLocation.match(new RegExp(pattern))
- );
- }
- } else {
- /* In this case, stickies ARE being killed. We must remove all
- applicable matching patterns. */
- ASK.matchingPatterns = ASK.matchingPatterns.filter(pattern =>
- !ASK.activeTabLocation.match(new RegExp(pattern))
- );
- }
- } else { // if whitelist mode
- // TODO: code this!
- }
- recalculatePatternEffects();
- }
-
- function regExpForCurrentTab() {
- let a = document.createElement("A");
- a.href = ASK.activeTabLocation;
- return ".*" + a.host.replace(/\./g, "\\.") + ".*";
- }
-
- function recalculatePatternEffects() {
- ASK.pageMatched = false;
- ASK.pageExcluded = false;
- for (let pattern of ASK.matchingPatterns) {
- if (ASK.activeTabLocation.match(new RegExp(pattern))) {
- ASK.pageMatched = true;
- break;
- }
- }
- for (let pattern of ASK.exclusionPatterns) {
- if (ASK.activeTabLocation.match(new RegExp(pattern))) {
- ASK.pageExcluded = true;
- break;
- }
- }
- }
-
- function updateState(result) {
- ASK.mode = result.mode || "blacklist";
- ASK.matchingPatterns = (ASK.mode == "whitelist") ?
- [ ".*" ] :
- result.matchingPatterns.split("\n");
- ASK.exclusionPatterns = (typeof result.exclusionPatterns != "undefined" &&
- result.exclusionPatterns != "") ?
- result.exclusionPatterns.split("\n") :
- [ ];
- recalculatePatternEffects();
- }
-
- function updateUIState() {
- let button = document.querySelector("#main-button-container button");
- if (ASK.mode == "blacklist" && ASK.pageMatched && !ASK.pageExcluded) {
- button.classList.toggle("active", true);
- // button.innerHTML = "X";
- } else if (ASK.mode == "blacklist" && (!ASK.pageMatched || ASK.pageExcluded)) {
- button.classList.toggle("active", false);
- // button.innerHTML = "O";
- } else if (ASK.mode == "whitelist" && !ASK.pageExcluded) {
- button.classList.toggle("active", false);
- // button.innerHTML = "X";
- } else if (ASK.mode == "whitelist" && ASK.pageExcluded) {
- button.classList.toggle("active", true);
- // button.innerHTML = "O";
- }
- }
-
- /******************/
- /* INITIALIZATION */
- /******************/
-
- function initialize() {
- window.ASK = { };
-
- chrome.tabs.query({currentWindow: true, active: true}, (tabs) => {
- ASK.activeTabLocation = tabs[0].url;
- chrome.storage.sync.get([ "matchingPatterns", "exclusionPatterns", "mode" ], (result) => {
- updateState(result);
- updateUIState();
- });
- });
-
- document.querySelector("#main-button-container button").addActivateEvent((event) => {
- toggleState();
-
- let matchingPatterns = ASK.matchingPatterns.join("\n");
- let exclusionPatterns = ASK.exclusionPatterns.join("\n");
- let mode = ASK.mode;
- chrome.storage.sync.set({
- "matchingPatterns": matchingPatterns,
- "exclusionPatterns": exclusionPatterns,
- "mode": mode
- }, () => {
- updateUIState();
- if (ASK.pageMatched && !ASK.pageExcluded)
- chrome.tabs.executeScript(null, { code: 'killSticky()' });
- });
- });
-
-
- document.querySelector("#aux-button-container button").addActivateEvent((event) => {
- chrome.runtime.openOptionsPage(null);
- });
- }
-
- initialize();
|