Search code examples
firebase-realtime-databasetampermonkey

Firebase realtime database once method doesn't work in web extensions


i'm using Firebase API to anonymously login and get data from Tampermonkey extension. Works as charm in Chrome but doesn't work in Firefox at all.

firebase.auth().signInAnonymously().catch(function(error) {
    console.log("login error: "+ error);
});

firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
    firebase.database().ref().once("value").then(function(snapshot) {
        debugger; // never gets here
        GM_setValue("lastUpdate", new Date());
        GM_setValue("lastState", snapshot.val());
    }).catch(function(error) {
        debugger; // never gets here
        console.log("error reading DB: " + error);
    });
  }
});

it never comes to debugger in Firefox. No errors in console at all. what could be a reason? GM_xmlhttprequest instead works more or less in both, but i need auth as well so wanted to use official API.

thank you

UPDATE: after recent Chrome update it doesn't work there as well. Silently dies somewhere...


Solution

  • it turns that there is a value in localStorage that preventing everything from working. code below at application init solves issue.

    if (localStorage) { // fix for API
        localStorage.removeItem("firebase:previous_websocket_failure");
    }
    

    something sets it to true and nobody is cleaning it. there are issues about it since 2017 on github about this, seems not resolved yet.