Nik*_*ola 6 javascript firefox firefox-addon indexeddb dexie
我正在开发Firefox附加组件,它有一些内容脚本可以将数据保存到IndexedDB.相同的代码在Chrome扩展程序中完美运行,但在Firefox扩展程序中则不行.在Firefox上,一切正常,直到必须将数据写入数据库的部分.
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")]
});
Run Code Online (Sandbox Code Playgroud)
content-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);
});
}
Run Code Online (Sandbox Code Playgroud)
我也检查了Storage Inspector,没有任何内容添加到数据库中.还有一个细节:我认为这个问题可能是由脚本加载引起的,因为我在content-script.js的开头定义了在DOM准备就绪时加载所有东西(也许,但我不确定它是否是由它造成的,我试过了"在contentScriptWhen参数中启动","ready"和"end" .
document.addEventListener("DOMContentLoaded", function(event) {
Run Code Online (Sandbox Code Playgroud)
content-script.js中的所有内容都在此事件侦听器中.
默认情况下,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
});
Run Code Online (Sandbox Code Playgroud)