StorageArea.set()
Stores one or more items in the storage area, or update existing items.
When you store or update a value using this API, the storage.onChanged
event will fire.
Note that when storing items in the sync
storage area, the browser enforces quotas on the amount of data each extension can store. See Storage quotas for sync data.
This is an asynchronous function that returns a Promise
.
Syntax
let settingItem = browser.storage.<storageType>.set(
keys
)
<storageType>
will be one of the writable storage types — storage.sync
or storage.local
.
Parameters
-
keys
-
An object containing one or more key/value pairs to be stored in storage. If an item already exists, its value will be updated.
Values can be primitive (such as a number, boolean, or string), Array
, or Object
types.
It's generally not possible to store other types, such as Function
, Date
, RegExp
, Set
, Map
, ArrayBuffer
, and so on. Some of these unsupported types will restore as an empty object, and some cause set()
to throw an error. The exact behavior here is browser-specific.
Return value
A Promise
that will be fulfilled with no arguments if the operation succeeded. If the operation failed, the promise will be rejected with an error message.
Browser compatibility
|
Desktop |
Mobile |
|
Chrome |
Edge |
Firefox |
Internet Explorer |
Opera |
Safari |
WebView Android |
Chrome Android |
Firefox for Android |
Opera Android |
Safari on IOS |
Samsung Internet |
set |
Yes |
14storage is limited to 1MB per value.
|
45 |
? |
33 |
14 |
? |
? |
48 |
? |
15 |
? |
Examples
function setItem() {
console.log("OK");
}
function gotKitten(item) {
console.log(`${item.kitten.name} has ${item.kitten.eyeCount} eyes`);
}
function gotMonster(item) {
console.log(`${item.monster.name} has ${item.monster.eyeCount} eyes`);
}
function onError(error) {
console.log(error)
}
let monster = {
name: "Kraken",
tentacles: true,
eyeCount: 10
}
let kitten = {
name: "Moggy",
tentacles: false,
eyeCount: 2
}
browser.storage.local.set({kitten, monster})
.then(setItem, onError);
browser.storage.local.get("kitten")
.then(gotKitten, onError);
browser.storage.local.get("monster")
.then(gotMonster, onError);