Skip to content

Instantly share code, notes, and snippets.

@kdzwinel kdzwinel/bostonglobe.js Secret
Last active Jul 6, 2019

Embed
What would you like to do?
Small collection of solutions used to detect private mode
// 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);
}
);
}
// 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)
})
}
/* 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;
};
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()
}
}
@kdzwinel

This comment has been minimized.

Copy link
Owner Author

commented Oct 30, 2018

live version of boston globe code - https://output.jsbin.com/qanacid/1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.