How to detect in JavaScript if a page was loaded in a Chrome webview?

I’m writing a packaged app for Chrome that has a webview tag where I load my website.

Is it possible for the website JavaScript code to detect that it was loaded in a webview? The navigator.userAgent property has no clues.

  • Remove fullscreen window decoration/border Chrome OS app/kiosk mode
  • Google Packaged App Native Messaging
  • In Chrome packaged apps v2 how to check if the window is maximized?
  • Chrome extension: onclick() event not triggering alert() popup
  • How to read a properties file in javascript from project directory?
  • Reasons for porting a Cordova App to a Mobile Chrome App?
  • Is it possible to detect being inside of a chrome app?
  • How to read a properties file in javascript from project directory?
  • What are the pros and cons of Chrome Apps compared to Electron?
  • Angular routing without changing location
  • Is there an API for the chrome://webrtc-internals/ variables in javascript?
  • Chrome Packaged App - Message passing from background.js to another script page
  • 2 Solutions collect form web for “How to detect in JavaScript if a page was loaded in a Chrome webview?”

    Other approaches in addition to the one mentioned by Jivings:

    • Load a slightly different URL in the webview, e.g., http://example.com?in_webview=1, and reflect that in the JavaScript served by the site.
    • Same idea but use a #fragment. I don’t know for sure whether a #fragment will work correctly, but it if does, it’s nice because the server won’t get confused by a strange query param.
    • Using the embedder, insert a script into the webview’s DOM that does something different.

    When the webview has finished loading fire an event to the page inside:

    webview.addEventListener("loadstop", function () {
       contentWindow.postMessage('Hello from Chrome App!', targetOrigin)
    });
    

    Your content page can listen for this message using the regular postMessage API. If it receives a message you will know it is loaded in a webview.