用于跨浏览器检测隐身模式(私有(private)浏览)的 JavaScript

标签 javascript google-chrome firefox safari microsoft-edge

我正在尝试创建一个跨浏览器 JavaScript,用于检测访问者是否使用隐身模式,并在用户以正常模式访问页面时发出警报消息。

目前我有一个脚本,在 Chrome 和 Opera 上运行得很好,但我需要让它在所有其他浏览器上也运行,如 Firefox、safari、Edge 等。

我的脚本(在 Chrome 和 Opera 上工作)是:

<script type='text/javascript'>//<![CDATA[
window.onload=function(){
function main() {
  var fs = window.RequestFileSystem || window.webkitRequestFileSystem;
  if (!fs) {
     alert("check failed!");
    return;
  }
  fs(window.TEMPORARY, 100, function(fs) {
    alert("You are not using Incognito Mode!");
  });
}
main();

}//]]> </script>

请帮我编写一个这样的脚本,以便在所有主要网络浏览器中给出相同的警报结果。

谢谢

更新:

我终于也为 Firefox 制作了一个工作脚本。代码如下:

<script type='text/javascript'>
var db;
var request = indexedDB.open("MyTestDatabase");
request.onsuccess = function(event) {
if (navigator.userAgent.indexOf("Firefox") != -1)
{
  alert("You are not using Incognito Mode!");
};
};</script>

我使用了“if (navigator.userAgent.indexOf("Firefox") != -1)”函数,以便它仅在 Firefox 上执行,而不在 Chrome 或任何其他浏览器中执行。

更新:

好吧,又一个成就!我也成功地为 Safari 编写了脚本。这是:

<script type='text/javascript'>
try { localStorage.test = 2; } catch (e) {
}
if (navigator.userAgent.search("Safari") >= 0 && navigator.userAgent.search("Chrome") < 0) 
{
if (localStorage.test = "true") {
alert("You are not using Incognito Mode!");
}
}
</script>

我再次使用了“if (navigator.userAgent.search("Safari") >= 0 && navigator.userAgent.search("Chrome") < 0)”函数,以便它仅在 Safari 上执行,并且不在任何其他浏览器中。

现在我需要帮助为 Edge 浏览器编写脚本。

最佳答案

我找到了这个解决方案 here :

我只是将其复制到下面,以防它被删除。

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() {
                    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() {
            return typeof is_private !== 'undefined' ? true : false;
        },
        function next(is_timeout) {
            callback(is_private);
        }
    );
}

关于用于跨浏览器检测隐身模式(私有(private)浏览)的 JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41499966/

相关文章:

javascript - WinJS DOM 加载

javascript - 使用 javascript 将 var 分配给输入标记的 name 属性

javascript - 在 Android 4.4 上获取 Chromium WebView?

html - 谷歌浏览器不会呈现 100% 高度的 body 元素

firefox - 为什么 Firefox 中的 gl.readPixels 给出全 0?

javascript - 如何在 ExtJS 上使用多个 gridfilter 类型?

javascript - Twitter Bootstrap 的 javascript Popover 看起来不对

google-maps - Google Chrome 浏览器中的 Google Maps 闪烁问题

javascript - 不使用附加 SDK 的 Firefox 扩展中的 ContentScript

html - 将 <li> 放在 <a> 中时出现 Firefox 语法问题 - 不可链接?