2016-02-20 28 views
6

我正在開發Firefox附加組件,它有一些將數據保存到IndexedDB的內容腳本。相同的代碼在Chrome擴展中可以很好地工作,但不適用於Firefox擴展。在Firefox上一切正常,直到數據必須寫入數據庫的部分。來自Firefox附加組件的內容腳本無法寫入到IndexedDB中

index.js

var data = require("sdk/self").data; 
var pageMod = require("sdk/page-mod"); 
var { indexedDB } = require('sdk/indexed-db'); 

var request = indexedDB.open("myDatabase"); 

request.onerror = function(event) { 
    console.log("Failure."); 
}; 

request.onsuccess = function(event) { 
    console.log("Success."); 
}; 

pageMod.PageMod({ 
    include: "*", 
    contentScriptWhen: "start", 
     //contentScriptFile: ["./js/jquery.min.js", "./js/jquery-ui.min.js", "./js/Dexie.min.js", "./js/content-script.js"] 
    contentScriptFile: [data.url("js/jquery.min.js"), data.url("js/content-script.js"), data.url("js/jquery-ui.min.js"), data.url("js/Dexie.min.js")], 
    contentStyleFile: [data.url("css/jquery-ui.min.css")] 
}); 

內容的script.js // 部分地方無法在Firefox

function transition(location, time, date) { 

    var db = new Dexie("myDatabase"); 
    db.version(1).stores({ 
     likes: 'url, date, time' 
    }); 

    db.open(); 

    db.likes.add({url: location, date: date, time: time}).then (function(){ 

     alert("Informations are added."); 

    }).catch(function(error) { 
     alert("There's an error: " + error); 
    }); 

} 

工作,我在存儲檢查檢查也沒有任何東西被添加到數據庫中。還有一個細節:我認爲這個問題可能是由腳本加載導致的,因爲我在content-script.js的開始時定義了在DOM準備好時加載所有內容(也許,但我不確定是否是由於此原因造成的,我試過「開始「,」準備就緒「和」結束「contentScriptWhen參數)。

document.addEventListener("DOMContentLoaded", function(event) { 

內容的script.js一切都是這個事件監聽器裏。

+0

你得到的開發工具控制檯任何錯誤?您是使用JPM擴展還是Firefox中的WebExtensions? –

+0

@JaromandaX不好,這也很奇怪,我沒有在控制檯中得到任何與此相關的錯誤。我正在使用JPM。 – Nikola

回答

1

Dexie將默認使用window或self中的indexedDB。在Firefox中,加載項不在窗口中運行,因此Dexie可能找不到它。在Dexie v1.3.6中,可以在構造函數中提供indexedDB API。

試用最新Dexie v1.3.6做:

var idb = require('sdk/indexed-db'); 
var db = new Dexie("myDatabase", { 
    indexedDB: idb.indexedDB, 
    IDBKeyRange: idb.IDBKeyRange 
}); 
+0

感謝您的回答。它仍然不起作用,但至少當我嘗試添加/獲取數據時,我終於遇到錯誤: 'MissingAPIError:indexedDB API未找到。如果使用IE10 +,請確保在服務器URL(不是本地)上運行代碼。如果使用Safari,請確保包含indexedDB polyfill.' 我現在很困惑,它應該在FF 47上工作... ... – Nikola

+0

您確定您使用Dexie v1.3.6,並且正確拼寫indexedDB,包括降低和鞋面? –

+0

是的,我已經從GitHub下載了最新版本的Dexie,並且檢查了整個代碼兩次。可以肯定的是 - 第一行是_index.js_,部分數據庫變量會轉到我的_content script_? – Nikola

相關問題