I'm developing a Chrome extension and I need to store some data and then get it in some point. I did investigation on available storage
s and came across to the following ones: window.localStorage
and chrome.storage.local
.
So my question is, which one is the right choice to use in Chrome extensions:window.localStorage
or chrome.storage.local
?
P.S. I'm using browser action
to load a local HTML
in IFRAME
. So I'm not using popup.js
.
Supposedly, window. localStorage makes the localStorage faster to be found than just writing localStorage. Storing a reference to it on a variable makes it even faster. Anyway, these improvements are negligible on modern browsers.
It's simple. Just go to the developer tools by pressing F12 , then go to the Application tab. In the Storage section expand Local Storage. After that, you'll see all your browser's local storage there.
No. localStorage is accessible by any webpage, and if you have the key, you can change whatever data you want. That being said, if you can devise a way to safely encrypt the keys, it doesn't matter how you transfer the data, if you can contain the data within a closure, then the data is (somewhat) safe.
Many browser extensions store their data in the browser's so-called Local Storage, which is nothing else than a storage location managed by the web browser. And as the same suggests, all is saved locally on the machine where the browser is installed.
localStorage
Pros:
var value = localStorage[key]
Cons:
JSON.stringify
chrome.storage.local
Pros:
chrome.storage.onChanged
"unlimitedStorage"
permission, can hold arbitrarily large amounts of data.Has a nice built-in mechanism for default values:
chrome.storage.local.get({key: defaultValue}, function(value){/*...*/});
Cons:
Asynchronous, therefore a bit harder to work with:
chrome.storage.local.get("key", function(value){/* Continue here */});
chrome.storage.local.get(null)
to get all values or use something like Storage Area Explorer.chrome.storage.sync
Same as above, but:
Pros:
Cons:
As of 2016-11-06, not yet supported in either Firefox WebExtensions or Edge Extensions, so non-portable.
Note: storage.sync
is now FF WebExtension compatible, though there is no way to make Chrome and FF natively sync between each other.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With