-
-
Save DarkPointer/4b96f41355dda2bc4be1747322ed7124 to your computer and use it in GitHub Desktop.
Detect if the browser is running in Private mode (Promise based)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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