您可以通过脚本确定Chrome是否处于隐身模式?
-
04-10-2019 - |
题
是否可以通过脚本确定Google Chrome是否处于隐身模式?
编辑: 我实际上是说是通过用户订阅,但是答案假设JavaScript在网页上运行。我重新提出了这个问题 这里 关于用户脚本。
解决方案
是的。文件系统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"));
}
其他提示
一种方法是访问唯一的URL,然后检查一下是否将其视为CSS访问的链接。
您可以在“检测隐身”中看到一个例子 (死链).
在 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”的哭泣。但是,它将从Web服务器提供服务。
如果要开发扩展名,则可以使用TABS API确定窗口/TAB隐身是否。
可以找到更多信息 这里.
如果您只是使用网页,它并不容易,并且设计为这种方式。但是,我注意到在Incongnito中,所有尝试打开数据库(窗口。数据库)失败的尝试,这是因为当隐藏中不允许在用户计算机上保留大量数据。
我尚未测试它,但我怀疑所有对LocalStorage的电话也失败了。
这使用承诺等待异步代码设置标志,以便之后我们可以同步使用它。
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');
在Chrome 74+中,您可以通过 估计可用文件系统存储空间
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')
}
基于快速复制函数 Alok的答案 (注意:这是异步的)
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 Syntaxt编写的建议答案,并略微清洁。
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)
隐身模式的文档专门说,网站的行为不会有所不同。我相信这意味着答案是否定的。
不隶属于 StackOverflow