/***********/ /* 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) { addPatternForCurrentTab(ASK.matchingPatterns); } if (ASK.pageExcluded) { disablePatternsForCurrentTab(ASK.exclusionPatterns); } } else { /* In this case, stickies ARE being killed. We must remove all applicable matching patterns. */ disablePatternsForCurrentTab(ASK.matchingPatterns); } } else { // if whitelist mode if (ASK.pageExcluded) { /* In this case, stickies are NOT being killed. We must remove all applicable exclusion patterns. */ disablePatternsForCurrentTab(ASK.exclusionPatterns); } else { /* In this case, stickies ARE being killed. We must add an exclusion pattern. */ addPatternForCurrentTab(ASK.exclusionPatterns); } } recalculatePatternEffects(); } function addPatternForCurrentTab(patterns) { /* First, we see if there are already any existing but commented-out patterns that match the active tab. If so, we enable them all. */ var existingPatternsFound = false; for (var i = 0; i < patterns.length; i++) { if (patterns[i] && patterns[i].hasPrefix("#")) { // Strip the leading comment characters and whitespace. let uncommentedPattern = patterns[i].replace(/^#[#\s]*/, ""); // Check if the pattern matches the active tab’s URL. if (ASK.activeTabLocation.match(new RegExp(uncommentedPattern))) { // If so, replace the commented pattern. patterns[i] = uncommentedPattern; existingPatternsFound = true; } } } if (existingPatternsFound) return; /* If no existing but commented-out patterns have been found, then we’ve got to add a new pattern. */ let dtf = new Intl.DateTimeFormat([], { month: 'short', day: 'numeric', year: 'numeric', hour: 'numeric', minute: 'numeric', second: 'numeric' }); patterns.push("## " + dtf.format(new Date()) + " - " + ASK.activeTabLocation); patterns.push(regExpForCurrentTab()); patterns.push(""); } function disablePatternsForCurrentTab(patterns) { for (var i = 0; i < patterns.length; i++) { if (patterns[i] && ASK.activeTabLocation.match(new RegExp(patterns[i]))) patterns[i] = "# " + patterns[i]; } } 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 mainButton = document.querySelector("button.main-button"); let info = document.querySelector(".info"); let statusLabel = document.querySelector(".info .status-display"); var active, whitelist; if (ASK.mode == "blacklist") { if (ASK.pageMatched && !ASK.pageExcluded) { active = true; } else { active = false; } } else { if (!ASK.pageExcluded) { active = false; } else { active = true; } } mainButton.classList.toggle("active", active); statusLabel.classList.toggle("active", active); mainButton.classList.toggle("whitelist", (ASK.mode == "whitelist")); info.classList.toggle("whitelist", (ASK.mode == "whitelist")); } /******************/ /* INITIALIZATION */ /******************/ function initialize() { window.ASK = { }; // Update version. document.querySelector(".info-header .version").innerHTML = chrome.runtime.getManifest().version; // 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();