是否可以通过脚本确定 Google Chrome 是否处于隐身模式?
编辑: 我实际上的意思是它可能通过用户脚本,但答案假设 JavaScript 正在网页上运行。我在这里重新询问了有关用户脚本的问题。
是否可以通过脚本确定 Google Chrome 是否处于隐身模式?
编辑: 我实际上的意思是它可能通过用户脚本,但答案假设 JavaScript 正在网页上运行。我在这里重新询问了有关用户脚本的问题。
此答案的功能取决于 Chrome 版本。最近的评论是这适用于v90
是的。FileSystem API 在隐身模式下被禁用。当您处于和不处于隐身模式时,请查看https://jsfiddle.net/w49x9f1a/ 。
示例代码:
var fs = window.RequestFileSystem || window.webkitRequestFileSystem;
if (!fs) {
console.log("check failed?");
} else {
fs(window.TEMPORARY,
100,
console.log.bind(console, "not in incognito mode"),
console.log.bind(console, "incognito mode"));
}
在 Chrome 74 到 84.0.4147.135 中,您可以通过估计可用的文件系统存储空间来确定这一点
if ('storage' in navigator && 'estimate' in navigator.storage) {
const {usage, quota} = await navigator.storage.estimate();
console.log(`Using ${usage} out of ${quota} bytes.`);
if(quota < 120000000){
console.log('Incognito')
} else {
console.log('Not Incognito')
}
} else {
console.log('Can not detect')
}
一种方法是访问唯一的 URL,然后检查指向该 URL 的链接是否被 CSS 视为已访问。
您可以在“Detecting Incognito” (死链接)中看到一个示例。
同一作者的研究论文替换了上面的 Detecting Incognito 链接
在main.html
添加 iframe 时,
<iframe id='testFrame' name='testFrame' onload='setUniqueSource(this)' src='' style="width:0; height:0; visibility:hidden;"></iframe>
,以及一些 JavaScript 代码:
function checkResult() {
var a = frames[0].document.getElementById('test');
if (!a) return;
var color;
if (a.currentStyle) {
color = a.currentStyle.color;
} else {
color = frames[0].getComputedStyle(a, '').color;
}
var visited = (color == 'rgb(51, 102, 160)' || color == '#3366a0');
alert('mode is ' + (visited ? 'NOT Private' : 'Private'));
}
function setUniqueSource(frame) {
frame.src = "test.html?" + Math.random();
frame.onload = '';
}
然后test.html
将其加载到 iFrame 中:
<style>
a:link { color: #336699; }
a:visited { color: #3366A0; }
</style>
<script>
setTimeout(function() {
var a = document.createElement('a');
a.href = location;
a.id = 'test';
document.body.appendChild(a);
parent.checkResult();
}, 100);
</script>
注意:从文件系统尝试这个可能会让 Chrome 哭泣“不安全的 Javascript”。但是,它将从网络服务器提供服务。
如果您正在开发扩展,那么您可以使用选项卡 API 来确定窗口/选项卡是否隐身。
更多信息可以在这里找到。
如果你只是在处理一个网页,这并不容易,它就是这样设计的。但是,我注意到在隐身模式下打开数据库(window.database)的所有尝试都失败了,这是因为在隐身模式下,不允许在用户计算机上留下任何数据痕迹。
我还没有测试过它,但我怀疑对 localStorage 的所有调用也会失败。
更新这似乎不再起作用了
这使用了一个 Promise 来等待异步代码设置一个标志,因此我们可以在之后同步使用它。
let isIncognito = await new Promise((resolve, reject)=>{
var fs = window.RequestFileSystem || window.webkitRequestFileSystem;
if (!fs) reject('Check incognito failed');
else fs(window.TEMPORARY, 100, ()=>resolve(false), ()=>resolve(true));
});
那么我们可以做
if(isIncognito) alert('in incognito');
else alert('not in incognito');
对于那些正在寻找解决方案的人,这里简要介绍了截至 2021 年 10 月在各种浏览器中检测隐私浏览模式的当前方法:
Chromium:类似于 Vinnie James 的回答,调用 navigator.storage.estimate(),获取 quota 属性并将其与 performance.memory.jsHeapSizeLimit 进行比较。如果 quota 属性小于 jsHeapSizeLimit,则为隐身。如果 jsHeapSizeLimit 未定义,请使用 1073741824 (1 GiB)。
适用于 macOS 的 Safari:在不存在的推送服务器上使用 safari.pushNotification.requestPermission 并获取错误。如果错误中没有出现“手势”,则它处于私人模式。
适用于 iOS 的 Safari:创建 iframe 并在 iframe 上使用 contentWindow.applicationCache 添加错误事件侦听器。如果错误发生,则它处于私有模式。
Firefox:navigator.serviceWorker 将在私有窗口中未定义。
Internet Explorer:window.indexedDB 在 InPrivate 模式下将未定义。
您可以在我在 GitHub 上提供的detectIncognito脚本中看到这些方法的实现。
基于Alok's Answer 的快速函数(注意:这是异步的)
更新- 不再工作
function ifIncognito(incog,func){
var fs = window.RequestFileSystem || window.webkitRequestFileSystem;
if (!fs) console.log("checking incognito failed");
else {
if(incog) fs(window.TEMPORARY, 100, ()=>{}, func);
else fs(window.TEMPORARY, 100, func, ()=>{});
}
}
用法:
ifIncognito(true, ()=>{ alert('in incognito') });
// or
ifIncognito(false, ()=>{ alert('not in incognito') });
这是用 ES6 语法编写的建议答案,并稍作清理。
const isIncognito = () => new Promise((resolve, reject) => {
const fs = window.RequestFileSystem || window.webkitRequestFileSystem;
if (!fs) {
reject('Cant determine whether browser is running in incognito mode!');
}
fs(window.TEMPORARY, 100, resolve.bind(null, false), resolve.bind(null, true));
});
// Usage
isIncognito()
.then(console.log)
.catch(console.error)
这适用于 2021 年 5 月:https ://jsfiddle.net/2b1dk8oa/
该脚本必须在 iframe 中的网页中执行。
try{
var ls = localStorage;
alert("You are not in Incognito Mode.");
}
catch(e) { alert("You are in Incognito Mode."); }