Browse Source
fix: only pass over config settings
(cherry picked from commit 3884158f06b71907f004d7a2b4df53e3e486983b)
20230320
Jixun Wu
3 years ago
committed by
MengYX
No known key found for this signature in database
GPG Key ID: E63F9C7303E8F604
3 changed files with
26 additions and
7 deletions
-
src/utils/storage/BaseStorage.ts
-
src/utils/storage/BrowserNativeStorage.ts
-
src/utils/storage/ChromeExtensionStorage.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<T>(name: string, value: T): Promise<void>; |
|
|
|
|
|
@ -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<T>(name: string, value: T): Promise<void> { |
|
|
@ -20,14 +24,20 @@ export default class BrowserNativeStorage extends BaseStorage { |
|
|
|
public async getAll(): Promise<Record<string, any>> { |
|
|
|
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<string, any>): Promise<void> { |
|
|
|
for (const [key, value] of Object.entries(obj)) { |
|
|
|
localStorage.setItem(key, JSON.stringify(value)); |
|
|
|
await this.save(key, value); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
@ -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<Record<string, any>> { |
|
|
|
return new Promise((resolve) => { |
|
|
|
chrome.storage.local.get(null, resolve); |
|
|
|
chrome.storage.local.get(null, (obj: Record<string, any>) => { |
|
|
|
const result: Record<string, any> = {}; |
|
|
|
for (const [key, value] of Object.entries(obj)) { |
|
|
|
if (key.startsWith(KEY_PREFIX)) { |
|
|
|
result[key] = value; |
|
|
|
} |
|
|
|
} |
|
|
|
resolve(result); |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|