Added support for comments in patterns lists, and completed auto enable/disable pattern code

This commit is contained in:
Said Achmiz 2019-01-31 13:38:42 -05:00
parent 9ee6b8039f
commit 3ce1693734

View File

@ -29,36 +29,64 @@ function toggleState() {
/* 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) { if (!ASK.pageMatched) {
ASK.matchingPatterns.push(regExpForCurrentTab()); addPatternForCurrentTab(ASK.matchingPatterns);
} }
if (ASK.pageExcluded) { if (ASK.pageExcluded) {
ASK.exclusionPatterns = ASK.exclusionPatterns.filter(pattern => disablePatternsForCurrentTab(ASK.exclusionPatterns);
!ASK.activeTabLocation.match(new RegExp(pattern))
);
} }
} 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. */
ASK.matchingPatterns = ASK.matchingPatterns.filter(pattern => disablePatternsForCurrentTab(ASK.matchingPatterns);
!ASK.activeTabLocation.match(new RegExp(pattern))
);
} }
} else { // if whitelist mode } else { // if whitelist mode
if (ASK.pageExcluded) { if (ASK.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. */
ASK.exclusionPatterns = ASK.exclusionPatterns.filter(pattern => disablePatternsForCurrentTab(ASK.exclusionPatterns);
!ASK.activeTabLocation.match(new RegExp(pattern))
);
} 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. */
ASK.exclusionPatterns.push(regExpForCurrentTab()); addPatternForCurrentTab(ASK.exclusionPatterns);
} }
} }
recalculatePatternEffects(); 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 tabs 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 weve
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() { function regExpForCurrentTab() {
let a = document.createElement("A"); let a = document.createElement("A");
a.href = ASK.activeTabLocation; a.href = ASK.activeTabLocation;