Detecting / Preventing Firefox IndexedDB InvalidStateError caused by private browsing

I am integrating a javascript library that uses IndexedDB, but it "immodestly" does not work when Firefox is in private view / window mode. The library returns 500 Internal Error, and Firefox throws an InvalidStateError to the console.

What I would like to do is add a check before I create this library, and will not use the library at all if IndexedDB is not available. those. some type of try / catch test. From what I saw, Firefox seems to have spit out a console error, even if the violation code is inside try / catch (but maybe there is still a way ...).

Actually, I have no interest in whether the user is in a closed window session or not, but this is the only time Firefox raises this InvalidStateError.

+4
source share
2 answers

I used indexedDB to verify that the user is in private browsing mode. InvalidStateError appears on window.onerror and is registered through the tracking system. It seems that the discovery happened in another topic. I found only this primitive solution: install the global window.onerror handler to hide this error.

    // Get old handler (maybe undefined)
    const oldHandler = window.onerror;
    // Empty handler
    const noop = () => 1;
    window.onerror = noop;

    const returnOldHandler = () => setTimeout(() => {
      // The ugly thing: we some external code could place own onerror handler
      // between our code evaluation.
      // For this case we should check is it changed.
      if (window.onerror === noop) {
        window.onerror = oldHandler;
      }
    }, 0);

    try {
      db = window.indexedDB.open('test');
      // Return global handler when DB opens.
      // It can create some errors due async process.
      db.onerror = returnOldHandler;
      db.onsuccess = returnOldHandler;
    } catch(e) {
      // never evaluate
    }
0
source

You have errors in the onerror function.

, " ", , indexedDB - , - , FireFox , , - Mozilla .

var db = window.indexedDB.open('test');
db.onerror = function()
{
    console.log("Can't use indexedDB")
}

InvalidStateError , js .

0

All Articles