javascript – Что лучше: chrome.storage.sync, chrome.storage.local, или localStorage?

Question:

What is better to use Google Сhrome Extention extensions for storing data, and what is the fundamental difference between chrome.storage.sync / chrome.storage.local / localStorage , taking into account the possibility of obtaining this data in any tab?

Preferably with an example, please.

Answer:

localStorage is HTML5 data storage. Documentation https://developer.mozilla.org/en/docs/Web/API/Window/localStorage

localStorage stores data in the form: name = value (value is a string), that is, it does not allow storing compound objects. Moreover, types like true , false will be stored as "true" and "false" . Data are accessed using a synchronous method:

var x = localStorage.x;
var x = localStorage['x'];
var x = localStorage.getItem('x');

chrome.storage.local – extension data storage. Allows to store objects, unlike localStorage . Data is accessed asynchronously. Allows you to "listen" to changes in variables. Storage documentation https://developer.chrome.com/extensions/storage

chrome.storage.sync – works like chrome.storage.local , but stores data on the server, synchronized by google account between all authorized browsers.

For extensions, it is preferable to use chrome.storage local / sync – depends on your needs

Scroll to Top