Skip to content

Commit 57f167a

Browse files
use sync instead of local
1 parent 2956f5b commit 57f167a

File tree

7 files changed

+82
-20
lines changed

7 files changed

+82
-20
lines changed

src/__mocks__/chrome.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ export const chrome = {
1515
set: vi.fn((items, callback) => callback && callback()),
1616
remove: vi.fn((key, callback) => callback && callback()),
1717
},
18+
sync: {
19+
get: vi.fn((keys, callback) => callback({ tab_modifier: null })),
20+
set: vi.fn((items, callback) => callback && callback()),
21+
remove: vi.fn((key, callback) => callback && callback()),
22+
},
1823
},
1924
tabs: {
2025
ungroup: vi.fn(),

src/common/storage.test.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ describe('Storage', () => {
5353
groups: [],
5454
settings: { enable_new_version_notification: false, theme: 'dim' },
5555
};
56-
global.chrome.storage.local.get.mockImplementation((keys, callback) => {
56+
global.chrome.storage.sync.get.mockImplementation((keys, callback) => {
5757
callback({ [STORAGE_KEY]: mockData });
5858
});
5959
const storageData = await _getStorageAsync();
@@ -63,7 +63,7 @@ describe('Storage', () => {
6363
it('_clearStorage should remove storage data', async () => {
6464
await _clearStorage();
6565

66-
expect(global.chrome.storage.local.remove).toHaveBeenCalledWith(STORAGE_KEY);
66+
expect(global.chrome.storage.sync.remove).toHaveBeenCalledWith(STORAGE_KEY);
6767
});
6868

6969
it('_setStorage should set storage data', async () => {
@@ -73,7 +73,7 @@ describe('Storage', () => {
7373
settings: { enable_new_version_notification: false, theme: 'dim' },
7474
};
7575
await _setStorage(mockData);
76-
expect(global.chrome.storage.local.set).toHaveBeenCalledWith({
76+
expect(global.chrome.storage.sync.set).toHaveBeenCalledWith({
7777
[STORAGE_KEY]: expect.objectContaining(mockData),
7878
});
7979
});
@@ -101,7 +101,7 @@ describe('Storage', () => {
101101
groups: [],
102102
settings: { enable_new_version_notification: false, theme: 'dim' },
103103
};
104-
global.chrome.storage.local.get.mockImplementation((keys, callback) => {
104+
global.chrome.storage.sync.get.mockImplementation((keys, callback) => {
105105
callback({ tab_modifier: mockData });
106106
});
107107
global.chrome.runtime.lastError = null;
@@ -134,7 +134,7 @@ describe('Storage', () => {
134134
groups: [],
135135
settings: { enable_new_version_notification: false, theme: 'dim' },
136136
};
137-
global.chrome.storage.local.get.mockImplementation((keys, callback) => {
137+
global.chrome.storage.sync.get.mockImplementation((keys, callback) => {
138138
callback({ [STORAGE_KEY]: mockData });
139139
});
140140
global.chrome.runtime.lastError = null;
@@ -166,7 +166,7 @@ describe('Storage', () => {
166166
groups: [],
167167
settings: { enable_new_version_notification: false, theme: 'dim' },
168168
};
169-
global.chrome.storage.local.get.mockImplementation((keys, callback) => {
169+
global.chrome.storage.sync.get.mockImplementation((keys, callback) => {
170170
callback({ [STORAGE_KEY]: mockData });
171171
});
172172
global.chrome.runtime.lastError = null;
@@ -198,7 +198,7 @@ describe('Storage', () => {
198198
groups: [],
199199
settings: { enable_new_version_notification: false, theme: 'dim' },
200200
};
201-
global.chrome.storage.local.get.mockImplementation((keys, callback) => {
201+
global.chrome.storage.sync.get.mockImplementation((keys, callback) => {
202202
callback({ [STORAGE_KEY]: mockData });
203203
});
204204
global.chrome.runtime.lastError = null;
@@ -230,7 +230,7 @@ describe('Storage', () => {
230230
groups: [],
231231
settings: { enable_new_version_notification: false, theme: 'dim' },
232232
};
233-
global.chrome.storage.local.get.mockImplementation((keys, callback) => {
233+
global.chrome.storage.sync.get.mockImplementation((keys, callback) => {
234234
callback({ [STORAGE_KEY]: mockData });
235235
});
236236
global.chrome.runtime.lastError = null;
@@ -265,7 +265,7 @@ describe('Storage', () => {
265265
groups: [],
266266
settings: { enable_new_version_notification: false, theme: 'dim' },
267267
};
268-
global.chrome.storage.local.get.mockImplementation((keys, callback) => {
268+
global.chrome.storage.sync.get.mockImplementation((keys, callback) => {
269269
callback({ [STORAGE_KEY]: mockData });
270270
});
271271
global.chrome.runtime.lastError = null;
@@ -300,7 +300,7 @@ describe('Storage', () => {
300300
groups: [],
301301
settings: { enable_new_version_notification: false, theme: 'dim' },
302302
};
303-
global.chrome.storage.local.get.mockImplementation((keys, callback) => {
303+
global.chrome.storage.sync.get.mockImplementation((keys, callback) => {
304304
callback({ [STORAGE_KEY]: mockData });
305305
});
306306
global.chrome.runtime.lastError = null;
@@ -334,7 +334,7 @@ describe('Storage', () => {
334334
groups: [],
335335
settings: { enable_new_version_notification: false, theme: 'dim' },
336336
};
337-
global.chrome.storage.local.get.mockImplementation((keys, callback) => {
337+
global.chrome.storage.sync.get.mockImplementation((keys, callback) => {
338338
callback({ [STORAGE_KEY]: mockData });
339339
});
340340
global.chrome.runtime.lastError = null;
@@ -367,7 +367,7 @@ describe('Storage', () => {
367367
groups: [],
368368
settings: { enable_new_version_notification: false, theme: 'dim' },
369369
};
370-
global.chrome.storage.local.get.mockImplementation((keys, callback) => {
370+
global.chrome.storage.sync.get.mockImplementation((keys, callback) => {
371371
callback({ [STORAGE_KEY]: mockData });
372372
});
373373
global.chrome.runtime.lastError = null;

src/common/storage.ts

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export function _getDefaultGroup(title?: string): Group {
4747

4848
export function _getStorageAsync(): Promise<TabModifierSettings | undefined> {
4949
return new Promise((resolve, reject) => {
50-
chrome.storage.local.get(STORAGE_KEY, (items) => {
50+
chrome.storage.sync.get(STORAGE_KEY, (items) => {
5151
if (chrome.runtime.lastError) {
5252
reject(new Error(chrome.runtime.lastError.message));
5353
} else {
@@ -58,11 +58,11 @@ export function _getStorageAsync(): Promise<TabModifierSettings | undefined> {
5858
}
5959

6060
export async function _clearStorage(): Promise<void> {
61-
await chrome.storage.local.remove(STORAGE_KEY);
61+
await chrome.storage.sync.remove(STORAGE_KEY);
6262
}
6363

6464
export async function _setStorage(tabModifier: TabModifierSettings): Promise<void> {
65-
await chrome.storage.local.set({
65+
await chrome.storage.sync.set({
6666
tab_modifier: _clone({
6767
rules: tabModifier.rules,
6868
groups: tabModifier.groups,
@@ -108,3 +108,49 @@ export async function _getRuleFromUrl(url: string): Promise<Rule | undefined> {
108108

109109
return foundRule;
110110
}
111+
112+
/**
113+
* Migrates data from chrome.storage.local to chrome.storage.sync
114+
* This function checks if data exists in local storage, and if so,
115+
* copies it to sync storage and then removes it from local storage.
116+
* This ensures a smooth transition without breaking existing installations.
117+
*/
118+
export async function _migrateLocalToSync(): Promise<void> {
119+
return new Promise((resolve, reject) => {
120+
// Check if data exists in local storage
121+
chrome.storage.local.get(STORAGE_KEY, async (localItems) => {
122+
if (chrome.runtime.lastError) {
123+
reject(new Error(chrome.runtime.lastError.message));
124+
return;
125+
}
126+
127+
const localData = localItems[STORAGE_KEY];
128+
129+
// If no data in local storage, nothing to migrate
130+
if (!localData) {
131+
resolve();
132+
return;
133+
}
134+
135+
try {
136+
// Check if sync storage already has data
137+
const syncData = await _getStorageAsync();
138+
139+
// Only migrate if sync storage is empty
140+
if (!syncData) {
141+
console.log('[Tab Modifier] Migrating data from local to sync storage...');
142+
await _setStorage(localData);
143+
console.log('[Tab Modifier] Migration successful');
144+
}
145+
146+
// Clean up local storage after successful migration
147+
await chrome.storage.local.remove(STORAGE_KEY);
148+
console.log('[Tab Modifier] Local storage cleaned up');
149+
150+
resolve();
151+
} catch (error) {
152+
reject(error);
153+
}
154+
});
155+
});
156+
}

src/content.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
function _getStorageAsync() {
22
return new Promise((resolve, reject) => {
3-
chrome.storage.local.get(STORAGE_KEY, (items) => {
3+
chrome.storage.sync.get(STORAGE_KEY, (items) => {
44
if (chrome.runtime.lastError) {
55
reject(new Error(chrome.runtime.lastError.message));
66
} else {
@@ -357,7 +357,7 @@ export async function applyRule(ruleParam, updateTitle) {
357357
});
358358
}
359359

360-
chrome.storage.local.get(STORAGE_KEY, async (items) => {
360+
chrome.storage.sync.get(STORAGE_KEY, async (items) => {
361361
const tabModifier = items?.[STORAGE_KEY];
362362

363363
if (!tabModifier) {

src/content.test.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
import { beforeEach, describe, expect, it, vi } from 'vitest';
2-
import { _getRuleFromUrl } from './common/storage.ts';
3-
import { applyRule, getTextBySelector, processIcon, processTitle, updateTitle } from './content.js';
4-
52
import { chrome } from './__mocks__/chrome.js';
63

4+
// Set up global chrome object BEFORE importing content.js
75
global.chrome = chrome;
86

7+
// Mock storage functions before importing
98
vi.mock('./common/storage.ts');
109

10+
import { _getRuleFromUrl } from './common/storage.ts';
11+
import { applyRule, getTextBySelector, processIcon, processTitle, updateTitle } from './content.js';
12+
1113
describe('Content', () => {
1214
beforeEach(() => {
1315
document.title = 'Original Title';
1416
document.body.innerHTML = '';
1517
vi.clearAllMocks();
1618
global.chrome = chrome;
19+
// Reset mock implementations
20+
global.chrome.storage.sync.get = vi.fn((keys, callback) => callback({ tab_modifier: null }));
1721
});
1822

1923
describe('updateTitle', () => {

src/stores/rules.store.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
_getDefaultTabModifierSettings,
77
_getStorageAsync,
88
_setStorage,
9+
_migrateLocalToSync,
910
} from '../common/storage.ts';
1011

1112
/**
@@ -102,6 +103,9 @@ export const useRulesStore = defineStore('rules', {
102103
},
103104
async init() {
104105
try {
106+
// Migrate data from local to sync storage if needed
107+
await _migrateLocalToSync();
108+
105109
let tabModifier = await _getStorageAsync();
106110

107111
if (!tabModifier) {

vitest.setup.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ global.chrome = {
1111
local: {
1212
get: vi.fn((keys, callback) => callback({ tab_modifier: null })),
1313
},
14+
sync: {
15+
get: vi.fn((keys, callback) => callback({ tab_modifier: null })),
16+
},
1417
},
1518
tabs: {
1619
ungroup: vi.fn(),

0 commit comments

Comments
 (0)