/***********/ /* 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 if (ASK.pageExcluded) { /* In this case, stickies are NOT being killed. We must remove all applicable exclusion patterns. */ ASK.exclusionPatterns = ASK.exclusionPatterns.filter(pattern => !ASK.activeTabLocation.match(new RegExp(pattern)) ); } else { /* In this case, stickies ARE being killed. We must add an exclusion pattern. */ ASK.exclusionPatterns.push(regExpForCurrentTab()); } } 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 (pattern && !pattern.hasPrefix("#") && ASK.activeTabLocation.match(new RegExp(pattern))) { ASK.pageMatched = true; break; } } for (let pattern of ASK.exclusionPatterns) { if (pattern && !pattern.hasPrefix("#") && ASK.activeTabLocation.match(new RegExp(pattern))) { ASK.pageExcluded = true; break; } } } function updateState(result) { ASK.mode = result.mode || "blacklist"; ASK.matchingPatterns = (ASK.mode == "whitelist") ? [ ".*" ] : (typeof result.matchingPatterns != "undefined" ? result.matchingPatterns.split("\n") : [ ]); ASK.exclusionPatterns = typeof result.exclusionPatterns != "undefined" ? result.exclusionPatterns.split("\n") : [ ]; recalculatePatternEffects(); } function updateUIState() { let button = document.querySelector("button.main-button"); let modeDisplay = document.querySelector(".mode-display"); if (ASK.mode == "blacklist") { if (ASK.pageMatched && !ASK.pageExcluded) { button.classList.toggle("active", true); } else { button.classList.toggle("active", false); } button.classList.toggle("whitelist", false); modeDisplay.classList.toggle("whitelist", false); } else { if (!ASK.pageExcluded) { button.classList.toggle("active", false); } else { button.classList.toggle("active", true); } button.classList.toggle("whitelist", true); modeDisplay.classList.toggle("whitelist", true); } } /******************/ /* INITIALIZATION */ /******************/ function initialize() { window.ASK = { }; // Retrieve saved settings. chrome.tabs.query({currentWindow: true, active: true}, (tabs) => { ASK.activeTabLocation = tabs[0].url; chrome.storage.sync.get([ "matchingPatterns", "exclusionPatterns", "mode" ], (result) => { updateState(result); updateUIState(); }); }); // Listener for main button. document.querySelector("button.main-button").addActivateEvent((event) => { toggleState(); var changes = { "exclusionPatterns": ASK.exclusionPatterns.join("\n"), "mode": ASK.mode }; if (ASK.mode == "blacklist") changes.matchingPatterns = ASK.matchingPatterns.join("\n"); chrome.storage.sync.set(changes, () => { updateUIState(); let reloadButton = document.querySelector("button.reload-button"); if (ASK.pageMatched && !ASK.pageExcluded) { chrome.tabs.executeScript(null, { code: 'killSticky()' }); reloadButton.classList.toggle("active", false); } else { reloadButton.classList.toggle("active", true); } }); }); // Listener for Options button. document.querySelector("button.options-button").addActivateEvent(() => { chrome.runtime.openOptionsPage(null); }); // Listener for reload button (appears when sticky-killing is toggled OFF). document.querySelector("button.reload-button").addActivateEvent(() => { chrome.tabs.update(null, { url: ASK.activeTabLocation }); window.close(); }); } initialize();