StorageArea.get()

Retrieves one or more items from the storage area.

This is an asynchronous function that returns a Promise.

Syntax

var gettingItem = browser.storage.<storageType>.get(
  keys    // null, string, or array of strings or objects
)

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

Parameters

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

Return value

A Promise that will be fulfilled with a results object containing every object in keys that was found in the storage area. If the operation failed, the promise will be rejected with an error message.

Browser compatibility

Chrome Edge Firefox Firefox for Android Opera
Basic support Yes Yes 45.0 48.0 33

Examples

Suppose storage contains two items:

// storage contains two items,
// "kittens" and "monsters"
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:

var 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
var gettingItem = browser.storage.local.get([]);
gettingItem.then(onGot, onError);

// -> Object { }

With the name of an object, retrieve the match:

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

// -> Object { kitten: Object }

With an array of object names, retrieve all matches:

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

// -> Object { kitten: Object, monster: 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.

Document Tags and Contributors

 Contributors to this page: wbamberg, Makyen, fiveNinePlusR, chrisdavidmills
 Last updated by: wbamberg,