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

1

6

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.

Acnode answered 20/2, 2016 at 14:11 Comment(2)
do you get any errors in the developer tools console? Are you using JPM extension, or WebExtensions in firefox?Stephan
@JaromandaX Nope, which is weird too, I don't get any errors related to this in console. I'm using JPM.Hallucinogen
A
1

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
});
Actino answered 11/4, 2016 at 9:56 Comment(3)
Thanks for answer. It still doesn't work, but at least I finally get an error when I try to add/get data: MissingAPIError: indexedDB API not found. If using IE10+, make sure to run your code on a server URL (not locally). If using Safari, make sure to include indexedDB polyfill. I'm really confused now, it should work on FF 47...Hallucinogen
Are you sure you are using Dexie v1.3.6 and that you spell indexedDB correctly, including lowers and uppers?Actino
Yes, I have downloaded latest version of Dexie from GitHub and checked whole code twice. Just to be sure - first line goes to index.js and part with db variable goes to my content script?Hallucinogen

© 2022 - 2024 — McMap. All rights reserved.