0

我正在尝试开发一个使用 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 中的这个问题。有解决方法吗?

4

2 回答 2

0

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

于 2016-03-22T08:01:54.553 回答
0

localforage 的作者不打算支持 Firefox 插件,但这并不意味着不可能。这是相关问题: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));
于 2016-07-02T22:27:57.630 回答