| /* HELPERS */ | /* HELPERS */ | ||||
| /***********/ | /***********/ | ||||
| /* Toggle the current state, as represented in the ASK object. (Nothing actually | |||||
| /* Toggle the current state, as represented in the AKS object. (Nothing actually | |||||
| happens until the UI state is updated, killSticky() is called (if needed), | happens until the UI state is updated, killSticky() is called (if needed), | ||||
| and the new settings are saved in storage.) | and the new settings are saved in storage.) | ||||
| pattern. | pattern. | ||||
| */ | */ | ||||
| function toggleState() { | function toggleState() { | ||||
| if (ASK.mode == "blacklist") { | |||||
| if (!ASK.pageMatched || ASK.pageExcluded) { | |||||
| if (AKS.mode == "blacklist") { | |||||
| if (!AKS.pageMatched || AKS.pageExcluded) { | |||||
| /* In this case, stickies are NOT being killed. We must add a matching | /* In this case, stickies are NOT being killed. We must add a matching | ||||
| pattern, and remove all applicable exclusion patterns. */ | pattern, and remove all applicable exclusion patterns. */ | ||||
| if (!ASK.pageMatched) { | |||||
| addPatternForCurrentTab(ASK.matchingPatterns); | |||||
| if (!AKS.pageMatched) { | |||||
| addPatternForCurrentTab(AKS.matchingPatterns); | |||||
| } | } | ||||
| if (ASK.pageExcluded) { | |||||
| disablePatternsForCurrentTab(ASK.exclusionPatterns); | |||||
| if (AKS.pageExcluded) { | |||||
| disablePatternsForCurrentTab(AKS.exclusionPatterns); | |||||
| } | } | ||||
| } else { | } else { | ||||
| /* In this case, stickies ARE being killed. We must remove all | /* In this case, stickies ARE being killed. We must remove all | ||||
| applicable matching patterns. */ | applicable matching patterns. */ | ||||
| disablePatternsForCurrentTab(ASK.matchingPatterns); | |||||
| disablePatternsForCurrentTab(AKS.matchingPatterns); | |||||
| } | } | ||||
| } else { // if whitelist mode | } else { // if whitelist mode | ||||
| if (ASK.pageExcluded) { | |||||
| if (AKS.pageExcluded) { | |||||
| /* In this case, stickies are NOT being killed. We must remove all | /* In this case, stickies are NOT being killed. We must remove all | ||||
| applicable exclusion patterns. */ | applicable exclusion patterns. */ | ||||
| disablePatternsForCurrentTab(ASK.exclusionPatterns); | |||||
| disablePatternsForCurrentTab(AKS.exclusionPatterns); | |||||
| } else { | } else { | ||||
| /* In this case, stickies ARE being killed. We must add an exclusion | /* In this case, stickies ARE being killed. We must add an exclusion | ||||
| pattern. */ | pattern. */ | ||||
| addPatternForCurrentTab(ASK.exclusionPatterns); | |||||
| addPatternForCurrentTab(AKS.exclusionPatterns); | |||||
| } | } | ||||
| } | } | ||||
| recalculatePatternEffects(); | recalculatePatternEffects(); | ||||
| // Strip the leading comment characters and whitespace. | // Strip the leading comment characters and whitespace. | ||||
| let uncommentedPattern = patterns[i].replace(/^#[#\s]*/, ""); | let uncommentedPattern = patterns[i].replace(/^#[#\s]*/, ""); | ||||
| // Check if the pattern matches the active tab’s URL. | // Check if the pattern matches the active tab’s URL. | ||||
| if (ASK.activeTabLocation.match(new RegExp(uncommentedPattern))) { | |||||
| if (AKS.activeTabLocation.match(new RegExp(uncommentedPattern))) { | |||||
| // If so, replace the commented pattern. | // If so, replace the commented pattern. | ||||
| patterns[i] = uncommentedPattern; | patterns[i] = uncommentedPattern; | ||||
| existingPatternsFound = true; | existingPatternsFound = true; | ||||
| got to add a new pattern. */ | got to add a new pattern. */ | ||||
| let dtf = new Intl.DateTimeFormat([], | let dtf = new Intl.DateTimeFormat([], | ||||
| { month: 'short', day: 'numeric', year: 'numeric', hour: 'numeric', minute: 'numeric', second: 'numeric' }); | { month: 'short', day: 'numeric', year: 'numeric', hour: 'numeric', minute: 'numeric', second: 'numeric' }); | ||||
| patterns.push("## " + dtf.format(new Date()) + " - " + ASK.activeTabLocation); | |||||
| patterns.push("## " + dtf.format(new Date()) + " - " + AKS.activeTabLocation); | |||||
| patterns.push(regExpForCurrentTab()); | patterns.push(regExpForCurrentTab()); | ||||
| patterns.push(""); | patterns.push(""); | ||||
| } | } | ||||
| function disablePatternsForCurrentTab(patterns) { | function disablePatternsForCurrentTab(patterns) { | ||||
| for (var i = 0; i < patterns.length; i++) { | for (var i = 0; i < patterns.length; i++) { | ||||
| if (patterns[i] && ASK.activeTabLocation.match(new RegExp(patterns[i]))) | |||||
| if (patterns[i] && AKS.activeTabLocation.match(new RegExp(patterns[i]))) | |||||
| patterns[i] = "# " + patterns[i]; | patterns[i] = "# " + patterns[i]; | ||||
| } | } | ||||
| } | } | ||||
| function regExpForCurrentTab() { | function regExpForCurrentTab() { | ||||
| let a = document.createElement("A"); | let a = document.createElement("A"); | ||||
| a.href = ASK.activeTabLocation; | |||||
| a.href = AKS.activeTabLocation; | |||||
| return ".*" + a.host.replace(/\./g, "\\.") + ".*"; | return ".*" + a.host.replace(/\./g, "\\.") + ".*"; | ||||
| } | } | ||||
| function recalculatePatternEffects() { | function recalculatePatternEffects() { | ||||
| ASK.pageMatched = false; | |||||
| ASK.pageExcluded = false; | |||||
| for (let pattern of ASK.matchingPatterns) { | |||||
| AKS.pageMatched = false; | |||||
| AKS.pageExcluded = false; | |||||
| for (let pattern of AKS.matchingPatterns) { | |||||
| if (pattern && | if (pattern && | ||||
| !pattern.hasPrefix("#") && | !pattern.hasPrefix("#") && | ||||
| ASK.activeTabLocation.match(new RegExp(pattern))) { | |||||
| ASK.pageMatched = true; | |||||
| AKS.activeTabLocation.match(new RegExp(pattern))) { | |||||
| AKS.pageMatched = true; | |||||
| break; | break; | ||||
| } | } | ||||
| } | } | ||||
| for (let pattern of ASK.exclusionPatterns) { | |||||
| for (let pattern of AKS.exclusionPatterns) { | |||||
| if (pattern && | if (pattern && | ||||
| !pattern.hasPrefix("#") && | !pattern.hasPrefix("#") && | ||||
| ASK.activeTabLocation.match(new RegExp(pattern))) { | |||||
| ASK.pageExcluded = true; | |||||
| AKS.activeTabLocation.match(new RegExp(pattern))) { | |||||
| AKS.pageExcluded = true; | |||||
| break; | break; | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| function updateState(result) { | function updateState(result) { | ||||
| ASK.mode = result.mode || "blacklist"; | |||||
| ASK.matchingPatterns = (ASK.mode == "whitelist") ? | |||||
| AKS.mode = result.mode || "blacklist"; | |||||
| AKS.matchingPatterns = (AKS.mode == "whitelist") ? | |||||
| [ ".*" ] : | [ ".*" ] : | ||||
| (typeof result.matchingPatterns != "undefined" ? | (typeof result.matchingPatterns != "undefined" ? | ||||
| result.matchingPatterns.split("\n") : | result.matchingPatterns.split("\n") : | ||||
| [ ]); | [ ]); | ||||
| ASK.exclusionPatterns = typeof result.exclusionPatterns != "undefined" ? | |||||
| AKS.exclusionPatterns = typeof result.exclusionPatterns != "undefined" ? | |||||
| result.exclusionPatterns.split("\n") : | result.exclusionPatterns.split("\n") : | ||||
| [ ]; | [ ]; | ||||
| recalculatePatternEffects(); | recalculatePatternEffects(); | ||||
| let info = document.querySelector(".info"); | let info = document.querySelector(".info"); | ||||
| let statusLabel = document.querySelector(".info .status-display"); | let statusLabel = document.querySelector(".info .status-display"); | ||||
| var active, whitelist; | var active, whitelist; | ||||
| if (ASK.mode == "blacklist") { | |||||
| if (ASK.pageMatched && !ASK.pageExcluded) { | |||||
| if (AKS.mode == "blacklist") { | |||||
| if (AKS.pageMatched && !AKS.pageExcluded) { | |||||
| active = true; | active = true; | ||||
| } else { | } else { | ||||
| active = false; | active = false; | ||||
| } | } | ||||
| } else { | } else { | ||||
| if (!ASK.pageExcluded) { | |||||
| if (!AKS.pageExcluded) { | |||||
| active = false; | active = false; | ||||
| } else { | } else { | ||||
| active = true; | active = true; | ||||
| mainButton.classList.toggle("active", active); | mainButton.classList.toggle("active", active); | ||||
| statusLabel.classList.toggle("active", active); | statusLabel.classList.toggle("active", active); | ||||
| mainButton.classList.toggle("whitelist", (ASK.mode == "whitelist")); | |||||
| info.classList.toggle("whitelist", (ASK.mode == "whitelist")); | |||||
| mainButton.classList.toggle("whitelist", (AKS.mode == "whitelist")); | |||||
| info.classList.toggle("whitelist", (AKS.mode == "whitelist")); | |||||
| } | } | ||||
| /******************/ | /******************/ | ||||
| /******************/ | /******************/ | ||||
| function initialize() { | function initialize() { | ||||
| window.ASK = { }; | |||||
| window.AKS = { }; | |||||
| // Update version. | // Update version. | ||||
| document.querySelector(".info-header .version").innerHTML = chrome.runtime.getManifest().version; | document.querySelector(".info-header .version").innerHTML = chrome.runtime.getManifest().version; | ||||
| // Retrieve saved settings. | // Retrieve saved settings. | ||||
| chrome.tabs.query({currentWindow: true, active: true}, (tabs) => { | chrome.tabs.query({currentWindow: true, active: true}, (tabs) => { | ||||
| ASK.activeTabLocation = tabs[0].url; | |||||
| AKS.activeTabLocation = tabs[0].url; | |||||
| chrome.storage.sync.get([ "matchingPatterns", "exclusionPatterns", "mode" ], (result) => { | chrome.storage.sync.get([ "matchingPatterns", "exclusionPatterns", "mode" ], (result) => { | ||||
| updateState(result); | updateState(result); | ||||
| updateUIState(); | updateUIState(); | ||||
| toggleState(); | toggleState(); | ||||
| var changes = { | var changes = { | ||||
| "exclusionPatterns": ASK.exclusionPatterns.join("\n"), | |||||
| "mode": ASK.mode | |||||
| "exclusionPatterns": AKS.exclusionPatterns.join("\n"), | |||||
| "mode": AKS.mode | |||||
| }; | }; | ||||
| if (ASK.mode == "blacklist") | |||||
| changes.matchingPatterns = ASK.matchingPatterns.join("\n"); | |||||
| if (AKS.mode == "blacklist") | |||||
| changes.matchingPatterns = AKS.matchingPatterns.join("\n"); | |||||
| chrome.storage.sync.set(changes, () => { | chrome.storage.sync.set(changes, () => { | ||||
| updateUIState(); | updateUIState(); | ||||
| let reloadButton = document.querySelector("button.reload-button"); | let reloadButton = document.querySelector("button.reload-button"); | ||||
| if (ASK.pageMatched && !ASK.pageExcluded) { | |||||
| if (AKS.pageMatched && !AKS.pageExcluded) { | |||||
| chrome.tabs.executeScript(null, { code: 'killSticky()' }); | chrome.tabs.executeScript(null, { code: 'killSticky()' }); | ||||
| reloadButton.classList.toggle("active", false); | reloadButton.classList.toggle("active", false); | ||||
| } else { | } else { | ||||
| // Listener for reload button (appears when sticky-killing is toggled OFF). | // Listener for reload button (appears when sticky-killing is toggled OFF). | ||||
| document.querySelector("button.reload-button").addActivateEvent(() => { | document.querySelector("button.reload-button").addActivateEvent(() => { | ||||
| chrome.tabs.update(null, { url: ASK.activeTabLocation }); | |||||
| chrome.tabs.update(null, { url: AKS.activeTabLocation }); | |||||
| window.close(); | window.close(); | ||||
| }); | }); | ||||
| } | } |