【问题标题】:Firefox Extension : Store Addon DataFirefox 扩展:存储插件数据
【发布时间】:2011-04-07 22:49:00
【问题描述】:

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

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

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

【问题讨论】:

    标签: firefox firefox-addon


    【解决方案1】:

    Nickolay Ponomarev 向我建议,使用以下方法可能是一个不错的选择:

    • 注解服务
    • SQLite 数据库

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

    【讨论】:

    【解决方案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;
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多