Skip to content

Commit

Permalink
Fix mv2 chrome namespace apis
Browse files Browse the repository at this point in the history
  • Loading branch information
bytedream committed Nov 17, 2023
1 parent 76a5bac commit 6a8c705
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/lib/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,34 @@ export const Other = {
};

export async function storageGet<T>(key: string, defaultValue?: T): Promise<T | undefined> {
const entry = await chrome.storage.local.get(key);
const value = entry[key];
return value === undefined ? defaultValue : value;
let resolve: (value: T | undefined) => void;
const promise = new Promise<T | undefined>((r) => (resolve = r));

chrome.storage.local.get(key, (entry) => {
const value = entry[key];
resolve(value === undefined ? defaultValue : value);
});

return promise;
}

export async function storageSet<T>(key: string, value: T) {
let resolve: () => void;
const promise = new Promise<void>((r) => (resolve = r));

const obj = {
[key]: value
};
await chrome.storage.local.set(obj);
chrome.storage.local.set(obj, () => resolve());

return promise;
}

export async function storageDelete(key: string) {
await chrome.storage.local.remove(key);
let resolve: () => void;
const promise = new Promise<void>((r) => (resolve = r));

chrome.storage.local.remove(key, () => resolve());

return promise;
}

0 comments on commit 6a8c705

Please sign in to comment.