我们的志愿者还没有将这篇文章翻译为 中文 (简体)。加入我们帮助完成翻译!
Retrieves one or more items from the storage area.
Syntax
chrome.storage.<storageType>.get( keys, // null, string, or array of strings or objects function(results){...} // function )
This API is also available as browser.storage.<storageType>.get()
in a version that returns a promise.
<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. callback
- A callback function that is run when the operation completes. If the operation is successful, the function is passed a
results
object containing every object inkeys
that was found in the storage area. If the operation fails,runtime.lastError
is set.
Browser compatibility
Chrome | Edge | Firefox | Firefox for Android | Opera | |
---|---|---|---|---|---|
Basic support | Yes | Yes | 45.0 | 48.0 | 33 |
Examples
// callback for get() function gotItem(item) { if (chrome.runtime.lastError) { console.log(chrome.runtime.lastError); } else { console.log(item); } } // storage contains two items, // "kittens" and "monsters" chrome.storage.local.set({ kitten: {name:"Mog", eats:"mice"}, monster:{name:"Kraken", eats:"people"} }); // with no `keys` argument, retrieve everything chrome.storage.local.get(gotItem); // -> Object { kitten: Object, monster: Object } // with an empty array, retrieve nothing chrome.storage.local.get([], gotItem); // -> Object { } // with the name of an object, retrieve the match chrome.storage.local.get("kitten", gotItem); // -> Object { kitten: Object } // with an array of object names, retrieve all matches chrome.storage.local.get(["kitten", "monster", "grapefruit"], gotItem); // -> Object { kitten: Object, monster: Object }
Example add-ons
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.