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             // object
)

<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 may be primitive types (such as numbers, booleans, and strings) or Array 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

BCD tables only load in the browser

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)
}

// define 2 objects
let monster = {
  name: "Kraken",
  tentacles: true,
  eyeCount: 10
}

let kitten = {
  name: "Moggy",
  tentacles: false,
  eyeCount: 2
}

// store the objects
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);
Acknowledgements

This API is based on Chromium's chrome.storage API. This documentation is derived from storage.json in the Chromium code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:109 次

字数:4012

最后编辑:6 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文