Categories
JavaScript Answers

How to determine if Chrome is in incognito mode via a script with JavaScript?

Spread the love

Sometimes, we want to determine if Chrome is in incognito mode via a script with JavaScript.

In this article, we’ll look at how to determine if Chrome is in incognito mode via a script with JavaScript.

How to determine if Chrome is in incognito mode via a script with JavaScript?

To determine if Chrome is in incognito mode via a script with JavaScript, we check if the file systems API can’t be used.

For instance, we write

const 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")
  );
}

to check if fs is undefined.

If it’s undefined, then the script is run in incognito mode.

Otherwise, we call fs with callbacks to check if it’s executed successfully.

The 3rd argument is the success callback and the 4th is the error callback.

If the error callback is run, then the user is in incognito mode.

Conclusion

To determine if Chrome is in incognito mode via a script with JavaScript, we check if the file systems API can’t be used.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

One reply on “How to determine if Chrome is in incognito mode via a script with JavaScript?”

Leave a Reply

Your email address will not be published. Required fields are marked *