diff --git a/docs/unique-pattern-proposal.md b/docs/unique-pattern-proposal.md index c72f285..e035dd4 100644 --- a/docs/unique-pattern-proposal.md +++ b/docs/unique-pattern-proposal.md @@ -1,7 +1,10 @@ # Proposal: Add `unique_pattern` Field to Rule Schema ## Status -🟡 **Proposed** - Not yet implemented +⚪ **Superseded** - Not implemented. Solved without a new field (#507): when `url_matcher` has +capture groups and `url_fragment` has no `$n` placeholders, the captured values are the unique key, +so tabs are duplicates only if they capture the same values. See `_getUniqueKey` in +`src/common/helpers.ts`. ## Problem Statement diff --git a/src/background/TabRulesService.ts b/src/background/TabRulesService.ts index 3fd3b16..210e1da 100644 --- a/src/background/TabRulesService.ts +++ b/src/background/TabRulesService.ts @@ -7,7 +7,7 @@ import { _setStorage, _shouldSkipUrl, } from '../common/storage'; -import { _processUrlFragment } from '../common/helpers'; +import { _getUniqueKey } from '../common/helpers'; /** * Service responsible for applying tab modification rules @@ -70,21 +70,15 @@ export class TabRulesService { } } - const processedUrlFragment = _processUrlFragment( - urlFragment, - currentTab.url, - rule?.tab?.url_matcher - ); + const uniqueKey = _getUniqueKey(urlFragment, currentTab.url, rule?.tab?.url_matcher); - // Without url_matcher every tab of the rule is one group; with it, the group is the processed fragment - const lockKey = rule?.tab?.url_matcher - ? `${rule?.id ?? ''}|${processedUrlFragment}` - : `${rule?.id ?? ''}`; + // Without url_matcher every tab of the rule is one group; with it, the group is the unique key + const lockKey = rule?.tab?.url_matcher ? `${rule?.id ?? ''}|${uniqueKey}` : `${rule?.id ?? ''}`; const previous = this.uniqueLocks.get(lockKey) ?? Promise.resolve(); const run = previous .catch(() => undefined) - .then(() => this.closeDuplicates(rule, urlFragment, processedUrlFragment, currentTab)); + .then(() => this.closeDuplicates(rule, urlFragment, uniqueKey, currentTab)); this.uniqueLocks.set(lockKey, run); try { @@ -97,7 +91,7 @@ export class TabRulesService { private async closeDuplicates( rule: Rule, urlFragment: string, - processedUrlFragment: string, + uniqueKey: string, currentTab: chrome.tabs.Tab ): Promise { const tabs = await this.queryTabs({}); @@ -151,11 +145,10 @@ export class TabRulesService { continue; } - // Process the fragment for each tab to compare - const tabProcessedFragment = _processUrlFragment(urlFragment, tab.url, rule.tab.url_matcher); + // Same unique key (captured values or processed fragment) means duplicate + const tabUniqueKey = _getUniqueKey(urlFragment, tab.url, rule.tab.url_matcher); - // Compare processed fragments instead of raw URL - if (tabProcessedFragment === processedUrlFragment && tab.id !== currentTab.id) { + if (tabUniqueKey === uniqueKey && tab.id !== currentTab.id) { // Remove beforeunload handler from the duplicate tab before closing it try { await chrome.scripting.executeScript({ diff --git a/src/background/__tests__/TabRulesService.test.ts b/src/background/__tests__/TabRulesService.test.ts index 2d3baba..5b81bbc 100644 --- a/src/background/__tests__/TabRulesService.test.ts +++ b/src/background/__tests__/TabRulesService.test.ts @@ -46,7 +46,7 @@ vi.mock('../../common/storage', () => ({ // Mock helpers module vi.mock('../../common/helpers', () => ({ - _processUrlFragment: vi.fn((_fragment, url) => url), + _getUniqueKey: vi.fn((_fragment, url) => url), })); import { diff --git a/src/background/__tests__/TabRulesService.unique.test.ts b/src/background/__tests__/TabRulesService.unique.test.ts index b9679c4..505251c 100644 --- a/src/background/__tests__/TabRulesService.unique.test.ts +++ b/src/background/__tests__/TabRulesService.unique.test.ts @@ -437,4 +437,58 @@ describe('TabRulesService - GitHub Unique Issue Test', () => { expect(mockChrome.tabs.remove).not.toHaveBeenCalled(); }); }); + + describe('url_matcher capture groups as unique key (#507)', () => { + // The sample GitHub Issues rule: REGEX detection without $ placeholders + const message = { + url_fragment: 'github\\.com/.+/issues/\\d+', + rule: { + id: 'github-issues', + detection: 'REGEX', + url_fragment: 'github\\.com/.+/issues/\\d+', + tab: { + unique: true, + url_matcher: 'github\\.com/.+/issues/(\\d+)', + }, + } as Rule, + }; + + it('should close another tab of the same issue', async () => { + const currentTab = { + id: 1, + url: 'https://github.com/owner/repo/issues/123', + } as chrome.tabs.Tab; + const sameIssue = { + id: 2, + url: 'https://github.com/owner/repo/issues/123#issuecomment-1', + } as chrome.tabs.Tab; + const otherIssue = { + id: 3, + url: 'https://github.com/owner/repo/issues/456', + } as chrome.tabs.Tab; + + mockChrome.tabs.query.mockImplementation((_queryInfo: any, callback: any) => { + callback([currentTab, sameIssue, otherIssue]); + }); + + await service.handleSetUnique(message, currentTab); + + expect(mockChrome.tabs.remove).toHaveBeenCalledTimes(1); + expect(mockChrome.tabs.remove).toHaveBeenCalledWith(2); + }); + + it('should keep tabs of different issues open', async () => { + const tab123 = { id: 1, url: 'https://github.com/owner/repo/issues/123' } as chrome.tabs.Tab; + const tab456 = { id: 2, url: 'https://github.com/owner/repo/issues/456' } as chrome.tabs.Tab; + const tab789 = { id: 3, url: 'https://github.com/other/repo/issues/789' } as chrome.tabs.Tab; + + mockChrome.tabs.query.mockImplementation((_queryInfo: any, callback: any) => { + callback([tab123, tab456, tab789]); + }); + + await service.handleSetUnique(message, tab123); + + expect(mockChrome.tabs.remove).not.toHaveBeenCalled(); + }); + }); }); diff --git a/src/common/__tests__/detection-vs-uniqueness.test.ts b/src/common/__tests__/detection-vs-uniqueness.test.ts index 970c321..4043d2b 100644 --- a/src/common/__tests__/detection-vs-uniqueness.test.ts +++ b/src/common/__tests__/detection-vs-uniqueness.test.ts @@ -1,5 +1,5 @@ -import { describe, it, expect } from 'vitest'; -import { _processUrlFragment } from '../helpers'; +import { describe, it, expect, vi } from 'vitest'; +import { _getUniqueKey, _processUrlFragment } from '../helpers'; /** * This test demonstrates the fundamental conflict between detection and uniqueness @@ -212,4 +212,35 @@ describe('Detection vs Uniqueness Conflict', () => { // So issue #123 and #456 can coexist! }); }); + + describe('_getUniqueKey (#507)', () => { + const matcher = 'github\\.com/.+/issues/(\\d+)'; + + it('uses the captured values when url_matcher has capture groups', () => { + const fragment = 'github\\.com/.+/issues/\\d+'; + + expect(_getUniqueKey(fragment, 'https://github.com/a/b/issues/123', matcher)).toBe('["123"]'); + expect(_getUniqueKey(fragment, 'https://github.com/a/b/issues/456', matcher)).toBe('["456"]'); + }); + + it('keeps the processed fragment when url_fragment has $ placeholders', () => { + expect( + _getUniqueKey('github.com/issues/$1', 'https://github.com/a/b/issues/123', matcher) + ).toBe('github.com/issues/123'); + }); + + it('falls back to url_fragment without url_matcher, without capture groups, or without a match', () => { + const url = 'https://github.com/a/b/issues/123'; + + expect(_getUniqueKey('github.com', url, null)).toBe('github.com'); + expect(_getUniqueKey('github.com', url, 'github\\.com/.+/issues/\\d+')).toBe('github.com'); + expect(_getUniqueKey('github.com', 'https://example.com', matcher)).toBe('github.com'); + }); + + it('falls back to url_fragment on an invalid regex', () => { + vi.spyOn(console, 'error').mockImplementation(() => undefined); + + expect(_getUniqueKey('github.com', 'https://github.com', '(')).toBe('github.com'); + }); + }); }); diff --git a/src/common/helpers.ts b/src/common/helpers.ts index 7d5dcbd..815f375 100644 --- a/src/common/helpers.ts +++ b/src/common/helpers.ts @@ -334,3 +334,31 @@ export function _processUrlFragment( return processedFragment; } + +/** + * Computes the key used by the "unique" option to decide whether two tabs are duplicates. + * - url_fragment with $n placeholders: the processed fragment (explicit template) + * - url_matcher with capture groups: the captured values, so e.g. each GitHub issue number + * is its own unique group + * - otherwise: the url_fragment itself, so every tab of the rule is one group + */ +export function _getUniqueKey( + urlFragment: string, + currentUrl: string, + urlMatcher?: string | null +): string { + if (!urlMatcher || urlFragment.includes('$')) { + return _processUrlFragment(urlFragment, currentUrl, urlMatcher); + } + + try { + const match = new RegExp(urlMatcher).exec(currentUrl); + if (match && match.length > 1) { + return JSON.stringify(match.slice(1)); + } + } catch (e) { + console.error('Tabee: Error computing unique key:', e); + } + + return urlFragment; +} diff --git a/src/components/options/center/sections/TabRules/RuleForm.vue b/src/components/options/center/sections/TabRules/RuleForm.vue index e6d2dde..c8d6821 100644 --- a/src/components/options/center/sections/TabRules/RuleForm.vue +++ b/src/components/options/center/sections/TabRules/RuleForm.vue @@ -257,7 +257,8 @@
Regex to capture text from URL. Use $0, $1, $2... in Tab Title to reference captured - groups.