Skip to content

Instantly share code, notes, and snippets.

@DarkPointer
Forked from jherax/is-private-mode.js
Created August 28, 2019 20:11
Show Gist options
  • Select an option

  • Save DarkPointer/4b96f41355dda2bc4be1747322ed7124 to your computer and use it in GitHub Desktop.

Select an option

Save DarkPointer/4b96f41355dda2bc4be1747322ed7124 to your computer and use it in GitHub Desktop.

Revisions

  1. @jherax jherax revised this gist May 5, 2019. 2 changed files with 34 additions and 19 deletions.
    49 changes: 32 additions & 17 deletions is-private-mode.js
    Original 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;

    /**
    * Detect if the browser is running in Private mode
    *
    * 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 on = () => resolve(true); // is in private mode
    const off = () => resolve(false); // not private mode
    const yes = () => resolve(true); // is in private mode
    const not = () => resolve(false); // not in private mode
    const testLocalStorage = () => {
    try {
    if (localStorage.length) off();
    if (localStorage.length) not();
    else {
    localStorage.x = 1;
    localStorage.removeItem('x');
    off();
    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 ? on() : off();
    navigator.cookieEnabled ? yes() : not();
    }
    };
    // Chrome & Opera
    if (window.webkitRequestFileSystem) {
    return void window.webkitRequestFileSystem(0, 0, off, on);
    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 on();
    if (indexedDB === null) return yes();
    const db = indexedDB.open('test');
    db.onerror = on;
    db.onsuccess = off;
    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 off();
    return not();
    } catch (_) {
    return on();
    };
    return yes();
    }
    }
    // IE10+ & Edge InPrivate
    if (!window.indexedDB && (window.PointerEvent || window.MSPointerEvent)) {
    return on();
    return yes();
    }
    // default navigation mode
    return off();
    return not();
    });
    }
    4 changes: 2 additions & 2 deletions test.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,3 @@
    isPrivateMode().then((inPrivate) => {
    console.log('is in private mode: ', inPrivate);
    isPrivateMode().then(function (isPrivate) {
    console.log('Browsing in private mode? ', isPrivate);
    });
  2. @jherax jherax revised this gist Feb 6, 2019. 2 changed files with 18 additions and 10 deletions.
    24 changes: 16 additions & 8 deletions is-private-mode.js
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,9 @@
    /**
    * Detect if the browser is running in Private Browsing mode
    * Detect if the browser is running in Private mode
    *
    * @export
    * @returns {Promise}
    */
    export default function isPrivateMode() {
    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 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
    if (/constructor/i.test(window.HTMLElement)) {
    return testLocalStorage();
    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
    // IE10+ & Edge InPrivate
    if (!window.indexedDB && (window.PointerEvent || window.MSPointerEvent)) {
    return on();
    }
    // others
    // default navigation mode
    return off();
    });
    }
    4 changes: 2 additions & 2 deletions test.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,3 @@
    isPrivateMode().then((isPrivate) => {
    console.log('Is in private mode: ', isPrivate);
    isPrivateMode().then((inPrivate) => {
    console.log('is in private mode: ', inPrivate);
    });
  3. @jherax jherax revised this gist Mar 6, 2017. 3 changed files with 52 additions and 29 deletions.
    29 changes: 0 additions & 29 deletions detect-private-mode.js
    Original file line number Diff line number Diff line change
    @@ -1,29 +0,0 @@
    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 ? window.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)
    });
    49 changes: 49 additions & 0 deletions is-private-mode.js
    Original 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();
    });
    }
    3 changes: 3 additions & 0 deletions test.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    isPrivateMode().then((isPrivate) => {
    console.log('Is in private mode: ', isPrivate);
    });
  4. @jherax jherax revised this gist Dec 1, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions detect-private-mode.js
    Original 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 ? webkitRequestFileSystem(0, 0, off, on)
    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
  5. @jherax jherax created this gist Dec 1, 2016.
    29 changes: 29 additions & 0 deletions detect-private-mode.js
    Original 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)
    });