Can detect if visitor uses private browsing or not. Test in a private navigation to validate the concept ;)
Created
June 19, 2019 07:55
-
-
Save dedenker/3960c035c15a2d6e15f4ceed90df9813 to your computer and use it in GitHub Desktop.
Detect Private Browsing
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
<div id="result"></div> |
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
function retry(isDone, next) { | |
var current_trial = 0, | |
max_retry = 50, | |
interval = 10, | |
is_timeout = false; | |
var id = window.setInterval( | |
function() { | |
if (isDone()) { | |
window.clearInterval(id); | |
next(is_timeout); | |
} | |
if (current_trial++ > max_retry) { | |
window.clearInterval(id); | |
is_timeout = true; | |
next(is_timeout); | |
} | |
}, | |
10 | |
); | |
} | |
function isIE10OrLater(user_agent) { | |
var ua = user_agent.toLowerCase(); | |
if (ua.indexOf('msie') === 0 && ua.indexOf('trident') === 0) { | |
return false; | |
} | |
var match = /(?:msie|rv:)\s?([\d\.]+)/.exec(ua); | |
if (match && parseInt(match[1], 10) >= 10) { | |
return true; | |
} | |
return false; | |
} | |
function detectPrivateMode(callback) { | |
var is_private; | |
if (window.webkitRequestFileSystem) { | |
window.webkitRequestFileSystem( | |
window.TEMPORARY, 1, | |
function() { | |
is_private = false; | |
}, | |
function(e) { | |
console.log(e); | |
is_private = true; | |
} | |
); | |
} else if (window.indexedDB && /Firefox/.test(window.navigator.userAgent)) { | |
var db; | |
try { | |
db = window.indexedDB.open('test'); | |
} catch (e) { | |
is_private = true; | |
} | |
if (typeof is_private === 'undefined') { | |
retry( | |
function isDone() { | |
return db.readyState === 'done' ? true : false; | |
}, | |
function next(is_timeout) { | |
if (!is_timeout) { | |
is_private = db.result ? false : true; | |
} | |
} | |
); | |
} | |
} else if (isIE10OrLater(window.navigator.userAgent)) { | |
is_private = false; | |
try { | |
if (!window.indexedDB) { | |
is_private = true; | |
} | |
} catch (e) { | |
is_private = true; | |
} | |
} else if (window.localStorage && /Safari/.test(window.navigator.userAgent)) { | |
try { | |
window.localStorage.setItem('test', 1); | |
} catch (e) { | |
is_private = true; | |
} | |
if (typeof is_private === 'undefined') { | |
is_private = false; | |
window.localStorage.removeItem('test'); | |
} | |
} | |
retry( | |
function isDone() { | |
return typeof is_private !== 'undefined' ? true : false; | |
}, | |
function next(is_timeout) { | |
callback(is_private); | |
} | |
); | |
} | |
detectPrivateMode( | |
function(is_private) { | |
document.getElementById('result').innerHTML = typeof is_private === 'undefined' ? 'cannot detect' : is_private ? 'private <span>👻</span>' : 'not private <span>🖐️</span>'; | |
} | |
); |
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
body{ | |
padding-top: 2rem; | |
text-align: center; | |
} | |
@keyframes hi { | |
0% { transform: rotate(20deg); } | |
50% { transform: rotate(0deg); } | |
100% { transform: rotate(20deg); } | |
} | |
#result{ | |
font-family: monospace; | |
position: relative; | |
font-size: 20px; | |
} | |
#result span{ | |
transform-origin: center bottom; | |
position: absolute; | |
top: -2px; | |
animation: hi 1s linear 0s infinite; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment