API Reference
defineExtensionStorage
function defineExtensionStorage<TSchema extends AnySchema = AnySchema>(
storage: Storage.StorageArea,
): ExtensionStorage<TSchema> {
// ...
}
Create a storage instance with an optional schema, TSchema
, for type safety.
Parameters
storage: Storage.StorageArea
The storage to to use. EitherBrowser.storage.local
,Browser.storage.sync
, orBrowser.storage.managed
.
Examples
import browser from 'webextension-polyfill';
interface Schema {
installDate: number;
}
const extensionStorage = defineExtensionStorage<Schema>(browser.storage.local);
const date = await extensionStorage.getItem("installDate");
ExtensionStorage
interface ExtensionStorage<TSchema extends AnySchema> {
clear(): Promise<void>;
getItem<TKey extends keyof TSchema>(
key: TKey,
): Promise<Required<TSchema>[TKey] | null>;
setItem<TKey extends keyof TSchema>(
key: TKey,
value: TSchema[TKey],
): Promise<void>;
removeItem<TKey extends keyof TSchema>(key: TKey): Promise<void>;
onChange<TKey extends keyof TSchema>(
key: TKey,
cb: OnChangeCallback<TSchema, TKey>,
): RemoveListenerCallback;
}
This is the interface for the storage objects exported from the package. It is similar to localStorage
, except for a few differences:
- It's async since the web extension storage APIs are async.
- It can store any data type, not just strings.
localExtStorage
const localExtStorage: ExtensionStorage<AnySchema>;
An implementation of ExtensionStorage
based on the browser.storage.local
storage area.
managedExtStorage
const managedExtStorage: ExtensionStorage<AnySchema>;
An implementation of ExtensionStorage
based on the browser.storage.managed
storage area.
sessionExtStorage
const sessionExtStorage: ExtensionStorage<AnySchema>;
An implementation of ExtensionStorage
based on the browser.storage.local
storage area.
- Added to Chrome 102 as of May 24th, 2022.
- Added to Safari 16.4 as of March 27th, 2023.
- Added to Firefox 115 as of July 4th, 2023.
syncExtStorage
const syncExtStorage: ExtensionStorage<AnySchema>;
An implementation of ExtensionStorage
based on the browser.storage.sync
storage area.
API reference generated by docs/generate-api-references.ts