I'm developing Firefox add-on which has some content scripts to save data to IndexedDB. Same code works perfectly fine in Chrome extension, but not in Firefox extension. On Firefox everything works fine until part where data has to be written to database.
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")]
});
content-script.js // part where it doesn't work in 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);
});
}
I checked in Storage Inspector too, nothing is added to database. One more detail: I think that problem may be caused by script loading because I defined at start of content-script.js to load everything when DOM is ready (maybe, but I'm not sure if it's caused by that, I tried "start" , "ready" and "end" in contentScriptWhen parameter).
document.addEventListener("DOMContentLoaded", function(event) {
Everything in content-script.js is inside this event listener.
IndexedDB on Chrome is fully supported on 23-106, partially supported on 11-22, and not supported on 4-10 Chrome versions. IndexedDB on Safari is fully supported on 10-16, partially supported on 7.1-14.1, and not supported on 3.2-7 Safari versions.
IndexedDB data belong to a type of temporary. So these data can be wiped out at any time. These data size/lifetime are managed by very new Quota Management API. In the future, IndexedDB could possibly used persistance type (not likely and not good idea too).
More specifically, IndexedDB data is stored in the browser profile folder. For some browsers, there is not a one-to-one relationship between users and profiles.
Dexie will by default use the indexedDB from window or self. In a firefox add-ons are not running in a window so probably Dexie doesn't find it. In Dexie v1.3.6, the indexedDB API can be provided in the constructor.
Try the latest Dexie v1.3.6 and do:
var idb = require('sdk/indexed-db');
var db = new Dexie("myDatabase", {
indexedDB: idb.indexedDB,
IDBKeyRange: idb.IDBKeyRange
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With