-
-
Save b44rd/09e76bdf09753300f8263861cb508237 to your computer and use it in GitHub Desktop.
Detect private browsing mode (InPrivate Browsing or Incognito).
This file contains 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
export default () => { | |
return new Promise((resolve) => { | |
const on = () => resolve(true) // is in private mode | |
const off = () => resolve(false) // not private mode | |
const testLocalStorage = () => { | |
try { | |
if (window.localStorage.length) { | |
return off() | |
} else { | |
window.localStorage.x = 1 | |
window.localStorage.removeItem('x') | |
return off() | |
} | |
} catch (e) { | |
return navigator.cookieEnabled ? on() : off() | |
} | |
} | |
// Chrome & Opera | |
if (window.webkitRequestFileSystem) { | |
return void window.webkitRequestFileSystem(0, 0, off, on) | |
} | |
// Firefox | |
if ('MozAppearance' in document.documentElement.style) { | |
const db = window.indexedDB.open('test') | |
db.onerror = on | |
db.onsuccess = off | |
return void 0 | |
} | |
// IE10+ & Edge | |
if (!window.indexedDB && (window.PointerEvent || window.MSPointerEvent)) { | |
return on() | |
} | |
// Safari | |
if (/Safari/.test(window.navigator.userAgent) && navigator.cookieEnabled && window.localStorage) { | |
return testLocalStorage() | |
} | |
// others | |
return off() | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment