MCPcopy Create free account
hub / github.com/scriptscat/scriptcat / migrateChromeStorage

Function migrateChromeStorage

src/app/migrate.ts:246–290  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

244
245// 迁移chrome.storage的数据
246export function migrateChromeStorage() {
247 const migrationList = [
248 {
249 version: 1,
250 upgrade: async () => {
251 // 修复之前originDomain字段错误的数据
252 const scriptDAO = new ScriptDAO();
253 scriptDAO.enableCache();
254 const scripts = await scriptDAO.all();
255 return Promise.all(
256 scripts.map((script) => {
257 // 处理originDomain为空,但origin有值的情况
258 if (
259 !script.originDomain &&
260 (script.origin?.startsWith("http://") || script.origin?.startsWith("https://"))
261 ) {
262 const u = new URL(script.origin);
263 script.originDomain = u.hostname;
264 return scriptDAO.save(script);
265 }
266 })
267 );
268 },
269 },
270 ];
271 const localstorageDAO = new LocalStorageDAO();
272 localstorageDAO.get("migrations").then(async (item) => {
273 const migrations = item?.value || [];
274 for (let i = 0; i < migrationList.length; i++) {
275 const m = migrationList[i];
276 if (!migrations.includes(m.version)) {
277 try {
278 await m.upgrade();
279 migrations.push(m.version);
280 } catch (e) {
281 throw new Error(`Chrome storage migration v${m.version} failed: ${e}`);
282 }
283 }
284 }
285 localstorageDAO.save({
286 key: "migrations",
287 value: migrations,
288 });
289 });
290}
291
292export default function migrate() {
293 // 数据库索引定义,每一次变动必须更新version

Callers 1

service_worker.tsFile · 0.90

Calls 5

enableCacheMethod · 0.95
saveMethod · 0.95
saveMethod · 0.95
pushMethod · 0.80
getMethod · 0.65

Tested by

no test coverage detected