Can you determine if Chrome is in incognito mode via a script?

JavascriptGoogle ChromeIncognito Mode

Javascript Problem Overview


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.

Javascript Solutions


Solution 1 - Javascript

The functionality of this answer is Chrome version dependant. The most recent comment was this works in v90

Yes. The FileSystem API is disabled in incognito mode. Check out https://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"));
    }

Solution 2 - Javascript

In Chrome 74 to 84.0.4147.135 you can determine this by estimating the available file system storage space

See the 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')
}

Solution 3 - Javascript

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.

Solution 4 - Javascript

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.

Solution 5 - Javascript

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

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

Solution 6 - Javascript

Update This seems to not be working anymore


This uses a promise to wait for the asynchronous code to set a flag, so we can use it synchronously afterward.

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');

Solution 7 - Javascript

For those looking for a solution, here's a brief rundown of the current methods of detecting Private Browsing modes in various browsers as of October 2021:

  • Chromium: Similar to Vinnie James's answer, call navigator.storage.estimate(), grab the quota property and compare it to performance.memory.jsHeapSizeLimit. If the quota property is less than jsHeapSizeLimit, it's incognito. If jsHeapSizeLimit is undefined, use 1073741824 (1 GiB).

  • Safari for macOS: Use safari.pushNotification.requestPermission on a non-existent push server & grab the error. If "gesture" does not appear in the error, it's in private mode.

  • Safari for iOS: Create an iframe & add an error event listener using contentWindow.applicationCache on the iframe. If the error trips, it's in private mode.

  • Firefox: navigator.serviceWorker will be undefined in a private window.

  • Internet Explorer: window.indexedDB will be undefined in InPrivate mode.

You can see an implementation of these methods in the detectIncognito script I have available on GitHub.

Solution 8 - Javascript

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

Update - not working anymore

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') });

Solution 9 - Javascript

Here is the suggested answer written in ES6 syntaxt and slightly cleand up.

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)

Solution 10 - Javascript

This works in May 2021: https://jsfiddle.net/2b1dk8oa/

The script has to be executed in a webpage, which is in an iframe.

try{
    var ls = localStorage;
    alert("You are not in Incognito Mode.");
}
catch(e) {  alert("You are in Incognito Mode.");  }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRodeoClownView Question on Stackoverflow
Solution 1 - JavascriptAlokView Answer on Stackoverflow
Solution 2 - JavascriptVinnie JamesView Answer on Stackoverflow
Solution 3 - JavascriptJHurrahView Answer on Stackoverflow
Solution 4 - JavascriptIgor ZevakaView Answer on Stackoverflow
Solution 5 - JavascriptKinlanView Answer on Stackoverflow
Solution 6 - JavascriptaljgomView Answer on Stackoverflow
Solution 7 - JavascriptJoe RutkowskiView Answer on Stackoverflow
Solution 8 - JavascriptaljgomView Answer on Stackoverflow
Solution 9 - JavascriptnikksanView Answer on Stackoverflow
Solution 10 - JavascriptIvan KuckirView Answer on Stackoverflow