Skip to content

WebView test page (WebContent/js/site.js) shows an error alert in Chrome and Edge #25807

Open

Description

@mikernet

Current behavior 🐛

The web content that the SamplesApp and the WebView2 runtime tests load, src/SamplesApp/LinkedFiles/WebContent/index.html, runs js/site.js, which tests for WebView2's bridge with:

if (window.hasOwnProperty("chrome") && typeof chrome.webview !== undefined) {
    chrome.webview.postMessage(backgroundColor);
}

typeof returns a string, so typeof chrome.webview !== undefined is always true. Chromium-based browsers define window.chrome in every page, but chrome.webview exists only in WebView2. In a browser, chrome.webview.postMessage throws, and the catch shows an alert:

Error occurred: TypeError: Cannot read properties of undefined (reading 'postMessage')

On WebAssembly, WebView2 shows the page in an iframe in the browser, so opening the WebView2_Folder or WebView2_MouseEvents sample in Chrome or Edge shows this alert. The alert is modal, so the app is blocked until it's dismissed.

I checked it with master's index.html and site.js in an iframe in headless Chrome 156: the alert has the message above, typeof window.chrome is "object" and typeof chrome.webview is "undefined".

Expected behavior 🎯

No alert. When no host bridge is available, the page posts nothing. The WebView2_PostMessage_RemoveAdd sample already uses the right check, with the string 'undefined' (WebView2_PostMessage_RemoveAdd.xaml.cs L30).

How to reproduce it (as minimally and precisely as possible) 🔬

  1. Run the SamplesApp on WebAssembly in Chrome or Edge.
  2. Open the WebView2_Folder sample (category WebView).
  3. The alert appears when the page loads.

Opening src/SamplesApp/LinkedFiles/WebContent/index.html directly in Chrome shows the same alert.

Workaround 🛠️

Dismiss the alert.

Affected platforms 📱💻🖥️

WebAssembly (in Chromium-based browsers). This is test content only; nothing that ships in Uno packages is affected.

Uno.Sdk version (and other relevant versions) 📦

master at 4744356 (Uno 7.0 dev).

IDE version 🧑‍💻

N/A

Anything else we need to know? 💬

  • The checks are in site.js L3 and L9 (webkit.messageHandlers has the same mistake), and the alert is at L15. Comparing with 'undefined' fixes it.
  • The inline HTML of two runtime tests in Given_WebView2.cs uses the same checks and alert (L458-L470, L531-L540). That class excludes WebAssembly today, so they don't show it yet.
  • The page is loaded by the WebView2_Folder and WebView2_MouseEvents samples and by Given_WebView2.When_LocalFolder_File (through the UnoNativeAssets virtual host mapping). We noticed it while adding WebAssembly runtime tests that load this page (fix(wasm): Complete WebView2 cross-origin navigations #25737).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions