Is it possible to determine if Chrome is in incognito mode via a user-script?
Asked Answered
S

3

25

I asked this question before but didn't make it clear that I meant in user script, not in JavaScript from a webpage.So I'll be more clear now.

Is it possible to determine if Google Chrome is in incognito mode via a user-script (basically a script run as an extension in the browser, not a script being run on a webpage)?

Sauncho answered 26/5, 2010 at 21:15 Comment(2)
why do you want to know this?Rillet
I'm looking to write and extension that closes the tab if it is opened in incognito mode, as Google hasn't provided a way of not having it present.Sauncho
S
8

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 or a userscript, 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.

Susiesuslik answered 26/5, 2010 at 22:2 Comment(2)
localStorage works, but the values are discarded when the private session ends. So, that would not be an effective detection method for the current session.Yasukoyataghan
localStorage behaves like sessionStorage when chrome is in InCognito Mode.Prowel
S
11

To detect whether a window is in incognito mode, check the incognito property of the relevant Tab or Window object. For example:

var bgPage = chrome.extension.getBackgroundPage();

function saveTabData(tab, data) {
  if (tab.incognito) {
    bgPage[tab.url] = data;       // Persist data ONLY in memory
  } else {
    localStorage[tab.url] = data; // OK to store data
}

http://code.google.com/chrome/extensions/overview.html

Svend answered 26/5, 2010 at 21:20 Comment(0)
S
8

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 or a userscript, 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.

Susiesuslik answered 26/5, 2010 at 22:2 Comment(2)
localStorage works, but the values are discarded when the private session ends. So, that would not be an effective detection method for the current session.Yasukoyataghan
localStorage behaves like sessionStorage when chrome is in InCognito Mode.Prowel
M
3

Nowadays it's quite easy to do this from a content script. Just use

if(chrome.extension.inIncognitoContext) {
    //you're incognito
} else {
    //you're not
}
Melodics answered 10/8, 2016 at 7:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.