6d2b553ca4
refactor(storage): 将storage工具类重命名为chromeStorage 重命名utils/storage.ts为utils/chromeStorage.ts以更好地反映其功能用途 feat(messages): 简化消息协议定义并移除过时的消息类型 将messages.tsx中的硬编码消息对象替换为ProtocolMap接口定义, 移除了不再使用的popup、content和offscreen相关消息类型, 使消息系统更加简洁和类型安全 ```
44 lines
1.0 KiB
TypeScript
44 lines
1.0 KiB
TypeScript
import { StorageSchema } from 'types/storage';
|
|
|
|
class StorageUtils {
|
|
async get<K extends keyof StorageSchema>(key: K): Promise<StorageSchema[K]>;
|
|
|
|
async get<K extends keyof StorageSchema>(
|
|
key: K,
|
|
defaultValue?: StorageSchema[K],
|
|
): Promise<StorageSchema[K] | undefined>;
|
|
|
|
/**
|
|
* 获取值
|
|
* @param key
|
|
* @param defaultValue
|
|
* @returns
|
|
*/
|
|
async get<K extends keyof StorageSchema>(
|
|
key: K,
|
|
defaultValue?: StorageSchema[K],
|
|
): Promise<StorageSchema[K] | undefined> {
|
|
const result = await chrome.storage.local.get([key]);
|
|
return (result[key] ?? defaultValue) as StorageSchema[K] | undefined;
|
|
}
|
|
|
|
/**
|
|
* 设置值
|
|
* @param key
|
|
* @param value
|
|
*/
|
|
async set<K extends keyof StorageSchema>(key: K, value: StorageSchema[K]): Promise<void> {
|
|
await chrome.storage.local.set({ [key]: value });
|
|
}
|
|
|
|
/**
|
|
* 删除值
|
|
* @param key
|
|
*/
|
|
async remove(key: keyof StorageSchema): Promise<void> {
|
|
await chrome.storage.local.remove([key]);
|
|
}
|
|
}
|
|
|
|
export const storage = new StorageUtils();
|