@mirawision/chrome-api
Version:
A comprehensive TypeScript library for Chrome Extension API, providing type-safe wrappers and utilities for bookmarks, commands, context menus, cookies, downloads, storage, notifications, runtime, scripting, and side panel functionalities.
129 lines (128 loc) • 4.43 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.StorageSync = void 0;
/**
* A class that provides a type-safe wrapper around Chrome's sync storage API.
* This class allows you to store and retrieve data that is synced across devices
* where the user is signed into Chrome. It's useful for storing user preferences
* and other small pieces of data that should persist across devices.
*/
class StorageSync {
/**
* Retrieves a value from sync storage by key.
* @param key - The key to retrieve
* @returns A promise that resolves to the stored value for the specified key
* @throws {Error} If there's an error retrieving the data
*/
static async get(key) {
return new Promise((resolve, reject) => {
chrome.storage.sync.get([key], (result) => {
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError);
}
else {
resolve(result[key]);
}
});
});
}
/**
* Retrieves all data from sync storage.
* @returns A promise that resolves to all stored data
* @throws {Error} If there's an error retrieving the data
*/
static async getAll() {
return new Promise((resolve, reject) => {
chrome.storage.sync.get(null, (result) => {
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError);
}
else {
resolve(result);
}
});
});
}
/**
* Stores a value in sync storage.
* @param key - The key under which to store the value
* @param value - The value to store
* @returns A promise that resolves when the value is stored
* @throws {Error} If there's an error storing the data
*/
static async set(key, value) {
return new Promise((resolve, reject) => {
chrome.storage.sync.set({ [key]: value }, () => {
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError);
}
else {
resolve();
}
});
});
}
/**
* Stores multiple key-value pairs in sync storage.
* @param data - An object containing the key-value pairs to store
* @returns A promise that resolves when all values are stored
* @throws {Error} If there's an error storing the data
*/
static async setBulk(data) {
return new Promise((resolve, reject) => {
chrome.storage.sync.set(data, () => {
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError);
}
else {
resolve();
}
});
});
}
/**
* Removes a value from sync storage.
* @param key - The key to remove
* @returns A promise that resolves when the value is removed
* @throws {Error} If there's an error removing the data
*/
static async remove(key) {
return new Promise((resolve, reject) => {
chrome.storage.sync.remove([key], () => {
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError);
}
else {
resolve();
}
});
});
}
/**
* Removes all data from sync storage.
* @returns A promise that resolves when all data is cleared
* @throws {Error} If there's an error clearing the data
*/
static async clear() {
return new Promise((resolve, reject) => {
chrome.storage.sync.clear(() => {
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError);
}
else {
resolve();
}
});
});
}
/**
* Adds a listener for storage change events.
* @param callback - Function called when storage data changes
* @returns A function that removes the listener when called
*/
static addChangedListener(callback) {
chrome.storage.onChanged.addListener(callback);
return () => chrome.storage.onChanged.removeListener(callback);
}
}
exports.StorageSync = StorageSync;