admin管理员组文章数量:1435184
I'm integrating a javascript library that uses IndexedDB, but it fails "ungraciously" when Firefox is in private browsing/window mode. The library returns out a 500 Internal Error and meanwhile Firefox spits out an InvalidStateError to console.
What I'd like to do is add a check before I instantiate this library, and not use the library at all if IndexedDB is not available. i.e. some type of try/catch test. From what I've seen, Firefox appears to spit out the console error even if the offending code is inside a try/catch (but maybe there's still a way..).
I don't actually have an interest whether the user is in a private window session or not, but that seems to be the only time when Firefox causes this InvalidStateError.
I'm integrating a javascript library that uses IndexedDB, but it fails "ungraciously" when Firefox is in private browsing/window mode. The library returns out a 500 Internal Error and meanwhile Firefox spits out an InvalidStateError to console.
What I'd like to do is add a check before I instantiate this library, and not use the library at all if IndexedDB is not available. i.e. some type of try/catch test. From what I've seen, Firefox appears to spit out the console error even if the offending code is inside a try/catch (but maybe there's still a way..).
I don't actually have an interest whether the user is in a private window session or not, but that seems to be the only time when Firefox causes this InvalidStateError.
Share Improve this question asked Jul 1, 2015 at 10:55 rgarethrgareth 3,5465 gold badges26 silver badges36 bronze badges 4- Could you share the code snippet to help people debug? – Brian Commented Jul 1, 2015 at 10:58
- Can you provide more detail: When is throwing the error?. When opening the database?. When executing and instruction to an opened database? – borja gómez Commented Jul 1, 2015 at 11:05
- The library is PouchDB. Basically if you use it at all inside a Firefox private browsing window, you'll get an internal error returned plus InvalidStateError on the console. – rgareth Commented Jul 1, 2015 at 15:22
- This is perhaps by design: bugzilla.mozilla/show_bug.cgi?id=1150666 – rgareth Commented Jul 1, 2015 at 16:10
3 Answers
Reset to default 3You have handle the errors in the onerror function.
This won't tell you for definite that the use is "In Private", but will tell you you can't use indexedDB - from which you could interpolate if you needed to - ie if its FireFox and this throws and error then chances are they are In Private - until the guys at Mozilla fix it.
var db = window.indexedDB.open('test');
db.onerror = function()
{
console.log("Can't use indexedDB")
}
This still kicks out the InvalidStateError to the console, but the js can handle the consequences.
I used indexedDB to check is user in private browsing mode. The is InvalidStateError appears on window.onerror and logged via tracking system. It looks like opening happened in different thread. I've found only this primitive solution: set global handler window.onerror, 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
}
This is intentional due to privacy reason:
Note: In private browsing mode, most data storage is not supported. Local storage data and cookies are still stored, but they are ephemeral — the data is deleted when you close the last private browsing window.
版权声明:本文标题:javascript - Detectingpreventing Firefox IndexedDB InvalidStateError caused by private browsing - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745638401a2667701.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论