Skip to content

Instantly share code, notes, and snippets.

@Ema4rl
Forked from cou929/detect-private-browsing.js
Last active February 27, 2017 17:02
Show Gist options
  • Save Ema4rl/d49bda878dc26455c5e8609adb345005 to your computer and use it in GitHub Desktop.
Save Ema4rl/d49bda878dc26455c5e8609adb345005 to your computer and use it in GitHub Desktop.
Detect private browsing mode (InPrivate Browsing or Incognito).
"use strict";
// check out https://jsfiddle.net/Lj2fs5kr/
new Promise(function (resolve) {
var db = void 0,
on = function on() {
return resolve(true);
},
off = function off() {
return resolve(false);
},
tryls = function tryls() {
try {
localStorage.length ? off() : (localStorage.x = 1, localStorage.removeItem("x"), off());
} catch (e) {
on();
}
};
// Blink
window.webkitRequestFileSystem ? webkitRequestFileSystem(window.TEMPORARY, 1, off, on)
// FF
: "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 (enabled) {
document.body.innerHTML = enabled;
});
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<script src="detect-private-browsing.js"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment