Skip to content Skip to sidebar Skip to footer

Google Chrome App - Check If App Or Regular Site

I am working on project which will run as Chrome App & regular site. How can I test/check in my JS if I am in an Chrome App? (i.e. some functionality will only work under chrom

Solution 1:

Turns out the question meant to distinguish between identical code running in a webpage and inside a (regular) Chrome App window.

It is enough to test for Chrome App APIs that are never exposed to regular pages. An example of that would be to test for app.runtime:

if (window.chrome && chrome.app && chrome.app.runtime) {
  // Running inside a Chrome App context
} else {
  // Either not Chrome, or not as an app window
}

Solution 2:

Edit: This answer turned out not to be relevant to this particular question, but I think I will leave this in case someone stumbles upon this question with a hosted app.

I assume that by "run as Chrome App" you mean a hosted Chrome App.

In this case, it is enough to check chrome.app.isInstalled from the website's code. It's not easy to find this in the documentation, as it was apparently left out as some point, but I will put this as a reference. I just checked and it still works.

So:

// Website code
if (window.chrome && chrome.app && chrome.app.isInstalled) {
  // App is installed
} else if (chrome) {
  // In Chrome, but app is not installed: offer inline install?
} else {
  // Not in Chrome at all
}

Post a Comment for "Google Chrome App - Check If App Or Regular Site"