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

时间:2016-03-18 17:48:47

标签: javascript firefox firefox-addon-sdk 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。有解决方法吗?

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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