Browse Source
fix: storage read/write in chrome extension
(cherry picked from commit bae9a7fec0c98807b3c5c3598f321135ccf6c9d5)
20230320
Jixun
3 years ago
committed by
MengYX
No known key found for this signature in database
GPG Key ID: E63F9C7303E8F604
1 changed files with
12 additions and
6 deletions
-
src/utils/storage/ChromeExtensionStorage.ts
|
|
@ -8,14 +8,20 @@ export default class ChromeExtensionStorage extends BaseStorage { |
|
|
|
} |
|
|
|
|
|
|
|
protected async load<T>(name: string, defaultValue: T): Promise<T> { |
|
|
|
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<T>(name: string, value: T): Promise<void> { |
|
|
|
return chrome.storage.local.set({ [name]: value }); |
|
|
|
return new Promise((resolve) => { |
|
|
|
chrome.storage.local.set({ [name]: value }, resolve); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|