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

纵饮孤独 提交于 2019-11-30 06:41:44

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.

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

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
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!