Search code examples
firefoxlocal-storagefirefox-addonkey-valuefirefox-addon-webextensions

Firefox Add-on: What is the Best Way to Store an Array that Changes thru Time?


I have a Firefox Add-on that needs to persist an array of Strings.

The Strings are simple strings - Filenames to be exact, such as:

11111.jpg
22222.gif
33333.jpg
44444.jpg
55555.png

This array is dynamic - I need to Add items to it from time to time, and also Delete some.

I am used to Relational Databases, with SQL Queries for performing operations on the data,
but for a Firefox Add-on, it seems that the way to store data is using Key-Value pairs.

So for storing my (dynamically growing/shrinking) array,
does this mean that I need to store the whole array as the Value of the Key-Value pair,
and then every time I want to Add/Delete an item from that Array, I need to:
1) Get that Key-Value pair,
2) Edit the Array (in memory),
3) Save it all back to the Storage area?

Isn't there a better way than reading everything, doing the small change, and then saving everything again?


Solution

  • Storing the array under one key, reading it, updating, writing under the same key is the only straightforward method of using the extension storage API.

    In case you see it being noticeably slow in Firefox profiler, you can reorganize the data so it's stored in smaller buckets. You can even make your own complicated db imitation on top of extension storage API which would store each value separately and keep an index key that lists every value so you would only perform a full update of this index in addition to writing the single value itself. The index can be even in chunks or use other advanced structures. Maybe there's even an existing library.

    You can use a DOM storage like IndexedDB inside your background script (or any other extension script except the content scripts) which offers some of proper DB features, but it's less reliable because many users a) are using various cleaners that also purge DOM storage inside extensions, b) Firefox disables DOM storage inside extensions when it's set to global private mode or when cookies are disabled, c) IndexedDB in extensions is known to have inexplicable bugs that cause data loss. Libraries like PouchDB would be very helpful here.