2

Can i check with a chrome extension if incognito mode is activated or can i start Incognito mode with a button in my extension?

Xan
  • 74,770
  • 16
  • 179
  • 206
user1767637
  • 33
  • 1
  • 3
  • 1
    @RobW I believe this duplicate status is incorrect. The referenced question tells how to see if the extension is allowed to run in incognito mode, but this question is asking how to tell if you are currently running a window with incognito mode. Those are two different things. – bridiver Mar 28 '17 at 13:44

1 Answers1

11

To start with, a little explanation of how extensions interact with Incognito mode. Note that extensions by default cannot run in Incognito mode or affect/detect Incognito tabs. The user has to explicitly enable that in Chrome's Extension settings for your extension.

You can detect whether you are allowed Incognito access by checking chrome.extension.isAllowedIncognitoAccess() (note the async nature of it). If it calls back with false, you can guide the user to enable it (thanks to Rob W for the link).

What happens when you're granted access is controlled by the "incognito" setting in the manifest.

If set to "spanning" (default), you will have a single background page with access to both normal and incognito contexts. There are some limitations to that approach, though.

If set to "split", you'll have 2 instances. You can detect which one you are in with chrome.extension.inIncognitoContext.


Now, to your question. Suppose you've been allowed Incognito access.

For detecting Incognito mode in a given tab/window, you can inspect the incognito property of the corresponding object, i.e. returned by chrome.windows.getCurrent.

Code example for a browser action click:

chrome.browserAction.onClicked.addListener( function(tab) {
  if (tab.incognito) {
    // Clicked in an Incognito window
  } else {
    // Clicked in a normal window
  }
});

For opening a new Incognito tab/window, you can pass incognito: true in the object describing the tab/window you're creating.

You don't need the tabs permission for either of the above. See Tabs and Windows API docs for more details.

Xan
  • 74,770
  • 16
  • 179
  • 206
  • The `"incognito"` key in the manifest has nothing to do with enabling/disabling incognito mode. The closest one can get to get an "enable incognito" button is described in [How can I enable my chrome extension in incognito mode?](http://stackoverflow.com/questions/17438354/how-can-i-enable-my-chrome-extension-in-incognito-mode/17443982#17443982) – Rob W May 07 '14 at 23:02
  • @RobW Thanks for the useful link. I have not, however, claimed that this enables the incognito mode – Xan May 07 '14 at 23:04
  • Could you then clarify your intended meaning by mentioning the `"incognito"` manifest key? I don't know what you mean by "What happens next is controlled by the "incognito" setting in the manifest." in the context of the question. – Rob W May 07 '14 at 23:05
  • @RobW I will reword my answer, but this will have to wait until tomorrow. Thanks for pointing that out. – Xan May 07 '14 at 23:07
  • @RobW Restructured my answer. I also challenge the duplicate status: there is no information in the question to interpret is as "how to get Incognito access". – Xan May 08 '14 at 08:32
  • You're right, I've voted to re-open. – Rob W May 08 '14 at 11:38