Get all keys from Chrome Storage

2019-02-04 08:08发布

问题:

I can get the value of a storage key with the Chrome Extension API:

chrome.storage.sync.get("someKey", function() {});

How can I get all key names that exist in the Chrome storage?

回答1:

From the documentation (emphasis mine):

An empty list or object will return an empty result object. Pass in null to get the entire contents of storage.

For some example code:

chrome.storage.sync.get(null, function(items) {
    var allKeys = Object.keys(items);
    console.log(allKeys);
});


回答2:

Just been messing about in Chrome with localstorage - it seems this will work too:

for(var i = 0; i < localStorage.length; i++)
{
    console.log(localStorage.key(i));
}