From 0715eeea0b083f5c585f1597a193f790d68f4aef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=B2=81=E6=A0=91=E4=BA=BA?= Date: Tue, 21 Dec 2021 22:17:43 +0000 Subject: [PATCH] fix: only pass over config settings (cherry picked from commit 3884158f06b71907f004d7a2b4df53e3e486983b) --- src/utils/storage/BaseStorage.ts | 3 ++- src/utils/storage/BrowserNativeStorage.ts | 18 ++++++++++++++---- src/utils/storage/ChromeExtensionStorage.ts | 12 ++++++++++-- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/utils/storage/BaseStorage.ts b/src/utils/storage/BaseStorage.ts index 2e163ca..466efee 100644 --- a/src/utils/storage/BaseStorage.ts +++ b/src/utils/storage/BaseStorage.ts @@ -1,4 +1,5 @@ -const KEY_JOOX_UUID = 'joox.uuid'; +export const KEY_PREFIX = 'um.conf.'; +const KEY_JOOX_UUID = `${KEY_PREFIX}joox.uuid`; export default abstract class BaseStorage { protected abstract save(name: string, value: T): Promise; diff --git a/src/utils/storage/BrowserNativeStorage.ts b/src/utils/storage/BrowserNativeStorage.ts index 2deae0c..4dd5c17 100644 --- a/src/utils/storage/BrowserNativeStorage.ts +++ b/src/utils/storage/BrowserNativeStorage.ts @@ -1,4 +1,4 @@ -import BaseStorage from './BaseStorage'; +import BaseStorage, { KEY_PREFIX } from './BaseStorage'; export default class BrowserNativeStorage extends BaseStorage { public static get works() { @@ -10,7 +10,11 @@ export default class BrowserNativeStorage extends BaseStorage { if (result === null) { return defaultValue; } - return JSON.parse(result); + try { + return JSON.parse(result); + } catch { + return defaultValue; + } } protected async save(name: string, value: T): Promise { @@ -20,14 +24,20 @@ export default class BrowserNativeStorage extends BaseStorage { public async getAll(): Promise> { const result = {}; for (const [key, value] of Object.entries(localStorage)) { - Object.assign(result, { [key]: JSON.parse(value) }); + if (key.startsWith(KEY_PREFIX)) { + try { + Object.assign(result, { [key]: JSON.parse(value) }); + } catch { + // ignored + } + } } return result; } public async setAll(obj: Record): Promise { for (const [key, value] of Object.entries(obj)) { - localStorage.setItem(key, JSON.stringify(value)); + await this.save(key, value); } } } diff --git a/src/utils/storage/ChromeExtensionStorage.ts b/src/utils/storage/ChromeExtensionStorage.ts index b2b48be..5f2f063 100644 --- a/src/utils/storage/ChromeExtensionStorage.ts +++ b/src/utils/storage/ChromeExtensionStorage.ts @@ -1,4 +1,4 @@ -import BaseStorage from './BaseStorage'; +import BaseStorage, { KEY_PREFIX } from './BaseStorage'; declare var chrome: any; @@ -27,7 +27,15 @@ export default class ChromeExtensionStorage extends BaseStorage { public async getAll(): Promise> { return new Promise((resolve) => { - chrome.storage.local.get(null, resolve); + chrome.storage.local.get(null, (obj: Record) => { + const result: Record = {}; + for (const [key, value] of Object.entries(obj)) { + if (key.startsWith(KEY_PREFIX)) { + result[key] = value; + } + } + resolve(result); + }); }); }