开发者

Firefox Extension : Store Addon Data

I am building up a firefox extension which allows users to drag and drop things. Now if a user closes the application or reloads the page, I want to restore the last activity he did.

Example : User moves a box from point X to Y.
There can be many more boxes as well.

Now after page reload or application startup, if user puts the addon 开发者_运维技巧ON, I want the box's position to be Y. So for this purpose should I be using the firefox preference thing or is there any other better way of doing it.


I wrote a Firefox extension for accessing bookmarks from a site a friend of mine developed. I store the bookmarks data locally as JSON in a text file in the user's extension profile directory in case the bookmark service is down.

My function for saving bookmarks JSON is:

/**
 * Stores bookmarks JSON to local persistence asynchronously.
 *
 * @param bookmarksJson The JSON to store
 * @param fnSuccess The function to call upon success
 * @param fnError The function to call upon error
 */
RyeboxChrome.saveBookmarkJson = function(bookmarksJson, fnSuccess, fnError) {
    var cu = Components.utils;
    cu.import("resource://gre/modules/NetUtil.jsm");
    cu.import("resource://gre/modules/FileUtils.jsm");

    var bookmarksJsonFile = RyeboxChrome.getOrCreateStorageDirectory();
    bookmarksJsonFile.append("bookmarks.txt");

    // You can also optionally pass a flags parameter here. It defaults to
    // FileUtils.MODE_WRONLY | FileUtils.MODE_CREATE | FileUtils.MODE_TRUNCATE;
    var ostream = FileUtils.openSafeFileOutputStream(bookmarksJsonFile);

    var converter = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
    converter.charset = "UTF-8";
    var istream = converter.convertToInputStream(bookmarksJson);

    NetUtil.asyncCopy(istream, ostream, function(status) {
        if ( !Components.isSuccessCode(status) && typeof(fnError) === 'function' ) {
            fnError();
        } else if ( typeof(fnSuccess) === 'function' ) {
            fnSuccess();
        }
        return;
    });
};

The function for reading the data is:

/**
* Reads bookmarks JSON from local persistence asynchronously.
*
* @param fnSuccess Function to call when successful. The bookmarks JSON will
* be passed to this function.
*
* @param fnError Function to call upon failure.
*/
RyeboxChrome.getBookmarksJson = function(fnSuccess, fnError) {

    Components.utils.import("resource://gre/modules/NetUtil.jsm");

    var bookmarksJsonFile = RyeboxChrome.getOrCreateStorageDirectory();
    bookmarksJsonFile.append("bookmarks.txt");

    NetUtil.asyncFetch(bookmarksJsonFile, function(inputStream, status) {
        if (!Components.isSuccessCode(status) && typeof(fnError) === 'function' ) {
            fnError();
        } else if ( typeof(fnSuccess) === 'function' ){
            var data = NetUtil.readInputStreamToString(inputStream, inputStream.available());
            fnSuccess(data);
        }
    });
};

Finally, my getOrCreateStorageDirectory function is:

/**
 * Storage of data is done in a Ryebox directory within the user's profile
 * directory.
 */
RyeboxChrome.getOrCreateStorageDirectory = function() {

    var ci = Components.interfaces;

    let directoryService = Components.classes["@mozilla.org/file/directory_service;1"].getService(ci.nsIProperties);

    // Reference to the user's profile directory
    let localDir = directoryService.get("ProfD", ci.nsIFile);

    localDir.append("Ryebox");

    if (!localDir.exists() || !localDir.isDirectory()) {
        localDir.create(ci.nsIFile.DIRECTORY_TYPE, 0774);
    }

    return localDir;
};


It was suggested to me by Nickolay Ponomarev that using the following can be a good option:

  • Annotation Service
  • SQLite DB

Right now I am planning to use database for the usage.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜