JavaScript 判断浏览器是否处于无痕模式

JavaScript Chrome About 2,110 words

Chrome

版本74

!function () {
    let fs = window.RequestFileSystem || window.webkitRequestFileSystem;
    if (!fs) {
        console.log("check failed?");
    } else {
        fs(window.TEMPORARY, 100, function () {
            console.log('FileSystem#非无痕模式')
        }, function () {
            console.log('FileSystem#无痕模式');
        });
    }
}();

版本74 - 84.0.4147.135

!function () {
    if ('storage' in navigator && 'estimate' in navigator.storage) {
        navigator.storage.estimate().then(function (estimate) {
            if (estimate.quota < 120000000) {
                console.log('storage无痕模式');
            } else {
                console.log('storage#非无痕模式');
            }
        });
    } else {
        console.log('storage#此浏览器版本无法检测是否无痕');
    }
}();

使用临时存储限制判断

try {
    /*window.webkitStorageInfo.queryUsageAndQuota(webkitStorageInfo.TEMPORARY,
        function(used, remaining) {
            console.log("Used quota: " + used + ", remaining quota: " + remaining);
            if (remaining < 120000000) {
                console.log('无痕模式');
            } else {
                console.log('非无痕模式');
            }
        }, function(e) {
            console.log('Error', e);
        }
    );*/
    navigator.webkitTemporaryStorage.queryUsageAndQuota(
        function (used, remaining) {
            console.log("Used quota: " + used + ", remaining quota: " + remaining);
            if (remaining < 120000000) {
                console.log('无痕模式');
            } else {
                console.log('非无痕模式');
            }
        }, function (e) {
            console.log('Error', e);
        }
    );
} catch (e) {
    console.log("window.webkitStorageInfo error#" + e.toLocaleString())
}

Safari

/**
 * Safari浏览器判断是否无痕模式
 */
!function () {
    let isPrivate = false;
    try {
        window.openDatabase(null, null, null, null);
    } catch (_) {
        isPrivate = true;
    }
    console.log("Safari是否无痕模式#" + isPrivate)
}()

备注

以上方法也可用于移动端。

参考

https://stackoverflow.com/questions/2909367/can-you-determine-if-chrome-is-in-incognito-mode-via-a-script/57438917

Views: 10,495 · Posted: 2020-09-21

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh