7

I am working on a Chrome extension that needs to save some information (tabs info mainly) that will exist throughout the lifetime of the extension (e.g , since the user starts using it until he closes the browser).

One option is to use localstorage, however localstorage can only save Strings and that makes it very uncomfortable for me (since I have a bunch of data to save - dates , URLs , integers etc). What I'm looking for is using my own javascript objects that will live throughout the time of the extension.
Now the problem is that defining these objects in a script in some javascript files will erase them each time the user clicks on the browser action . In other words I have a browser action called popup.html that includes a javascript file (in which I want to save my objects) and every time the user clicks on the browser action all the objects I defined in the JS file are lost, yet I want everything to be persisted .

What option do I have to keep data that persists through many clicks on the browser action and that is NOT localstorage?

Forge
  • 6,538
  • 6
  • 44
  • 64
Joel Blum
  • 7,750
  • 10
  • 41
  • 60
  • 1
    So, do you want to remember data for the current session (till the browser is closed)? In that case, just create the objects in the [background page](https://code.google.com/chrome/extensions/background_pages.html). – Rob W May 28 '12 at 15:47
  • 1
    or you can use [indexedDB](https://developer.mozilla.org/en/IndexedDB/Using_IndexedDB) – gengkev May 28 '12 at 18:48
  • 1
    These days you can use chrome.storage which automagically does serialize/deserialize FWIW: http://stackoverflow.com/a/24281357/32453 – rogerdpack Oct 25 '16 at 15:26
  • You should use [`chrome.storage`](https://developer.chrome.com/apps/storage) in extensions, not other types of storage – fregante Jul 25 '19 at 19:57

3 Answers3

3

You really should use localStorage (you may use JSON.stringify and JSON.parse).

If you really don't want to use localStorage nor a server side storage, use IndexedDb : https://developer.mozilla.org/en/IndexedDB/Using_IndexedDB

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • I will fall back to localStorage if there is no other option . Do you know if a background page can be used to solve my problem ? I can't completely understand what they do from the manuals ... – Joel Blum May 28 '12 at 15:40
  • You may use a background page, but it's probably not needed. Any script of your extension can read and write from/to localStorage. – Denys Séguret May 28 '12 at 15:42
2

Try using the Filesystem API's persistent storage. That is more reliable than localStorage. localStorage will be cleared once the user clears the cache, cookies etc. Filesystem is more reliable.

Jophin Joseph
  • 2,864
  • 4
  • 27
  • 40
  • 2
    Why is it a good idea to persist data even though the user explicitly deletes cookies etc.? Would you as a user want that behavior? This is particularly strange given that @Joel_Blum is asking for something that persists only during the user's session. – steinar May 31 '13 at 01:22
  • Unfortunately it appears the FileSystem API wasn't adopted by other browsers than Chrome and was basically "replaced" by LocalStorage – rogerdpack Oct 25 '16 at 15:29
1

This answer about using chrome.storage worked for me far better than the others.

https://stackoverflow.com/a/14009240/766115

It's like localStorage, but works between the popup / background and content scripts.

Community
  • 1
  • 1
JTE
  • 1,301
  • 12
  • 14