diff --git a/popup.js b/popup.js index a91f09d..df53288 100644 --- a/popup.js +++ b/popup.js @@ -29,36 +29,64 @@ function toggleState() { /* 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()); + addPatternForCurrentTab(ASK.matchingPatterns); } if (ASK.pageExcluded) { - ASK.exclusionPatterns = ASK.exclusionPatterns.filter(pattern => - !ASK.activeTabLocation.match(new RegExp(pattern)) - ); + disablePatternsForCurrentTab(ASK.exclusionPatterns); } } 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)) - ); + 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. */ - ASK.exclusionPatterns = ASK.exclusionPatterns.filter(pattern => - !ASK.activeTabLocation.match(new RegExp(pattern)) - ); + disablePatternsForCurrentTab(ASK.exclusionPatterns); } else { /* In this case, stickies ARE being killed. We must add an exclusion pattern. */ - ASK.exclusionPatterns.push(regExpForCurrentTab()); + 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;