Skip to content

Instantly share code, notes, and snippets.

@DarkPointer
Forked from jherax/is-private-mode.js
Created August 28, 2019 20:11
Show Gist options
  • Select an option

  • Save DarkPointer/4b96f41355dda2bc4be1747322ed7124 to your computer and use it in GitHub Desktop.

Select an option

Save DarkPointer/4b96f41355dda2bc4be1747322ed7124 to your computer and use it in GitHub Desktop.
Detect if the browser is running in Private mode (Promise based)
new Promise(function(resolve) {
var db,
on = function() { resolve(true) }, // is in private mode
off = function() { resolve(false) }, // not private mode
tryls = function() {
try {
localStorage.length ? off() : (localStorage.x = 1, localStorage.removeItem('x'), off());
} catch (e) {
// Safari only enables cookie in private mode
// if cookie is disabled then all client side storage is disabled
// if all client side storage is disabled, then there is no point
// in using private mode
navigator.cookieEnabled ? on() : off();
}
}
// Chrome & Opera
window.webkitRequestFileSystem ? webkitRequestFileSystem(0, 0, off, on)
// Firefox
: 'MozAppearance' in document.documentElement.style ? (db = indexedDB.open('test'), db.onerror = on, db.onsuccess = off)
// Safari
: (/constructor/i).test(window.HTMLElement) ? tryls()
// IE10+ & Edge
: !window.indexedDB && (window.PointerEvent || window.MSPointerEvent) ? on()
// Rest
: off();
}).then(function(isPrivateMode) {
console.log('Private mode is: ' + isPrivateMode)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment