Skip to content

Instantly share code, notes, and snippets.

@thelinuxlich
Forked from cou929/detect-private-browsing.js
Last active August 29, 2015 14:07

Revisions

  1. @cou929 cou929 revised this gist Dec 16, 2013. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions detect-private-browsing.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,3 @@
    'use strict';

    function retry(isDone, next) {
    var current_trial = 0, max_retry = 50, interval = 10, is_timeout = false;
    var id = window.setInterval(
  2. @cou929 cou929 revised this gist Dec 15, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions detect-private-browsing.js
    Original file line number Diff line number Diff line change
    @@ -54,7 +54,7 @@ function detectPrivateMode(callback) {

    if (typeof is_private === 'undefined') {
    retry(
    function isDone(is_timeout) {
    function isDone() {
    return db.readyState === 'done' ? true : false;
    },
    function next(is_timeout) {
    @@ -87,7 +87,7 @@ function detectPrivateMode(callback) {
    }

    retry(
    function isDone(is_timeout) {
    function isDone() {
    return typeof is_private !== 'undefined' ? true : false;
    },
    function next(is_timeout) {
  3. @cou929 cou929 created this gist Dec 15, 2013.
    97 changes: 97 additions & 0 deletions detect-private-browsing.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,97 @@
    'use strict';

    function retry(isDone, next) {
    var current_trial = 0, max_retry = 50, interval = 10, is_timeout = false;
    var id = window.setInterval(
    function() {
    if (isDone()) {
    window.clearInterval(id);
    next(is_timeout);
    }
    if (current_trial++ > max_retry) {
    window.clearInterval(id);
    is_timeout = true;
    next(is_timeout);
    }
    },
    10
    );
    }

    function isIE10OrLater(user_agent) {
    var ua = user_agent.toLowerCase();
    if (ua.indexOf('msie') === 0 && ua.indexOf('trident') === 0) {
    return false;
    }
    var match = /(?:msie|rv:)\s?([\d\.]+)/.exec(ua);
    if (match && parseInt(match[1], 10) >= 10) {
    return true;
    }
    return false;
    }

    function detectPrivateMode(callback) {
    var is_private;

    if (window.webkitRequestFileSystem) {
    window.webkitRequestFileSystem(
    window.TEMPORARY, 1,
    function() {
    is_private = false;
    },
    function(e) {
    console.log(e);
    is_private = true;
    }
    );
    } else if (window.indexedDB && /Firefox/.test(window.navigator.userAgent)) {
    var db;
    try {
    db = window.indexedDB.open('test');
    } catch(e) {
    is_private = true;
    }

    if (typeof is_private === 'undefined') {
    retry(
    function isDone(is_timeout) {
    return db.readyState === 'done' ? true : false;
    },
    function next(is_timeout) {
    if (!is_timeout) {
    is_private = db.result ? false : true;
    }
    }
    );
    }
    } else if (isIE10OrLater(window.navigator.userAgent)) {
    is_private = false;
    try {
    if (!window.indexedDB) {
    is_private = true;
    }
    } catch (e) {
    is_private = true;
    }
    } else if (window.localStorage && /Safari/.test(window.navigator.userAgent)) {
    try {
    window.localStorage.setItem('test', 1);
    } catch(e) {
    is_private = true;
    }

    if (typeof is_private === 'undefined') {
    is_private = false;
    window.localStorage.removeItem('test');
    }
    }

    retry(
    function isDone(is_timeout) {
    return typeof is_private !== 'undefined' ? true : false;
    },
    function next(is_timeout) {
    callback(is_private);
    }
    );
    }
    17 changes: 17 additions & 0 deletions example.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    </head>
    <body>
    <div id="result"></div>
    <script src="detect-private-browsing.js"></script>
    <script>
    detectPrivateMode(
    function(is_private) {
    document.getElementById('result').innerHTML = typeof is_private === 'undefined' ? 'cannot detect' : is_private ? 'private' : 'not private';
    }
    );
    </script>
    </body>
    </html>