1
function test(){
alert(chrome.extension.inIncognitoContext);
if(chrome.extension.inIncognitoContext){
   chrome.tabs.query({currentWindow: true, active: true},function(tabs){
      alert(tabs[0].url);
    });
}
}

document.addEventListener('DOMContentLoaded', function() {
    var link = document.getElementById('link');
   // onClick's logic below:
    link.addEventListener('click', function() {
    test();
  });
});

this is my .js file for the chrome extension application where i am calling the test() function on clicking a button in the extension(below is the html code of the extension). In both incognito mode and default chrome it is alerting as false. I have a seen in another reference to use the boolean variable chrome.extension.inIncognitoContext to check this. Am I wrong ,are is there any problem with the code

<!doctype html>
<!--
This page is shown when the extension button is clicked, because the
"browser_action" field in manifest.json contains the "default_popup" key with
value "popup.html".
-->
<html>
<head>
<title>Getting Started Extension's Popup</title>
<style>
  body {
    font-family: "Segoe UI", "Lucida Grande", Tahoma, sans-serif;
    font-size: 100%;
  }
  #status {
    /* avoid an excessively wide status text */
    white-space: pre;
    text-overflow: ellipsis;
    overflow: hidden;
    max-width: 400px;
  }
</style>

<!--
  - JavaScript and HTML must be in separate files: see our Content Security
  - Policy documentation[1] for details and explanation.
  -
  - [1]: https://developer.chrome.com/extensions/contentSecurityPolicy
 -->
<script src="popup.js"></script>
</head>
<body>
<button id= "link" onclick="test()">store the page</button>
</body>
</html>
Xan
  • 66,873
  • 13
  • 150
  • 174
murali krish
  • 1,227
  • 15
  • 21
  • @ Mr Xan my question is not how to call a function on button click, but to find out whether i am in incognto mode or not. Yes Mr Apsillers is correct – murali krish Jul 22 '15 at 13:38
  • @apsillers @Murali Mea culpa; I saw the offending `onclick` and did not read thoroughly. Reversing duplicate. – Xan Jul 22 '15 at 13:40
  • Do you have anything related to incognito in your manifest? – Xan Jul 22 '15 at 13:41
  • 1
    Could you also post your `manifest.json`? I am specifically interested in your `incognito` field. – apsillers Jul 22 '15 at 13:41

1 Answers1

5

The Chrome docs on isIncognitoContext have this to say about the property:

True for content scripts running inside incognito tabs, and for extension pages running inside an incognito process. The latter only applies to extensions with 'split' incognito_behavior.

Your code here is in a popup, which means it is an extension page. It is not a content script. According to the description above, extension pages only see a true value for isIncognitoContext if the manifest has the top-level property "incognito": "split".

You probably have "spanning" or possibly nothing at all. According to this page about incognito manifest options, "spanning" is the default:

The default for extensions and Chrome apps is "spanning", which means that it will run in a single shared process...

apsillers
  • 101,930
  • 15
  • 206
  • 224