Javascript 您能否通过脚本确定 Chrome 是否处于隐身模式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2909367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can you determine if Chrome is in incognito mode via a script?
提问by RodeoClown
Is it possible to determine if Google Chrome is in incognito mode via a script?
是否可以通过脚本确定 Google Chrome 是否处于隐身模式?
Edit:I actually meant is it possible via user-script, but the answers assume JavaScript is running on a web page. I've re-asked the question herein regards to user scripts.
编辑:我的意思实际上是通过用户脚本可以实现,但答案假设 JavaScript 正在网页上运行。我在这里重新询问了有关用户脚本的问题。
回答by Alok
Yes. The FileSystem API is disabled in incognito mode. Check out https://jsfiddle.net/w49x9f1a/when you are and aren't in incognito mode.
是的。FileSystem API 在隐身模式下被禁用。当您处于和未处于隐身模式时,请查看https://jsfiddle.net/w49x9f1a/。
Sample code:
示例代码:
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"));
}
回答by JHurrah
One way is to visit a unique URL and then check to see whether a link to that URL is treated as visited by CSS.
一种方法是访问唯一的 URL,然后检查该 URL 的链接是否被 CSS 视为访问过。
You can see an example of this in "Detecting Incognito" (Dead link).
你可以在“Detecting Incognito” (死链接)中看到一个例子。
Research paper by same author to replace Detecting Incognito link above
In main.htmladd an iframe,
在main.html新增的iframe,
<iframe id='testFrame' name='testFrame' onload='setUniqueSource(this)' src='' style="width:0; height:0; visibility:hidden;"></iframe>
, and some JavaScript code:
,以及一些 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 = '';
}
Then in test.htmlthat are loaded into the iFrame:
然后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>
NOTE: trying this from the filesystem can make Chrome cry about "Unsafe Javascript". It will, however, work serving from a webserver.
注意:从文件系统尝试这个会让 Chrome 抱怨“不安全的 Javascript”。但是,它将从网络服务器提供服务。
回答by Vinnie James
In Chrome 74+ you can determine this by estimating the available file system storage space
在 Chrome 74+ 中,您可以通过估计可用的文件系统存储空间来确定这一点
See the jsfiddle
查看jsfiddle
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')
}
回答by Igor Zevaka
You can, in JavaScript, see JHurrah's answer. Except for not highlighting links, all incognito mode does is not save browse history and cookies. From google help page:
您可以在 JavaScript 中查看 JHurrah's answer。除了不突出显示链接外,所有隐身模式都不会保存浏览历史和 cookie。从谷歌帮助页面:
- Webpages that you open and files downloaded while you are incognito aren't recorded in your browsing and download histories.
- All new cookies are deleted after you close all incognito windows that you've opened.
- 您在隐身时打开的网页和下载的文件不会记录在您的浏览和下载历史记录中。
- 关闭所有已打开的隐身窗口后,所有新 cookie 都会被删除。
As you can see the differences between normal browsing and incognito happen afteryou visit the webpage, hence there is nothing that browser communicates to the server when it's in this mode.
正如您在访问网页后看到的正常浏览和隐身浏览之间的差异一样,因此当浏览器处于这种模式时,浏览器不会与服务器进行任何通信。
You can see what exactly your browser sends to the server using one of many HTTP request analysers, like this one here. Compare the headers between normal session and incognito and you will see no difference.
您可以使用许多 HTTP 请求分析器之一查看浏览器向服务器发送的确切内容,就像这里的这个。比较普通会话和隐身会话之间的标题,您将看不到任何区别。
回答by Kinlan
If you are developing an Extension then you can use the tabs API to determine if a window/tab incognito.
如果您正在开发扩展,那么您可以使用选项卡 API 来确定窗口/选项卡是否隐身。
More information can be found here.
可以在此处找到更多信息。
If you are just working with a webpage, it is not easy, and it is designed to be that way. However, I have noticed that all attempts to open a database (window.database) fail when in incongnito, this is because when in incognito no trace of data is allowed to be left on the users machine.
如果你只是在处理一个网页,这并不容易,而且它就是这样设计的。但是,我注意到在隐身模式下打开数据库(window.database)的所有尝试都失败了,这是因为在隐身模式下,不允许在用户计算机上留下任何数据痕迹。
I haven't tested it but I suspect all calls to localStorage fail too.
我还没有测试过,但我怀疑所有对 localStorage 的调用也失败了。
回答by aljgom
This uses a promise to wait for the asynchronous code to set a flag, so we can use it synchronously afterward.
这使用了一个 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));
});
then we can do
那么我们可以做
if(isIncognito) alert('in incognito');
else alert('not in incognito');
回答by nikksan
Here is the suggested answer written in ES6 syntaxt and slightly cleand up.
这是用 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)
回答by aljgom
Quick copy-paste function based on Alok's Answer(note: this is asynchronous)
基于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, ()=>{});
}
}
usage:
用法:
ifIncognito(true, ()=>{ alert('in incognito') });
// or
ifIncognito(false, ()=>{ alert('not in incognito') });
回答by Juan Ricardo
Here the solution using promises
这里使用承诺的解决方案
const isIncognito = () => {
return new Promise((resolve, reject) => {
if ("storage" in navigator && "estimate" in navigator.storage) {
navigator.storage.estimate().then((res) => {
console.log(`Using ${res.usage} out of ${res.quota} bytes.`);
if (res.quota < 120000000) {
resolve(true);
} else {
reject(false);
}
});
} else {
reject(false);
}
});
};
An how to use it:
一个如何使用它:
isIncognito().then(yes => console.log(yes)).catch(isnot => console.log(isnot))
回答by Puppy
The documentation for incognito mode specifically says that websites won't behave differently. I believe this means that the answer is no.
隐身模式的文档特别指出,网站的行为不会有所不同。我相信这意味着答案是否定的。

