Skip to content

Instantly share code, notes, and snippets.

@b44rd
Forked from cou929/detect-private-browsing.js
Last active September 1, 2017 10:44
Show Gist options
  • Save b44rd/09e76bdf09753300f8263861cb508237 to your computer and use it in GitHub Desktop.
Save b44rd/09e76bdf09753300f8263861cb508237 to your computer and use it in GitHub Desktop.
Detect private browsing mode (InPrivate Browsing or Incognito).
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