Repository navigation
Description
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) 🔬
- Run the SamplesApp on WebAssembly in Chrome or Edge.
- Open the
WebView2_Foldersample (category WebView). - 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.jsL3 and L9 (webkit.messageHandlershas the same mistake), and the alert is at L15. Comparing with'undefined'fixes it. - The inline HTML of two runtime tests in
Given_WebView2.csuses 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_FolderandWebView2_MouseEventssamples and byGiven_WebView2.When_LocalFolder_File(through theUnoNativeAssetsvirtual host mapping). We noticed it while adding WebAssembly runtime tests that load this page (fix(wasm): Complete WebView2 cross-origin navigations #25737).
Activity