From 8673adfda64fb9e33315b9fb40d06ed3860311e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=B2=81=E6=A0=91=E4=BA=BA?= Date: Mon, 20 Dec 2021 19:13:27 +0000 Subject: [PATCH] fix: storage read/write in chrome extension (cherry picked from commit bae9a7fec0c98807b3c5c3598f321135ccf6c9d5) --- src/utils/storage/ChromeExtensionStorage.ts | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/utils/storage/ChromeExtensionStorage.ts b/src/utils/storage/ChromeExtensionStorage.ts index ccf8048..8fa6a5e 100644 --- a/src/utils/storage/ChromeExtensionStorage.ts +++ b/src/utils/storage/ChromeExtensionStorage.ts @@ -8,14 +8,20 @@ export default class ChromeExtensionStorage extends BaseStorage { } protected async load(name: string, defaultValue: T): Promise { - const result = await chrome.storage.local.get({ [name]: defaultValue }); - if (Object.prototype.hasOwnProperty.call(result, name)) { - return result[name]; - } - return defaultValue; + return new Promise((resolve) => { + chrome.storage.local.get({ [name]: defaultValue }, (result: any) => { + if (Object.prototype.hasOwnProperty.call(result, name)) { + resolve(result[name]); + } else { + resolve(defaultValue); + } + }); + }); } protected async save(name: string, value: T): Promise { - return chrome.storage.local.set({ [name]: value }); + return new Promise((resolve) => { + chrome.storage.local.set({ [name]: value }, resolve); + }); } }