Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Content script from Firefox add-on doesn't write to IndexedDB

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.

like image 491
Nikola Stojaković Avatar asked Feb 20 '16 14:02

Nikola Stojaković


People also ask

Does IndexedDB work on all browsers?

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.

How long does IndexedDB last?

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

Where is IndexedDB stored?

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.


1 Answers

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
});
like image 197
David Fahlander Avatar answered Sep 23 '22 14:09

David Fahlander