-
-
Save DarkPointer/4b96f41355dda2bc4be1747322ed7124 to your computer and use it in GitHub Desktop.
Revisions
-
jherax revised this gist
May 5, 2019 . 2 changed files with 34 additions and 19 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,38 +1,53 @@ // 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 var fs = window.webkitRequestFileSystem || window.RequestFileSystem; if (fs) { return void fs(window.TEMPORARY, 100, not, yes); } // 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 @@ -42,16 +57,16 @@ function isPrivateMode() { 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(); }); } This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,3 @@ isPrivateMode().then(function (isPrivate) { console.log('Browsing in private mode? ', isPrivate); }); -
jherax revised this gist
Feb 6, 2019 . 2 changed files with 18 additions and 10 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,10 +1,9 @@ /** * Detect if the browser is running in Private mode * * @returns {Promise} */ function isPrivateMode() { return new Promise((resolve) => { const on = () => resolve(true); // is in private mode const off = () => resolve(false); // not private mode @@ -18,7 +17,7 @@ export default function isPrivateMode() { } } 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(); @@ -30,20 +29,29 @@ export default function isPrivateMode() { } // Firefox if ('MozAppearance' in document.documentElement.style) { if (indexedDB === null) return on(); const db = indexedDB.open('test'); db.onerror = on; db.onsuccess = off; 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 off(); } catch (_) { return on(); }; } // IE10+ & Edge InPrivate if (!window.indexedDB && (window.PointerEvent || window.MSPointerEvent)) { return on(); } // default navigation mode return off(); }); } This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,3 @@ isPrivateMode().then((inPrivate) => { console.log('is in private mode: ', inPrivate); }); -
jherax revised this gist
Mar 6, 2017 . 3 changed files with 52 additions and 29 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,29 +0,0 @@ This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,49 @@ /** * Detect if the browser is running in Private Browsing mode * * @export * @returns {Promise} */ export default function isPrivateMode() { return new Promise((resolve) => { const on = () => resolve(true); // is in private mode const off = () => resolve(false); // not private mode const testLocalStorage = () => { 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(); } }; // Chrome & Opera if (window.webkitRequestFileSystem) { return void window.webkitRequestFileSystem(0, 0, off, on); } // Firefox if ('MozAppearance' in document.documentElement.style) { const db = indexedDB.open('test'); db.onerror = on; db.onsuccess = off; return void 0; } // Safari if (/constructor/i.test(window.HTMLElement)) { return testLocalStorage(); } // IE10+ & Edge if (!window.indexedDB && (window.PointerEvent || window.MSPointerEvent)) { return on(); } // others return off(); }); } This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,3 @@ isPrivateMode().then((isPrivate) => { console.log('Is in private mode: ', isPrivate); }); -
jherax revised this gist
Dec 1, 2016 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -12,10 +12,10 @@ new Promise(function(resolve) { // in using private mode navigator.cookieEnabled ? on() : off(); } }; // Chrome & Opera window.webkitRequestFileSystem ? window.webkitRequestFileSystem(0, 0, off, on) // Firefox : 'MozAppearance' in document.documentElement.style ? (db = indexedDB.open('test'), db.onerror = on, db.onsuccess = off) // Safari -
jherax created this gist
Dec 1, 2016 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,29 @@ new Promise(function(resolve) { var db, on = function() { resolve(true) }, // is in private mode off = function() { resolve(false) }, // not private mode tryls = function() { try { localStorage.length ? off() : (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(); } } // Chrome & Opera window.webkitRequestFileSystem ? webkitRequestFileSystem(0, 0, off, on) // Firefox : 'MozAppearance' in document.documentElement.style ? (db = indexedDB.open('test'), db.onerror = on, db.onsuccess = off) // Safari : (/constructor/i).test(window.HTMLElement) ? tryls() // IE10+ & Edge : !window.indexedDB && (window.PointerEvent || window.MSPointerEvent) ? on() // Rest : off(); }).then(function(isPrivateMode) { console.log('Private mode is: ' + isPrivateMode) });