Reputation: 1011
Morning,
My Firefox extension is going to produce, keep, and use some text-based data. For now, I'm not going to have it synchronized between Firefoxes on different computers. All I need is to write data to a text file and extract them. With that in mind, my questions are:
Where should I keep these data? I mean, in the <profile>/extensions
folder, I have my *.xpi
file (which is some kind of zip file) and don't have a dedicated folder for my extension. Do I need to create a special folder and keep my data in the text file within this folder? Or are there other, more appropriate ways to do it?
My data is going to be like this:
key1, value1, value2, value3, value4 key2, value1, value2, value3, value4 key3, value1, value2, value3, value4
Could you please recommend the best way to write/read these strings? Typical write/append/read in pure JavaScript, or using JSON
or something like that?
Thanks, Racoon
Upvotes: 1
Views: 229
Reputation: 752
Simplest way is to use localStorage. Firefox creates storage in profile folder and deletes it automatically once user uninstalls the addon. As a side effect you could reuse your code for chrome and safari extensions
Upvotes: 1
Reputation: 33162
It is common to keep data in the profile folder aka. ProfD
:
Components.utils.import("resource://gre/modules/FileUtils.jsm");
// get the "data.txt" file in the profile directory
var file = FileUtils.getFile("ProfD", ["data.txt"]);
Also see the File I/O Snippets page for more information.
If you have more than a couple of files, it would be a good idea to create a dedicated subfolder in ProfD
for your extension.
Do not keep data in extensions/
! This might confuse the add-on manager. Also, when creating unpacked extensions, do not keep data in the extension dir, as that folder will be wiped out during updates.
How you write the data is up to you. SQLite (Storage) might be overkill for a couple of strings, but great if you have lots of data (multi-megabyte) and also desire fast random lookups. A plain text file, or JSON.stringify
encoded data might be better. Also, if you data is "small" enough, you may use the Preferences Service instead and avoid having to mess around with actual files.
BTW: You can read back text (JSON) and/or xml data from the file system quite easily using XMLHttpRequest
s (but you cannot use it to write data, of course).
SDK add-ons may still use the File I/O stuff from above (the SDK io/file
module is too limited at the moment to use IMO). The SDK also offers dedicated indexed-db
and simple-prefs
modules.
Upvotes: 3
Reputation: 2112
SQLite is built in to recent versions of Firefox for use as a data-storage mechanism. More information, and examples of how to use the FF wrapper API, can be found on MDC.
Another option could be to use Local nsIFiles for persisting the data.
Upvotes: 1