private async Task InitializeWebView2() { await _webview.CreateEnvironmentAsync().ConfigureAwait(true); await _webview.EnsureCoreWebView2Async(); ApplyDefaultWebViewSettings(); _webview.CoreWebView2.AddWebResourceRequestedFilter($"{AppOrigin}*", CoreWebView2WebResourceContextWrapper.All); var removeResourceCallback = _webview.CoreWebView2.AddWebResourceRequestedHandler(async(s, eventArgs) => { await HandleWebResourceRequest(eventArgs); }); // The code inside blazor.webview.js is meant to be agnostic to specific webview technologies, // so the following is an adaptor from blazor.webview.js conventions to WebView2 APIs await _webview.CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync(@" window.external = { sendMessage: message => { window.chrome.webview.postMessage(message); }, receiveMessage: callback => { window.chrome.webview.addEventListener('message', e => callback(e.data)); } }; ").ConfigureAwait(true); QueueBlazorStart(); var removeMessageCallback = _webview.CoreWebView2.AddWebMessageReceivedHandler(e => MessageReceived(new Uri(e.Source), e.WebMessageAsString)); }
private async Task InitializeWebView2() { await _webview.CreateEnvironmentAsync().ConfigureAwait(true); await _webview.EnsureCoreWebView2Async(); ApplyDefaultWebViewSettings(); _webview.CoreWebView2.AddWebResourceRequestedFilter($"{AppOrigin}*", CoreWebView2WebResourceContextWrapper.All); var removeResourceCallback = _webview.CoreWebView2.AddWebResourceRequestedHandler((s, eventArgs) => { // Unlike server-side code, we get told exactly why the browser is making the request, // so we can be smarter about fallback. We can ensure that 'fetch' requests never result // in fallback, for example. var allowFallbackOnHostPage = eventArgs.ResourceContext == CoreWebView2WebResourceContextWrapper.Document || eventArgs.ResourceContext == CoreWebView2WebResourceContextWrapper.Other; // e.g., dev tools requesting page source var requestUri = QueryStringHelper.RemovePossibleQueryString(eventArgs.Request.Uri); if (TryGetResponseContent(requestUri, allowFallbackOnHostPage, out var statusCode, out var statusMessage, out var content, out var headers)) { var headerString = GetHeaderString(headers); eventArgs.SetResponse(content, statusCode, statusMessage, headerString); } }); // The code inside blazor.webview.js is meant to be agnostic to specific webview technologies, // so the following is an adaptor from blazor.webview.js conventions to WebView2 APIs await _webview.CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync(@" window.external = { sendMessage: message => { window.chrome.webview.postMessage(message); }, receiveMessage: callback => { window.chrome.webview.addEventListener('message', e => callback(e.data)); } }; ").ConfigureAwait(true); QueueBlazorStart(); var removeMessageCallback = _webview.CoreWebView2.AddWebMessageReceivedHandler(e => MessageReceived(new Uri(e.Source), e.WebMessageAsString)); }