-
-
Save kdzwinel/783df9b129ae5c8443dd96c0d4ed9723 to your computer and use it in GitHub Desktop.
Small collection of solutions used to detect private mode
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
// credit https://www.bostonglobe.com | |
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; | |
} | |
// MS Edge Detection from this gist: https://gist.github.com/cou929/7973956 | |
var edge = /edge/.exec(ua); | |
if (edge && edge[0] == "edge") { | |
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)) { | |
// One-off check for weird sports 2.0 polyfill | |
// This also impacts iOS Firefox and Chrome (newer versions), apparently | |
// @see bglobe-js/containers/App.js:116 | |
if (window.safariIncognito) { | |
is_private = true; | |
} else { | |
try { | |
window.openDatabase(null, null, null, null); | |
} catch (e) { | |
is_private = true; | |
} | |
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); | |
} | |
); | |
} |
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
// credit https://www.breakingviews.com/ | |
function incognitoBrowsers() { | |
return new Promise(function(resolve) { | |
var on = function() { | |
resolve(!0) | |
} | |
, off = function() { | |
resolve(!1) | |
}; | |
if (!window.webkitRequestFileSystem) { | |
if ('MozAppearance'in document.documentElement.style) { | |
var db = indexedDB.open('test'); | |
return db.onerror = on, | |
void (db.onsuccess = off) | |
} | |
if (/^((?!chrome|android).)*safari/i.test(navigator.userAgent)) { | |
var isPrivate = !1; | |
try { | |
window.openDatabase(null, null, null, null) | |
} catch (_) { | |
isPrivate = !0 | |
} | |
isPrivate ? on() : off() | |
} | |
return off() | |
} | |
window.webkitRequestFileSystem(0, 0, off, on) | |
}) | |
} |
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
/* eslint-disable consistent-return */ | |
let on; | |
let off; | |
const isSafari = () => { | |
if (Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0) { | |
return true; | |
} | |
if ( | |
// eslint-disable-next-line no-undef | |
(!window.safari || safari.pushNotification).toString() === '[object SafariRemoteNotification]' | |
) { | |
return true; | |
} | |
try { | |
return window.localStorage && /Safari/.test(window.navigator.userAgent); | |
} catch (e) { | |
return false; | |
} | |
}; | |
const isMozilla = () => 'MozAppearance' in document.documentElement.style; | |
const Webkit = () => { | |
if (window.webkitRequestFileSystem) { | |
window.webkitRequestFileSystem(window.TEMPORARY, 1, off, on); | |
return true; | |
} | |
}; | |
const Mozilla = () => { | |
if (isMozilla()) { | |
const db = indexedDB.open('test'); | |
db.onerror = on; | |
db.onsuccess = off; | |
return true; | |
} | |
}; | |
const Safari = () => { | |
if (isSafari()) { | |
// iOS 11 | |
// Origin: https://gist.github.com/cou929/7973956#gistcomment-2272103 | |
try { | |
window.openDatabase(null, null, null, null); | |
} catch (e) { | |
on(); | |
return true; | |
} | |
// Older Safari | |
try { | |
if (localStorage.length) off(); | |
else { | |
localStorage.x = 1; | |
localStorage.removeItem('x'); | |
off(); | |
} | |
} catch (e) { | |
// Original gist: https://gist.github.com/jherax/a81c8c132d09cc354a0e2cb911841ff1 | |
// 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(); // eslint-disable-line no-unused-expressions | |
} | |
return true; | |
} | |
}; | |
const IE10Edge = () => { | |
if (!window.indexedDB && (window.PointerEvent || window.MSPointerEvent)) { | |
on(); | |
return true; | |
} | |
}; | |
export const checkPrivate = (onCb, offCb) => { | |
on = onCb || (() => {}); | |
off = offCb || (() => {}); | |
Webkit() || Mozilla() || Safari() || IE10Edge() || off(); // eslint-disable-line no-unused-expressions | |
}; | |
export const checkPrivateWhitelist = (onCb, offCb) => { | |
const whitelistOn = onCb || (() => {}); | |
const whitelistOff = offCb || (() => {}); | |
const privateButAllowed = [ | |
'FBAV', | |
'FBAN', | |
'FBIOS', | |
'FBBV', | |
'FBDV', | |
'FBMD', | |
'FBSN', | |
'FBSV', | |
'FBSS', | |
'FBCR', | |
'FBID', | |
'FBLC', | |
'FBOP', | |
'Twitter for iPhone', | |
'TwitterAndroid', | |
'nytios', | |
'nytiphone', | |
'nytipad', | |
'nyt-android', | |
'AppleNews', | |
'Flipboard', | |
]; | |
const re = new RegExp(privateButAllowed.join('|'), 'i'); | |
if (re.test(navigator.userAgent)) { | |
whitelistOn(); | |
return true; | |
} | |
whitelistOff(); | |
return false; | |
}; |
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 detectPrivateMode(cb) { | |
if( | |
navigator.userAgent.indexOf('WebKit') !== -1 | |
&& navigator.userAgent.indexOf('Mobile') !== -1 | |
){ | |
try { | |
window.openDatabase(null, null, null, null); | |
cb(false); | |
} catch (_) { | |
cb(true); | |
} | |
}else{ | |
var db, | |
on = cb.bind(null, true), | |
off = cb.bind(null, false) | |
function tryls() { | |
try { | |
localStorage.length ? off() : (localStorage.x = 1, localStorage.removeItem("x"), off()); | |
} catch (e) { | |
navigator.cookieEnabled ? on() : off(); | |
} | |
} | |
window.webkitRequestFileSystem ? webkitRequestFileSystem(0, 0, off, on) | |
: "MozAppearance" in document.documentElement.style ? (db = indexedDB.open("test"), db.onerror = on, db.onsuccess = off) | |
: /constructor/i.test(window.HTMLElement) || window.safari ? tryls() | |
: !window.indexedDB && (window.PointerEvent || window.MSPointerEvent) ? on() | |
: off() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi infoziko,
to detect browser I use that function:
function getBrowserInfo () { var ua = navigator.userAgent; var tem; var M = ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || []; if (/trident/i.test(M[1])) { tem = /\brv[ :]+(\d+)/g.exec(ua) || []; return { browser: 'IE', version: (tem[1] || '') } } if (M[1] === 'Chrome') { tem = ua.match(/\b(OPR|Edge)\/(\d+)/); if (tem != null) { return { browser: tem[1].replace('OPR', 'Opera'), version: (tem[2] || '') } } } M = M[2] ? [M[1], M[2]] : [navigator.appName, navigator.appVersion, '-?'] if ((tem = ua.match(/version\/(\d+)/i)) != null) M.splice(1, 1, tem[1]) return { browser: M[0], version: M[1] } }