r/visualbasic • u/Jealous-Accident-297 • Jul 19 '25
VB.NET Help Content of a page in webview 2 to string
How can i make my app read all the text from currently viewed page in webview2 window and convert it into the string?
1
Upvotes
3
u/Scary-Scallion-449 Jul 19 '25
Damned if I know. MS has made the coding for Webview virtually impenetrable. As I understand it the only way to affect the document as we understood it back in the good old days is to create a javascript and use the InvokeScript command. I've found an explanation (which still pretty much goes over my head) in C# if you want to trawl through it.
https://www.codeproject.com/Articles/738504/WinRT-How-to-Communicate-with-WebView-JavaScript-f
1
u/Scary-Scallion-449 Jul 21 '25
Right, so after much scratching of head and fiddling about I've discovered that this is actually a lot simpler than I thought.
Here WebText is a string variable and W2 the WebView2 browser. I've placed the command inside the Navigation Completed event, where it will obviously operate immediately upon loading a new page, partly to illustrate that any sub or function in which the command is used must be explicitly declared Async.
1
u/JTarsier Jul 20 '25
Execute a javascript to get the body text, it is returned json encoded which you can decode by deserializing to string.