Skip to content

Commit 7d6dc1a

Browse files
use sync instead of local
1 parent 57f167a commit 7d6dc1a

File tree

6 files changed

+292
-13
lines changed

6 files changed

+292
-13
lines changed

src/background.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,19 @@ import {
55
_getRuleFromUrl,
66
_getStorageAsync,
77
_setStorage,
8+
_shouldSkipUrl,
89
} from './common/storage.ts';
910
import { _processUrlFragment } from './common/helpers.ts';
1011

1112
chrome.tabs.onUpdated.addListener(
1213
async (_: number, changeInfo: chrome.tabs.TabChangeInfo, tab: chrome.tabs.Tab) => {
1314
if (!changeInfo.url) return;
1415

16+
// Skip processing if lightweight mode excludes this URL
17+
if (await _shouldSkipUrl(changeInfo.url)) {
18+
return;
19+
}
20+
1521
await applyRuleToTab(tab);
1622
}
1723
);
@@ -92,6 +98,11 @@ chrome.tabs.onMoved.addListener(async (tabId) => {
9298
if (!tab) return;
9399
if (!tab.url) return;
94100

101+
// Skip processing if lightweight mode excludes this URL
102+
if (await _shouldSkipUrl(tab.url)) {
103+
return;
104+
}
105+
95106
const tabModifier = await _getStorageAsync();
96107
if (!tabModifier) return;
97108

src/common/storage.test.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ describe('Storage', () => {
2828
settings: {
2929
enable_new_version_notification: false,
3030
theme: 'dim',
31+
lightweight_mode_enabled: false,
32+
lightweight_mode_patterns: [],
3133
},
3234
});
3335
});
@@ -51,7 +53,7 @@ describe('Storage', () => {
5153
const mockData = {
5254
rules: [],
5355
groups: [],
54-
settings: { enable_new_version_notification: false, theme: 'dim' },
56+
settings: { enable_new_version_notification: false, theme: 'dim', lightweight_mode_enabled: false, lightweight_mode_patterns: [] },
5557
};
5658
global.chrome.storage.sync.get.mockImplementation((keys, callback) => {
5759
callback({ [STORAGE_KEY]: mockData });
@@ -70,7 +72,7 @@ describe('Storage', () => {
7072
const mockData = {
7173
rules: [],
7274
groups: [],
73-
settings: { enable_new_version_notification: false, theme: 'dim' },
75+
settings: { enable_new_version_notification: false, theme: 'dim', lightweight_mode_enabled: false, lightweight_mode_patterns: [] },
7476
};
7577
await _setStorage(mockData);
7678
expect(global.chrome.storage.sync.set).toHaveBeenCalledWith({
@@ -99,7 +101,7 @@ describe('Storage', () => {
99101
const mockData = {
100102
rules: [mockRule],
101103
groups: [],
102-
settings: { enable_new_version_notification: false, theme: 'dim' },
104+
settings: { enable_new_version_notification: false, theme: 'dim', lightweight_mode_enabled: false, lightweight_mode_patterns: [] },
103105
};
104106
global.chrome.storage.sync.get.mockImplementation((keys, callback) => {
105107
callback({ tab_modifier: mockData });
@@ -132,7 +134,7 @@ describe('Storage', () => {
132134
const mockData = {
133135
rules: [mockRule],
134136
groups: [],
135-
settings: { enable_new_version_notification: false, theme: 'dim' },
137+
settings: { enable_new_version_notification: false, theme: 'dim', lightweight_mode_enabled: false, lightweight_mode_patterns: [] },
136138
};
137139
global.chrome.storage.sync.get.mockImplementation((keys, callback) => {
138140
callback({ [STORAGE_KEY]: mockData });
@@ -164,7 +166,7 @@ describe('Storage', () => {
164166
const mockData = {
165167
rules: [mockRule],
166168
groups: [],
167-
settings: { enable_new_version_notification: false, theme: 'dim' },
169+
settings: { enable_new_version_notification: false, theme: 'dim', lightweight_mode_enabled: false, lightweight_mode_patterns: [] },
168170
};
169171
global.chrome.storage.sync.get.mockImplementation((keys, callback) => {
170172
callback({ [STORAGE_KEY]: mockData });
@@ -196,7 +198,7 @@ describe('Storage', () => {
196198
const mockData = {
197199
rules: [mockRule],
198200
groups: [],
199-
settings: { enable_new_version_notification: false, theme: 'dim' },
201+
settings: { enable_new_version_notification: false, theme: 'dim', lightweight_mode_enabled: false, lightweight_mode_patterns: [] },
200202
};
201203
global.chrome.storage.sync.get.mockImplementation((keys, callback) => {
202204
callback({ [STORAGE_KEY]: mockData });
@@ -228,7 +230,7 @@ describe('Storage', () => {
228230
const mockData = {
229231
rules: [mockRule],
230232
groups: [],
231-
settings: { enable_new_version_notification: false, theme: 'dim' },
233+
settings: { enable_new_version_notification: false, theme: 'dim', lightweight_mode_enabled: false, lightweight_mode_patterns: [] },
232234
};
233235
global.chrome.storage.sync.get.mockImplementation((keys, callback) => {
234236
callback({ [STORAGE_KEY]: mockData });
@@ -263,7 +265,7 @@ describe('Storage', () => {
263265
const mockData = {
264266
rules: [mockRule],
265267
groups: [],
266-
settings: { enable_new_version_notification: false, theme: 'dim' },
268+
settings: { enable_new_version_notification: false, theme: 'dim', lightweight_mode_enabled: false, lightweight_mode_patterns: [] },
267269
};
268270
global.chrome.storage.sync.get.mockImplementation((keys, callback) => {
269271
callback({ [STORAGE_KEY]: mockData });
@@ -298,7 +300,7 @@ describe('Storage', () => {
298300
const mockData = {
299301
rules: [mockRule],
300302
groups: [],
301-
settings: { enable_new_version_notification: false, theme: 'dim' },
303+
settings: { enable_new_version_notification: false, theme: 'dim', lightweight_mode_enabled: false, lightweight_mode_patterns: [] },
302304
};
303305
global.chrome.storage.sync.get.mockImplementation((keys, callback) => {
304306
callback({ [STORAGE_KEY]: mockData });
@@ -332,7 +334,7 @@ describe('Storage', () => {
332334
const mockData = {
333335
rules: [mockRule],
334336
groups: [],
335-
settings: { enable_new_version_notification: false, theme: 'dim' },
337+
settings: { enable_new_version_notification: false, theme: 'dim', lightweight_mode_enabled: false, lightweight_mode_patterns: [] },
336338
};
337339
global.chrome.storage.sync.get.mockImplementation((keys, callback) => {
338340
callback({ [STORAGE_KEY]: mockData });
@@ -365,7 +367,7 @@ describe('Storage', () => {
365367
const mockData = {
366368
rules: [mockRule],
367369
groups: [],
368-
settings: { enable_new_version_notification: false, theme: 'dim' },
370+
settings: { enable_new_version_notification: false, theme: 'dim', lightweight_mode_enabled: false, lightweight_mode_patterns: [] },
369371
};
370372
global.chrome.storage.sync.get.mockImplementation((keys, callback) => {
371373
callback({ [STORAGE_KEY]: mockData });

src/common/storage.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ export function _getDefaultTabModifierSettings(): TabModifierSettings {
1111
settings: {
1212
enable_new_version_notification: false,
1313
theme: 'dim',
14+
lightweight_mode_enabled: false,
15+
lightweight_mode_patterns: [],
1416
},
1517
};
1618
}
@@ -154,3 +156,46 @@ export async function _migrateLocalToSync(): Promise<void> {
154156
});
155157
});
156158
}
159+
160+
/**
161+
* Check if a URL should be excluded from Tab Modifier processing
162+
* based on lightweight mode patterns
163+
*/
164+
export async function _shouldSkipUrl(url: string): Promise<boolean> {
165+
const tabModifier = await _getStorageAsync();
166+
if (!tabModifier) {
167+
return false;
168+
}
169+
170+
const { settings } = tabModifier;
171+
172+
// If lightweight mode is not enabled or not configured, don't skip any URLs
173+
if (!settings.lightweight_mode_enabled || !settings.lightweight_mode_patterns) {
174+
return false;
175+
}
176+
177+
// Check if URL matches any enabled pattern
178+
for (const pattern of settings.lightweight_mode_patterns) {
179+
if (!pattern.enabled) continue;
180+
181+
try {
182+
if (pattern.type === 'domain') {
183+
// Simple domain matching
184+
if (url.includes(pattern.pattern)) {
185+
return true;
186+
}
187+
} else if (pattern.type === 'regex') {
188+
// Regex matching with safety check
189+
const isMatch = _safeRegexTestSync(pattern.pattern, url);
190+
if (isMatch) {
191+
return true;
192+
}
193+
}
194+
} catch (error) {
195+
console.error('[Tabee] Error checking lightweight mode pattern:', error);
196+
// Continue checking other patterns
197+
}
198+
}
199+
200+
return false;
201+
}

src/common/types.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,18 @@ export type Group = {
3535
collapsed: boolean;
3636
};
3737

38+
export type LightweightModePattern = {
39+
id: string;
40+
pattern: string;
41+
type: 'domain' | 'regex';
42+
enabled: boolean;
43+
};
44+
3845
export type Settings = {
3946
enable_new_version_notification: boolean;
4047
theme: string;
48+
lightweight_mode_enabled: boolean;
49+
lightweight_mode_patterns: LightweightModePattern[];
4150
};
4251

4352
export type TabModifierSettings = {

0 commit comments

Comments
 (0)