2016-03-18 20 views
0

我試圖開發附加SDK擴展Firefox插件SDK擴展,使用localforage庫保存的數據,但我收到以下錯誤:如何開發Firefox中使用localforage

Full message: ReferenceError: self is not defined 
Full stack: [email protected]://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 
[email protected]://gre/modules/commonjs/sdk/addon/runner.js:147:19 

我使用npm安裝localforage。

我認爲這個問題可能是因爲localforage中的this issue。有沒有解決方法?

回答

0

This拉請求被合併到庫中,似乎已經解決了這個問題。

+0

這並沒有解決在Addon中使用'localforage'的問題。 'localforage'通過檢查像'typeof indexedDB!=='undefined''這樣的全局變量來檢查可用的存儲引擎,但'indexedDB'在插件的環境中不可用。它必須顯式地需要bu'var {indexedDB} = require('sdk/indexed-db');'。 – DUzun

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