SzuruChrome/src/composables/useStorageLocal.ts

25 lines
717 B
TypeScript

import { storage } from "webextension-polyfill";
import type { MaybeRef, RemovableRef, StorageLikeAsync, UseStorageAsyncOptions } from "@vueuse/core";
import { useStorageAsync } from "@vueuse/core";
const storageLocal: StorageLikeAsync = {
removeItem(key: string) {
return storage.local.remove(key);
},
setItem(key: string, value: string) {
return storage.local.set({ [key]: value });
},
async getItem(key: string) {
const item = (await storage.local.get(key))[key];
return item;
},
};
export const useStorageLocal = <T>(
key: string,
initialValue: MaybeRef<T>,
options?: UseStorageAsyncOptions<T>
): RemovableRef<T> => useStorageAsync(key, initialValue, storageLocal, options);