Is it possible to determine if Google Chrome is in incognito mode via a script?

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 here in regards to user scripts.

share|improve this question
15  
It wouldn't be very incognito if you could determine it easily :) – Aren May 26 '10 at 0:05
    
And you have to remember that the user must allow incognito mode for the extension manual. By default, everything is true. – Mohamed Mansour May 26 '10 at 1:23
    
@Mohamed: The user having to allow it would be me, so that won't be a problem :) – RodeoClown May 26 '10 at 20:01
    
Wait, I just realised I didn't make myself clear - I meant with a user script. I'll keep this question alive, as it has some useful answers (thanks), but I'll ask another question with extra clarification. – RodeoClown May 26 '10 at 21:17
1  
up vote 116 down vote accepted

Yes. The FileSystem API is disabled in incognito mode. Check out http://jsfiddle.net/w49x9f1a/ when you are and aren't in incognito mode.

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"));
}
share|improve this answer
12  
This is the least hack-ish way and should be at the top. Clean and elegant. – Tom Teman Oct 4 '15 at 16:21
    
This is brilliant! Thank you soo much! The only problem is that it's asynchronous and since I have a situation where I have to wait for that check it slows down the page load by a few miliseconds. – user2718671 Feb 24 at 13:38
    
@user2718671 do the check once and then set a cookie. A few ms on the first page load should not be noticeable in the larger scheme of things. – Alok Feb 28 at 1:29
    
@Alok Thanks for that information. But I think in my case it wouldn't help because the user can't use all functionalities on the page if incognito mode is enabled. After the check a notification about that is displayed that the user if he wants to use all functionalities he should leave incognito mode and refresh the page. So the check every time he enters the page makes sense. – user2718671 Feb 28 at 10:53
1  
I tried at Chrome. It works, but the logic is backwards. – Lucas Massuh Aug 16 at 13:19

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.

You can see an example of this in "Detecting Incognito" (Dead link).

Research paper by same author to replace Detecting Incognito link above

In main.html add an iframe,

 <iframe id='testFrame' name='testFrame' onload='setUniqueSource(this)' src='' style="width:0; height:0; visibility:hidden;"></iframe>

, and some JavaScript code:

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.html that are loaded into the 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.

share|improve this answer
    
That's pretty cool, i didn't realise that incognito mode doesn't highlight visited links. This requires user to click a link though. – Igor Zevaka May 26 '10 at 0:29
1  
No it doesn't, the iframe "clicks" the link. – kibibu May 26 '10 at 3:31
29  
This doesn't actually work since most browsers don't expose :visited style information through javascript. The CSS interface will say as if the link has the non-visited color. This is a security measure that has been in WebKit- and Gecko browsers at least since 2010. This was to protect the user's history (e.g. one could try all possible URLs, and send the visited ones to a third party. This way one could get access to tokens in urls and what not). – Krinkle Oct 11 '12 at 2:52
    
Official Mozilla article explaining the privacy changes regarding :visited: hacks.mozilla.org/2010/03/… – Denilson Sá Maia Feb 29 '16 at 18:04

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:

  • 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.

As you can see the differences between normal browsing and incognito happen after you 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.

share|improve this answer
    
Recently, it disables all extensions except those extensions which were specifically marked by the user as incognito safe. – Tiberiu-Ionuț Stan Oct 12 '12 at 17:39

If you are developing an Extension then you can use the tabs API to determine if a window/tab incognito.

More information can be found on code.google.com.

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.

I haven't tested it but I suspect all calls to localStorage fail too.

share|improve this answer
    
I am developing an extension (well, user-script), but didn't make that clear. I've re-asked the question (see the original question for a link). If you want to put this answer over there I'll mark it as accepted. Thanks. – RodeoClown May 26 '10 at 21:21
    
@RodeoClown done. but someone has already answered the question. – Kinlan May 26 '10 at 22:03

The documentation for incognito mode specifically says that websites won't behave differently. I believe this means that the answer is no.

share|improve this answer
5  
I know this answer is 4 years old, but recently Netflix implemented the "incognito" detection featured, and that piqued my interest to research how they pull that off. If you visit Netflix on incognito, netflix does not let you play videos. – ILikeTacos Jan 3 '15 at 4:53
    
Verified <Chrome>: "Incognito Mode Error Your browser appears to be in Incognito Mode." – Pete Alvin Apr 18 '16 at 13:53

Quick copy-paste function based on Alok's Answer (note: this is asynchronous)

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') });
share|improve this answer
    
this is not working anymore (maybe it worked in the past) – Alexandru Rada May 2 at 8:35
    
Just updated my chrome now and it still works. I see you posted the same above and then said you were mistaken, just pointing this out for future viewers (feel free to delete your comment, I will delete this one if you do). – aljgom May 3 at 9:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.