Skip to content

Instantly share code, notes, and snippets.

@jrltt
Created February 8, 2018 15:39
Show Gist options
  • Select an option

  • Save jrltt/95bdd2f617579f030d583a8b77273235 to your computer and use it in GitHub Desktop.

Select an option

Save jrltt/95bdd2f617579f030d583a8b77273235 to your computer and use it in GitHub Desktop.
Check if browser (Firefox or Safari) is private navigation
function isPrivate() {
return new Promise(function (resolve) {
const on = function () { return resolve(true); }; // is in private mode
const off = function () { return resolve(false); }; // not private mode
const testLocalStorage = function () {
try {
if (localStorage.length) off();
else {
localStorage.x = 1;
localStorage.removeItem('x');
off();
}
} catch (e) {
// 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();
}
};
if ('MozAppearance' in document.documentElement.style) {
const db = indexedDB.open('test');
db.onerror = on;
db.onsuccess = off;
return void 0;
}
if (navigator.userAgent.indexOf('Safari') && /Version\/10/gi.test(navigator.userAgent)) {
return testLocalStorage();
}
// others
return off();
});
}
// Example..
// isPrivate().then(function (isPrivate) {
// console.log('Is in private mode: ', isPrivate);
// });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment