Skip to content

Commit

Permalink
fix: regex detection (#328)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienfontaine authored Jun 26, 2024
1 parent 6c7a528 commit c6a0f74
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/common/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export async function _getRuleFromUrl(url: string): Promise<Rule | undefined> {
case 'ENDS':
case 'ENDS_WITH':
return url.endsWith(urlFragment);
case 'REGEX':
case 'REGEXP':
return new RegExp(urlFragment).test(url);
case 'EXACT':
Expand Down
1 change: 0 additions & 1 deletion src/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ export async function applyRule(ruleParam, updateTitle) {

const callback = function () {
if (document.title !== lastTitle) {
console.log('Processing title with mutation observer');
originalTitleElement.setAttribute('content', document.title);

originalTitle = originalTitleElement.getAttribute('content');
Expand Down
38 changes: 27 additions & 11 deletions src/stores/rules.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,20 @@ export const useRulesStore = defineStore('rules', {
fixDuplicateRuleIds(rules: Rule[]) {
const uniqueIds = new Set();

for (const rule of rules) {
rules = rules.map((rule) => {
if (uniqueIds.has(rule.id)) {
rule.id = _generateRandomId();
}

uniqueIds.add(rule.id);
}

return rule;
});

return rules;
},
handleMissingRuleSettings(rules: Rule[]) {
rules.forEach((rule) => {
handleMissingRuleSettings(rules: Rule[]): Rule[] {
rules = rules.map((rule) => {
if (!rule.id) {
rule.id = _generateRandomId();
}
Expand All @@ -49,10 +51,18 @@ export const useRulesStore = defineStore('rules', {
rule.detection = 'STARTS_WITH';
}

if (rule.detection === 'REGEXP') {
rule.detection = 'REGEX';
}

if (rule.detection === 'ENDS') {
rule.detection = 'ENDS_WITH';
}

return rule;
});

return rules;
},
async updateRulePosition(ruleId: string, position: number) {
const index = this.getRuleIndexById(ruleId);
Expand All @@ -68,12 +78,16 @@ export const useRulesStore = defineStore('rules', {

await this.save();
},
addMissingInvisibleChar(groups: Group[]) {
groups.forEach((group) => {
addMissingInvisibleChar(groups: Group[]): Group[] {
groups = groups.map((group) => {
if (!group.title.endsWith(INVISIBLE_CHAR)) {
group.title = group.title + INVISIBLE_CHAR;
}

return group;
});

return groups;
},
async init() {
try {
Expand All @@ -88,12 +102,10 @@ export const useRulesStore = defineStore('rules', {
throw new Error('Failed to set config');
}

this.handleMissingRuleSettings(tabModifier.rules);

// FIX: Remove this later
tabModifier.rules = this.handleMissingRuleSettings(tabModifier.rules);
tabModifier.rules = this.fixDuplicateRuleIds(tabModifier.rules);

this.addMissingInvisibleChar(tabModifier.groups);
tabModifier.groups = this.addMissingInvisibleChar(tabModifier.groups);

this.groups = tabModifier.groups;
this.rules = tabModifier.rules;
Expand All @@ -112,6 +124,10 @@ export const useRulesStore = defineStore('rules', {
shouldInit: boolean = true
): Promise<TabModifierSettings | undefined> {
try {
config.rules = this.handleMissingRuleSettings(config.rules);
config.rules = this.fixDuplicateRuleIds(config.rules);
config.groups = this.addMissingInvisibleChar(config.groups);

const defaultConfig = _getDefaultTabModifierSettings();

const mergedConfig = {
Expand Down Expand Up @@ -312,7 +328,7 @@ export const useRulesStore = defineStore('rules', {
tabModifier = _getDefaultTabModifierSettings();
}

tabModifier.rules.push(rule);
tabModifier.rules.unshift(rule);

this.rules = tabModifier.rules;

Expand Down

0 comments on commit c6a0f74

Please sign in to comment.