如何使用 localforage 开发 Firefox Add-on SDK 扩展

How to develop Firefox Add-on SDK extension using localforage

我正在尝试开发一个使用 localforage 库保存数据的 Firefox Add-on SDK 扩展,但出现以下错误:

Full message: ReferenceError: self is not defined
Full stack: polyfill@resource://browser-journey/node_modules/localforage/dist/localforage.js:259:9
@resource://browser-journey/node_modules/localforage/dist/localforage.js:689:1
@resource://browser-journey/node_modules/localforage/dist/localforage.js:7:2
@resource://browser-journey/index.js:6:19
run@resource://gre/modules/commonjs/sdk/addon/runner.js:147:19

我使用 npm 安装了 localforage。

我认为问题可能是因为 localforage 中的 this issue。有解决方法吗?

This 拉取请求刚刚合并到库中,似乎已经解决了问题。

localforage 的作者不打算支持 Firefox Addons,但这并不意味着不可能。 这是相关问题:https://github.com/mozilla/localForage/issues/584

您可以为 localforage 编写自定义驱动程序:https://github.com/mozilla/localForage/pull/282

或者在 dist/localforage.min.js 文件的顶部添加此代码,然后再将其包含在您的插件中:

/**
 * Detect Firefox SDK Addon environment and prepare some globals
 *
 * @author Dumitru Uzun (DUzun.Me)
 */
(function (window, undefined) {
    if (typeof exports != "object" || typeof module == "undefined") return;

    if ( window && window.Array === Array ) try {
        window.window = window;

        // Timers
        if ( typeof setTimeout == 'undefined' ) {
            expo(
                require("sdk/timers")
              , [
                    'setTimeout'  , 'clearTimeout',
                    'setImmediate', 'clearImmediate'
                ]
            );
        }

        // Blob, FileReader
        var Cu = require("chrome").Cu;
        var Services = Cu['import']("resource://gre/modules/Services.jsm", {});
        expo(
            Services
          , ['Blob', 'FileReader']
        );
        expo(
            Services.appShell && Services.appShell.hiddenDOMWindow
          , ['Blob', 'FileReader']
        );

        // IndexedDB
        expo(require('sdk/indexed-db'));

    } catch(err) {
        console.log('error', err);
    }

    function expo(ctx, props) {
        if ( !ctx ) return;
        if ( !props ) props = Object.keys(ctx);
        for(var i=props.length,p; i--;) {
            p = props[i];
            if ( ctx[p] != undefined && !(p in window) ) {
              window[p] = ctx[p];
            }
        }
        return ctx;
    }
}(this));