Dokug
Dokug

Reputation: 213

.NET MAUI webview view actual HTML

Is it possible to retrieve the HTML a .NET MAUI WebView is currently displaying?
If so, how?
Unfortunately, I can't find anything on it inside the official documentation.

.NET MAUI WebView documentation

I am trying to retrieve the currently displayed HTML as a string, after a JavaScript event changed the HTML body content, to read out some meta-data.

Upvotes: 4

Views: 5428

Answers (2)

Liqun Shen-MSFT
Liqun Shen-MSFT

Reputation: 8220

You may use Navigated event to detect if page changes:

In .xaml,

<WebView x:Name="webview" ... Navigated="webview_Navigated" />

In .cs, implement Event Handler

async void webview_Navigated(System.Object sender, Xamarin.Forms.WebNavigatedEventArgs e)
{
    var webView = sender as WebView;
    HttpClient client = new HttpClient();
    // Get the html
    var html = await client.GetStringAsync((webView.Source as UrlWebViewSource).Url);
    }

Furthermore, you may want JS/C# interaction on MAUI WebView. You could refer to this SO issue: JS/.NET interact on MAUI WebView.

Hope it works for you.

Upvotes: -1

Poulpynator
Poulpynator

Reputation: 1176

I don't think you can access it directly (like with WebView2). However you can call Javascript, which have access to the HTML :

string result = await webView.EvaluateJavaScriptAsync($"document.documentElement.innerHTML");

Upvotes: 6

Related Questions