Pergunta

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

Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top