//判断当前浏览器是否开启了无痕模式
var detectPrivateMode = async (callback) => {
var isPrivate = false;
// Check for private browsing mode in Safari
if (window.webkitRequestFileSystem) {
window.webkitRequestFileSystem(
window.TEMPORARY,
1,
function () {
isPrivate = false;
},
function (e) {
isPrivate = true;
}
);
}
// Check for private browsing mode in Firefox
if (typeof InstallTrigger !== "undefined") {
function isPrivateMode() {
return new Promise(function (resolve, reject) {
const db = indexedDB.open("test");
db.onerror = function () {
return resolve(true);
};
db.onsuccess = function () {
return resolve(false);
};
});
}
var isPri = await isPrivateMode();
if (isPri) {
isPrivate = true;
} else {
isPrivate = false;
}
}
// Check for private browsing mode in Chrome and Edge
if (
window.navigator.userAgent.indexOf("Edge") > -1 ||
window.navigator.userAgent.indexOf("Chrome") > -1
) {
try {
window.localStorage.setItem("test", "test");
window.localStorage.removeItem("test");
} catch (e) {
isPrivate = true;
}
}
// Execute the callback function with the boolean value
if (callback) {
callback(isPrivate);
}
};
// Example usage
detectPrivateMode(function (isPrivate) {
if (isPrivate) {
console.log("The browser is in private mode"); }
else {
console.log("The browser is not in private mode");
}
});
检查当前浏览器是否开启隐私模式
最新推荐文章于 2025-04-25 12:33:49 发布