繁体   English   中英

适用于iOS 12 Safari的私密/隐身模式检测

[英]Private / Incognito Mode Detection for iOS 12 Safari

似乎适用于iOS 11和相应Safari版本的旧检测方法不再起作用。
我已经尝试过以下脚本: https : //gist.github.com/cou929/7973956
但它不适用于iOS 12上的野生动物园,也不适用于iOS 12上的Chrome 69。

这个相当新的库也不适用于iOS 12浏览器:
https://github.com/Maykonn/js-detect-incognito-private-browsing-paywall

那么,iOS 12浏览器有什么解决方案吗?

BostonGlobe似乎有解决方案,但我不知道他们是如何做到的:
https://www.bostonglobe.com/sports/redsox/2018/10/09/redsox/D66J59viZ1qxyZlhI18l8L/story.html (如果您想以隐身/私人模式阅读BostonGlobe.com文章,则会看到一个询问您的屏幕登录)

Chrome Devtools =>用于检测隐身/私有模式的模块称为“ detect-private-browsing ”,位于webpack:///./~/detect-private-browsing/index.js

// ./~/detect-private-browsing/index.js

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;
    }
    // MS Edge Detection from this gist: https://gist.github.com/cou929/7973956
    var edge = /edge/.exec(ua); 
    if (edge && edge[0] == "edge") { 
        return true; 
    }
    return false;
}

module.exports = {
    detectPrivateMode: function(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)) {

            // One-off check for weird sports 2.0 polyfill
            // This also impacts iOS Firefox and Chrome (newer versions), apparently
            // @see bglobe-js/containers/App.js:116
            if (window.safariIncognito) {
                is_private = true;
            } else {
                        try {
                           window.openDatabase(null, null, null, null);
                        } catch (e) {
                           is_private = true;
                        }

                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);
            }
        );
    }
};
//FOR IOS 12
var e = false;
if (window.localStorage && /Safari/.test(window.navigator.userAgent)) {
  if (window.safariIncognito) {
    e = true;
  } else {
    try {
      window.openDatabase(null, null, null, null);
      window.localStorage.setItem("test", 1)
    } catch (t) {
      e = true;
      alert("PRIVATE");
    }
  }
  void !e && (e = !1, window.localStorage.removeItem("test"))
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM