1

I have a unique requirement where in my application I need to intercept user input on webview2 control and preprocess user input prior to posting.

I have started with a sample code from the following location :

https://learn.microsoft.com/en-us/microsoft-edge/webview2/gettingstarted/winforms

I have tried following callbacks but none of them is called when the user enter any information on the input box or click the button

NavigationStarting SourceChanged ContentLoading HistoryChanged NavigationCompleted

Only time I get the handle is when it requires to reload.

Any thoughs ..

gk bheli
  • 11
  • 3
  • 1
    You must write some javascript (and inject it) to catch the events, then use the `window.chrome.webview.postMessage` method to post the value to your C# code. See https://learn.microsoft.com/da-dk/microsoft-edge/webview2/gettingstarted/winforms#step-8---communication-between-host-and-web-content – Poul Bak Mar 13 '21 at 14:10
  • Thank you. I have been to that page. Is there any example available for code injection. – gk bheli Mar 13 '21 at 17:10
  • You can use `CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync(String)` method - check this: https://learn.microsoft.com/da-dk/dotnet/api/microsoft.web.webview2.core.corewebview2.addscripttoexecuteondocumentcreatedasync?view=webview2-dotnet-1.0.774.44#Microsoft_Web_WebView2_Core_CoreWebView2_AddScriptToExecuteOnDocumentCreatedAsync_System_String_ – Poul Bak Mar 13 '21 at 17:18
  • You might be inspired by my answer here (it shows how to catch mouse clicks): https://stackoverflow.com/questions/65354344/how-detect-a-mouse-click-in-webview2-c-vb-net/65398068#65398068 – Poul Bak Mar 13 '21 at 17:21

0 Answers0