6

For example let us say that we want to save a list of URLs and this list is updated dynamically. The following are the ways that I see this can be done.
1) Save in a text file
2) Save in a SQLite data base
3) Save in a preference (I know preferences are for storing preferences but is there any drawback of using a preference?)

What is the best method to save data related to a Firefox add-on? If a generic answer is not possible, what is the best method for a situation like the example above? And why do you think that such a method is appropriate and others are not?

Can't Tell
  • 12,714
  • 9
  • 63
  • 91

2 Answers2

9

Preferences are simple and lightweight, but your storage requirements could outgrow preferences. SQLite is good for larger sets of data that need fast query capabilities. I find using file-based storage simple and effective. Just make sure you use async I/O whenever possible.

Saving the data to a JSON (or other format) text file is a simple thing to do. The Firefox session store does the same thing. When saving, use the profile folder location, maybe even make a sub folder.

MDN has notes on finding the profile folder and reading/writing a text file:

You can use the nsIJSON component in older releases or the built-in JSON object in current releases of Firefox: https://developer.mozilla.org/en/JSON

Jonah Bishop
  • 12,279
  • 6
  • 49
  • 74
Mark Finkle
  • 966
  • 4
  • 5
  • 1
    It is recommended to use OS.File for file reading and writing now: [OS.File](https://developer.mozilla.org/en-US/docs/JavaScript_OS.File/OS.File_for_the_main_thread) – Noitidart Mar 22 '14 at 19:25
1

simple-storage

The simple storage module exports an object called storage that is persistent and private to your add-on. It's a normal JavaScript object, and you can treat it as you would any other.

To store a value, just assign it to a property on storage:

var ss = require("sdk/simple-storage");
ss.storage.myArray = [1, 1, 2, 3, 5, 8, 13];
ss.storage.myBoolean = true;
ss.storage.myNull = null;
ss.storage.myNumber = 3.1337;
ss.storage.myObject = { a: "foo", b: { c: true }, d: null };
ss.storage.myString = "O frabjous day!";

You can store array, boolean, number, object, null, and string values. If you'd like to store other types of values, you'll first have to convert them to strings or another one of these types.

EpokK
  • 38,062
  • 9
  • 61
  • 69