Storage
MV2
MV3
Chrome
Firefox
Safari
Overview
@webext-core/storage
provides a type-safe, localStorage
-like API for interacting with extension storage.
ts
const { key: value } = await browser.storage.local.get('key');
// VS
const value = await localExtStorage.getItem('key');
WARNING
Requires the storage
permission.
Installation
NPM
sh
pnpm i @webext-core/storage
ts
import { localExtStorage } from '@webext-core/storage';
const value = await localExtStorage.getItem('key');
await localExtStorage.setItem('key', 123);
CDN
sh
curl -o storage.js https://cdn.jsdelivr.net/npm/@webext-core/storage/lib/index.global.js
html
<script src="/storage.js"></script>
<script>
const { localExtStorage } = webExtCoreStorage;
const value = await localExtStorage.getItem('key');
await localExtStorage.setItem('key', 123);
</script>
Differences with localStorage
and browser.storage
@webext-core/storage | localStorage | browser.storage | |
---|---|---|---|
Set value to undefined removes it? | ✅ | ✅ | ❌ |
Returns null for missing values? | ✅ | ✅ | ❌ |
Can store values of any type? | ✅ | ❌ | ✅ |
Async? | ✅ | ❌ | ✅ |
Otherwise, the storage behaves the same as localStorage
/ sessionStorage
.