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
.
let settingItem = browser.storage.<storageType>.set(
keys
// object
)
<storageType>
will be one of the writable storage types —
storage.sync
or
storage.local
.
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),
数组
,或
对象
类型。
It's generally not possible to store other types, such as
函数
,
日期
,
RegExp
,
Set
,
地图
,
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.
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.
BCD tables only load in the browser
function setItem() {
console
.
log
(
"OK"
)
;
}
function
gotKitten
(
item
)
{
console
.
log
(
`
${
item
.
kitten
.
名称
}
has
${
item
.
kitten
.
eyeCount
}
eyes
`
)
;
}
function
gotMonster
(
item
)
{
console
.
log
(
`
${
item
.
monster
.
名称
}
has
${
item
.
monster
.
eyeCount
}
eyes
`
)
;
}
function
onError
(
error
)
{
console
.
log
(
error
)
}
// define 2 objects
let
monster
=
{
名称
:
"Kraken"
,
tentacles
:
true
,
eyeCount
:
10
}
let
kitten
=
{
名称
:
"Moggy"
,
tentacles
:
false
,
eyeCount
:
2
}
// store the objects
浏览器
.
storage
.
local
.
set
(
{
kitten
,
monster
}
)
.
then
(
setItem
,
onError
)
;
浏览器
.
storage
.
local
.
get
(
"kitten"
)
.
then
(
gotKitten
,
onError
)
;
浏览器
.
storage
.
local
.
get
(
"monster"
)
.
then
(
gotMonster
,
onError
)
;
注意:
This API is based on Chromium's
chrome.storage
API. This documentation is derived from
storage.json
in the Chromium code.
最后修改: , 由 MDN 贡献者