<html>
<head>
<meta name="description" content="Testing isPrivateMode">
<meta charset=utf-8 />
<title>isPrivateMode</title>
<script src="https://cdn.jsdelivr.net/npm/es6-promise@4/dist/es6-promise.auto.min.js"></script>
</head>
<body>
<h4>Testing</h4>
<p>
<code>function isPrivateMode(): Promise<boolean></code>
</p>
<hr />
<p>
An <code>alert()</code> will notify if you are browsing in private mode.
</p>
<hr />
<h4>Usage</h4>
<p>
<code>
isPrivateMode().then(function (isPrivate) {
<br>
console.log('Browsing in private mode? ', isPrivate);
<br>
});
</code>
</p>
<hr />
<aside>
<strong>Important!</strong>
<a target="_blank" href="https://cdn.jsdelivr.net/npm/es6-promise@4/dist/es6-promise.auto.min.js"><code>Promise</code></a> object is polyfilled.
</aside>
</body>
</html>
aside {
font-style: italic;
}
/**
* Lightweight script to detect whether the browser is running in Private mode.
* @returns {Promise<boolean>}
*
* 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
*
* Source:
* @see https://gist.github.com/jherax/a81c8c132d09cc354a0e2cb911841ff1
*/
function isPrivateMode() {
return new Promise(function detect(resolve) {
var yes = function() { resolve(true); }; // is in private mode
var not = function() { resolve(false); }; // not in private mode
function detectChromeOpera() {
// https://developers.google.com/web/updates/2017/08/estimating-available-storage-space
var isChromeOpera = /(?=.*(opera|chrome)).*/i.test(navigator.userAgent) && navigator.storage && navigator.storage.estimate;
if (isChromeOpera) {
navigator.storage.estimate().then(function(data) {
return data.quota < 120000000 ? yes() : not();
});
}
return !!isChromeOpera;
}
function detectFirefox() {
var isMozillaFirefox = 'MozAppearance' in document.documentElement.style;
if (isMozillaFirefox) {
if (indexedDB == null) yes();
else {
var db = indexedDB.open('inPrivate');
db.onsuccess = not;
db.onerror = yes;
}
}
return isMozillaFirefox;
}
function detectSafari() {
var isSafari = navigator.userAgent.match(/Version\/([0-9\._]+).*Safari/);
if (isSafari) {
var testLocalStorage = function() {
try {
if (localStorage.length) not();
else {
localStorage.setItem('inPrivate', '0');
localStorage.removeItem('inPrivate');
not();
}
} catch (_) {
// 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();
}
return true;
};
var version = parseInt(isSafari[1], 10);
if (version < 11) return testLocalStorage();
try {
window.openDatabase(null, null, null, null);
not();
} catch (_) {
yes();
}
}
return !!isSafari;
}
function detectEdgeIE10() {
var isEdgeIE10 = !window.indexedDB && (window.PointerEvent || window.MSPointerEvent);
if (isEdgeIE10) yes();
return !!isEdgeIE10;
}
// when a browser is detected, it runs tests for that browser
// and skips pointless testing for other browsers.
if (detectChromeOpera()) return;
if (detectFirefox()) return;
if (detectSafari()) return;
if (detectEdgeIE10()) return;
// default navigation mode
return not();
});
}
// -------------------------------
setTimeout(function() {
isPrivateMode().then(function(isPrivate) {
console.log('Browsing in private mode? ', isPrivate);
alert('Browsing in private mode? ' + isPrivate);
});
}, 100);
Output
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |