| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- /***********/
- /* HELPERS */
- /***********/
-
- /* Toggle the current state, as represented in the AKS 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 (AKS.mode == "blacklist") {
- if (!AKS.pageMatched || AKS.pageExcluded) {
- /* In this case, stickies are NOT being killed. We must add a matching
- pattern, and remove all applicable exclusion patterns. */
- if (!AKS.pageMatched) {
- addPatternForCurrentTab(AKS.matchingPatterns);
- }
- if (AKS.pageExcluded) {
- disablePatternsForCurrentTab(AKS.exclusionPatterns);
- }
- } else {
- /* In this case, stickies ARE being killed. We must remove all
- applicable matching patterns. */
- disablePatternsForCurrentTab(AKS.matchingPatterns);
- }
- } else { // if whitelist mode
- if (AKS.pageExcluded) {
- /* In this case, stickies are NOT being killed. We must remove all
- applicable exclusion patterns. */
- disablePatternsForCurrentTab(AKS.exclusionPatterns);
- } else {
- /* In this case, stickies ARE being killed. We must add an exclusion
- pattern. */
- addPatternForCurrentTab(AKS.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 (AKS.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()) + " - " + AKS.activeTabLocation);
- patterns.push(regExpForCurrentTab());
- patterns.push("");
- }
-
- function disablePatternsForCurrentTab(patterns) {
- for (var i = 0; i < patterns.length; i++) {
- if (patterns[i] && AKS.activeTabLocation.match(new RegExp(patterns[i])))
- patterns[i] = "# " + patterns[i];
- }
- }
-
- function regExpForCurrentTab() {
- let a = document.createElement("A");
- a.href = AKS.activeTabLocation;
- return ".*" + a.host.replace(/\./g, "\\.") + ".*";
- }
-
- function recalculatePatternEffects() {
- AKS.pageMatched = false;
- AKS.pageExcluded = false;
- for (let pattern of AKS.matchingPatterns) {
- if (pattern &&
- !pattern.hasPrefix("#") &&
- AKS.activeTabLocation.match(new RegExp(pattern))) {
- AKS.pageMatched = true;
- break;
- }
- }
- for (let pattern of AKS.exclusionPatterns) {
- if (pattern &&
- !pattern.hasPrefix("#") &&
- AKS.activeTabLocation.match(new RegExp(pattern))) {
- AKS.pageExcluded = true;
- break;
- }
- }
- }
-
- function updateState(result) {
- AKS.mode = result.mode || "blacklist";
- AKS.matchingPatterns = (AKS.mode == "whitelist") ?
- [ ".*" ] :
- (typeof result.matchingPatterns != "undefined" ?
- result.matchingPatterns.split("\n") :
- [ ]);
- AKS.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 (AKS.mode == "blacklist") {
- if (AKS.pageMatched && !AKS.pageExcluded) {
- active = true;
- } else {
- active = false;
- }
- } else {
- if (!AKS.pageExcluded) {
- active = false;
- } else {
- active = true;
- }
- }
-
- mainButton.classList.toggle("active", active);
- statusLabel.classList.toggle("active", active);
-
- mainButton.classList.toggle("whitelist", (AKS.mode == "whitelist"));
- info.classList.toggle("whitelist", (AKS.mode == "whitelist"));
- }
-
- /******************/
- /* INITIALIZATION */
- /******************/
-
- function initialize() {
- window.AKS = { };
-
- // Update version.
- document.querySelector(".info-header .version").innerHTML = chrome.runtime.getManifest().version;
-
- // Retrieve saved settings.
- chrome.tabs.query({currentWindow: true, active: true}, (tabs) => {
- AKS.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": AKS.exclusionPatterns.join("\n"),
- "mode": AKS.mode
- };
- if (AKS.mode == "blacklist")
- changes.matchingPatterns = AKS.matchingPatterns.join("\n");
- chrome.storage.sync.set(changes, () => {
- updateUIState();
- let reloadButton = document.querySelector("button.reload-button");
- if (AKS.pageMatched && !AKS.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 the Help button.
- document.querySelector("button.help-button").addActivateEvent(() => {
- // TODO: code!
- });
-
- // Listener for reload button (appears when sticky-killing is toggled OFF).
- document.querySelector("button.reload-button").addActivateEvent(() => {
- chrome.tabs.update(null, { url: AKS.activeTabLocation });
- window.close();
- });
- }
-
- initialize();
|