2010-09-03 64 views
3

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

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

现在,经过重新加载页面或应用程序启动时,如果用户将插件,我要盒子的是Y.所以为了此目的位置,我应该使用Firefox的偏好的事情或有任何其他更好的办法做到这一点。

回答

1

有人建议我通过Nickolay波诺马廖夫,使用以下可以是一个很好的选择:

  • 注解服务
  • 的SQLite数据库

现在我打算使用数据库用法。

+0

难道我建议为了存储单个标志?如果是这样,oops:对于这种用例来说,这绝对是重量级的。顺便说一下,现在正在讨论针对此特定情况的解决方案:在dev.platform上的Gecko中存储https://groups.google.com/d/topic/mozilla.dev.platform/vYbQqkqGzlo/discussion和https:/ /bugzilla.mozilla.org/show_bug.cgi?id=866238 – Nickolay 2013-05-01 01:36:35

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