Skip to content

Instantly share code, notes, and snippets.

@Choy-lingling
Forked from jherax/is-private-mode.js
Last active November 19, 2021 17:12
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Choy-lingling/071aa8ca03ebaba3fe0278376f2321ba to your computer and use it in GitHub Desktop.
Detect if the browser is running in Private mode (Promise based)
// uncomment if you are transpiling with Babel + Webpack
// const { window, document } = global;
/**
* Lightweight script to detect whether the browser is running in Private mode.
* @returns {Promise}
*
* Live demo:
* @see http://live.datatables.net/piduzelo/1
*
* This snippet uses ES2015 syntax. If you want to run it in old browsers, transpile it with Babel:
* @see https://babeljs.io/repl
*
* This snippet uses Promises. If you want to run it in old browsers, polyfill it:
* @see https://cdn.jsdelivr.net/npm/es6-promise@4/dist/es6-promise.auto.min.js
*
* More Promise Polyfills:
* @see https://ourcodeworld.com/articles/read/316/top-5-best-javascript-promises-polyfills
*/
function isPrivateMode() {
return new Promise((resolve) => {
const yes = () => resolve(true); // is in private mode
const not = () => resolve(false); // not in private mode
const testLocalStorage = () => {
try {
if (localStorage.length) not();
else {
localStorage.x = 1;
localStorage.removeItem('x');
not();
}
} 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 ? yes() : not();
}
};
// Chrome & Opera adjusted to cope with this issue
//https://stackoverflow.com/questions/2860879/detecting-if-a-browser-is-using-private-browsing-mode#targetText=Google%20is%20removing%20the%20ability,permanently%20in%20Chrome%2076%20onwards.&targetText=To%20anyone%20else%20coming%20across,mode%20through%20Javascript%20or%20CSS.
var fs = window.webkitRequestFileSystem || window.RequestFileSystem;
if (fs) {
async function f() {
if ('storage' in navigator && 'estimate' in navigator.storage) {
const {
usage,
quota
} = await navigator.storage.estimate();
if (quota < 120000000) {
return yes();
} else {
return not();
}
}
}
return void f();
}
// Firefox
if ('MozAppearance' in document.documentElement.style) {
if (indexedDB === null) return yes();
const db = indexedDB.open('test');
db.onerror = yes;
db.onsuccess = not;
return void 0;
}
// Safari
const isSafari = navigator.userAgent.match(/Version\/([0-9\._]+).*Safari/);
if (isSafari) {
const version = parseInt(isSafari[1], 10);
if (version < 11) return testLocalStorage();
try {
window.openDatabase(null, null, null, null);
return not();
} catch (_) {
return yes();
}
}
// IE10+ & Edge InPrivate
if (!window.indexedDB && (window.PointerEvent || window.MSPointerEvent)) {
return yes();
}
// default navigation mode
return not();
});
}
isPrivateMode().then(function (isPrivate) {
console.log('Browsing in private mode? ', isPrivate);
});
@cjies
Copy link

cjies commented Aug 29, 2019

found a bug in line 54, if the site is serving with HTTP protocol, navigator.storage API will not exist.
Not sure how to fix it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment