javascriptwebviewmicrosoft-edgebrowser-detectionwebview2

Detecting WebView2 vs standalone Edge browser?


I have a page that needs to render differently depending on whether it's being viewed from a WebView2 component (inside another application) or from a standalone Microsoft Edge browser. I tried distinguishing the two via the user agent string, but the strings are more or less identical (both contain "Edg"). Does anyone have a method to distinguish between a WebView2 and an Edge browser?


Solution

  • There's no perfect way for web content to identify that is running within a WebView2 because the host app of the WebView2 can customize so many aspects of the WebView2.

    For example, the host app can change the user agent via CoreWebView2Settings.UserAgent to appear to be Chrome or any other browser. Or the host app can inject script using CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync and change the DOM or other objects in script.

    However, if you are trying to determine if your web content is running within your own app, you can use those same customizations I mention above as a way for the host app to signal to the web content that the web content is running within a WebView2.