StorageArea.get() 编辑

Retrieves one or more items from the storage area.

This is an asynchronous function that returns a Promise.

Syntax

let results = browser.storage.<storageType>.get(
  keys    // null, string, object or array of strings
)

<storageType> will be one of the writable storage types — sync, local, or managed.

Parameters

keys
A key (string) or keys (an array of strings, or an object specifying default values) to identify the item(s) to be retrieved from storage. If you pass an empty string, object or array here, an empty object will be retrieved. If you pass null, or an undefined value, the entire storage contents will be retrieved.

Return value

A Promise that resolves to a results object, containing every object in keys that was found in the storage area. If keys is an object, keys that are not found in the storage area will have their values given by the keys object.

If the operation failed, the promise is rejected with an error message.

If managed storage is not set, undefined will be returned.

Caution: When used within a content script in Firefox versions prior to 52, the Promise returned by browser.storage.local.get() is fulfilled with an Array containing one Object. The Object in the Array contains the keys found in the storage area, as described above.

The Promise is correctly fulfilled with an Object when used in the background context (background scripts, popups, options pages, etc.).

When this API is used as chrome.storage.local.get(), it correctly passes an Object to the callback function.

Browser compatibility

BCD tables only load in the browser

Examples

Suppose storage contains two items:

// storage contains two items,
// "kitten" and "monster"
browser.storage.local.set({
  kitten:  {name:"Mog", eats:"mice"},
  monster: {name:"Kraken", eats:"people"}
});

Define success and failure handlers for the promise:

function onGot(item) {
  console.log(item);
}

function onError(error) {
  console.log(`Error: ${error}`);
}

With no keys argument, retrieve everything:

let gettingItem = browser.storage.local.get();
gettingItem.then(onGot, onError);

// -> Object { kitten: Object, monster: Object }

With an empty keys argument, return nothing:

// with an empty array, retrieve nothing
let gettingItem = browser.storage.local.get([]);
gettingItem.then(onGot, onError);

// -> Object { }

With the name of an object, retrieve the match:

let gettingItem = browser.storage.local.get("kitten");
gettingItem.then(onGot, onError);

// -> Object { kitten: Object }

With an array of object names, retrieve all matches:

let gettingItem = browser.storage.local.get(["kitten", "monster", "grapefruit"]);
gettingItem.then(onGot, onError);

// -> Object { kitten: Object, monster: Object } 

With an object with object names as keys and the default value as value:

let gettingItem = browser.storage.local.get({
  kitten: "no kitten",
  monster: "no monster",
  grapefruit: {
    name: "Grape Fruit",
    eats: "Water"
  }
});

// -> Object { kitten: Object, monster: Object, grapefruit: Object }

Chrome examples

chrome.storage.local.get("kitten", function(items){
  console.log(items.kitten);  // -> {name:"Mog", eats:"mice"}
});

Or with an arrow function

chrome.storage.local.get("kitten", items => {
  console.log(items.kitten); // -> {name:"Mog", eats:"mice"}
});

Or using a Promise

let gettingItem = new Promise(resolve => chrome.storage.local.get("kitten", resolve));
gettingItem.then(onGot); // -> Object { kitten: Object }
Acknowledgements

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

Microsoft Edge compatibility data is supplied by Microsoft Corporation and is included here under the Creative Commons Attribution 3.0 United States License.

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

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

发布评论

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

词条统计

浏览:75 次

字数:6329

最后编辑:7年前

编辑次数:0 次

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