Google Chrome App - check if app or regular site

泄露秘密 提交于 2019-12-11 03:23:56

问题


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 chrome)

Just FYI, here is my Chrome App manifest, please note I am running this in the developer mode (directly from the source, not packaged yet)

{
    "manifest_version": 2,
    "name": "Example KIOSK APP",
    "version": "1.1",
    "icons": {
        "16": "images/icon-16.png",
        "48": "images/icon-48.png",
        "128": "images/icon-128.png"
    },
    "app": {
        "background": {
            "scripts": ["background.js"],
            "persistent": true
        }
    },
    "kiosk_enabled": true,
    "offline_enabled": true,
    "permissions": [
        "system.display",
        "power",
        "webview",
        "fileSystem",
        "alwaysOnTopWindows",
        "system.storage",
         "<all_urls>"
    ]
}

Any suggestions much appreciated.


回答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
}



回答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
}


来源:https://stackoverflow.com/questions/26118171/google-chrome-app-check-if-app-or-regular-site

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