StorageArea.get()
Retrieves one or more items from the storage area.
This is an asynchronous function that returns aPromise.
In this article
Syntax
let results = browser.storage.<storageType>.get( keys // null, string, object or array of strings)Where<storageType> is one of the storage types —sync,local,session, ormanaged.
Parameters
keysA 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 object or array here, an empty object will be retrieved. If you passnull, or an undefined value, the entire storage contents will be retrieved.
Return value
APromise that resolves to aresults object, containing a key-value pair for every key inkeys found in the storage area. Whenkeys is an object, any key that isn't found in storage takes the default value from thekeys object.
If the operation fails, the promise is rejected with an error message.
If managed storage is not set,undefined is returned.
Warning:In Firefox, if an extension's managed storage has not been configured with anative manifest or using the3rdparty enterprise policy, an exception is thrown when using this function to access managed storage (seeFirefox bug 1868153). This issue can be avoided by catching the error. This issue is related to the lack of support for thestorage.managed_schema manifest key (seeFirefox bug 1771731).
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 nokeys argument, retrieve everything:
let gettingItem = browser.storage.local.get();gettingItem.then(onGot, onError);// -> Object { kitten: Object, monster: Object }With an emptykeys argument, return nothing:
// with an empty array, retrieve nothinglet 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", (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 }Browser compatibility
Note:This API is based on Chromium'schrome.storage API. This documentation is derived fromstorage.json in the Chromium code.