UNPKG

@stolbivi/pirojok

Version:

Some minimalistic library used to build chrome extensions, covers some popular Chrome Extension API

112 lines (111 loc) 4.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Storage = void 0; /** * A utility class for managing Chrome extension storage operations. * Provides methods for both sync and local storage operations. */ var Storage = /** @class */ (function () { function Storage() { } /** * Clears all data from Chrome's sync storage. * @returns A promise that resolves when the storage is cleared */ Storage.prototype.clearStorage = function () { return new Promise(function (resolve) { chrome.storage.sync.clear(function () { console.debug('Storage cleared'); resolve(); }); }); }; /** * Removes specific keys from Chrome's sync storage. * @param keys - A single key or array of keys to remove from storage * @returns A promise that resolves when the keys are removed */ Storage.prototype.removeFromStorage = function (keys) { return new Promise(function (resolve) { chrome.storage.sync.remove(keys, function () { console.debug('Removing from storage:', keys); resolve(); }); }); }; /** * Saves data to Chrome's sync storage. * @param data - The data object to store * @returns A promise that resolves with the stored data */ Storage.prototype.saveToStorage = function (data) { return new Promise(function (resolve) { chrome.storage.sync.set(data, function () { console.debug('Storage is updated:', data); resolve(data); }); }); }; /** * Reads data from Chrome's sync storage. * @param keys - A single key, array of keys, or object with default values * @returns A promise that resolves with the retrieved data */ Storage.prototype.readFromStorage = function (keys) { return new Promise(function (resolve) { console.debug('Reading storage for:', keys); chrome.storage.sync.get(keys, function (result) { return resolve(result); }); }); }; /** * Clears all data from Chrome's local storage. * @returns A promise that resolves when the storage is cleared */ Storage.prototype.clearLocalStorage = function () { return new Promise(function (resolve) { chrome.storage.local.clear(function () { console.debug('Storage cleared'); resolve(); }); }); }; /** * Removes specific keys from Chrome's local storage. * @param keys - A single key or array of keys to remove from storage * @returns A promise that resolves when the keys are removed */ Storage.prototype.removeFromLocalStorage = function (keys) { return new Promise(function (resolve) { chrome.storage.local.remove(keys, function () { console.debug('Removing from storage:', keys); resolve(); }); }); }; /** * Saves data to Chrome's local storage. * @param data - The data object to store * @returns A promise that resolves with the stored data */ Storage.prototype.saveToLocalStorage = function (data) { return new Promise(function (resolve) { chrome.storage.local.set(data, function () { console.debug('Storage is updated:', data); resolve(data); }); }); }; /** * Reads data from Chrome's local storage. * @param keys - A single key, array of keys, or object with default values * @returns A promise that resolves with the retrieved data */ Storage.prototype.readFromLocalStorage = function (keys) { return new Promise(function (resolve) { console.debug('Reading storage for:', keys); chrome.storage.local.get(keys, function (result) { return resolve(result); }); }); }; return Storage; }()); exports.Storage = Storage;