Firefox扩展:存储插件数据

时间:2010-09-03 16:25:26

标签: firefox firefox-addon

我正在构建一个firefox扩展,允许用户拖放东西。现在,如果用户关闭应用程序或重新加载页面,我想恢复他所做的最后一个活动。

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

现在在页面重新加载或应用程序启动后,如果用户打开插件,我希望盒子的位置为Y.所以为此目的,我应该使用firefox首选项还是有其他更好的方法。

2 个答案:

答案 0 :(得分:2)

我写了一个Firefox扩展程序,用于从我的朋友开发的网站访问书签。我将书签数据作为JSON存储在用户的扩展配置文件目录中的文本文件中,以防书签服务中断。

我保存书签JSON的功能是:

/**
 * 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;
    });
};

读取数据的功能是:

/**
* 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);
        }
    });
};

最后,我的getOrCreateStorageDirectory函数是:

/**
 * 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;
};

答案 1 :(得分:1)

Nickolay Ponomarev向我建议使用以下内容可能是个不错的选择:

  • 注释服务
  • SQLite DB

现在我打算使用数据库进行使用。