繁体   English   中英

如何使用localforage开发Firefox附加SDK扩展

[英]How to develop Firefox Add-on SDK extension using localforage

我正在尝试开发一个Firefox附加SDK扩展,该扩展使用localforage库保存数据,但是出现以下错误:

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中存在此问题 有解决方法吗?

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM