从 Xamarin Forms WebView 获取 HTML 文档

Get HTML document from Xamarin Forms WebView

我试图将 webview 的内容作为标准 HTML 字符串获取,但结果是一个我似乎无法取消编码的编码字符串。这是一个简单的示例来说明:

XAML:

<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:d="http://xamarin.com/schemas/2014/forms/design" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="TestWebView.MainPage">
    <StackLayout>
        <WebView Source="https://www.microsoft.com" Navigated="WebView_Navigated" />
    </StackLayout>
</ContentPage>

代码隐藏:

        async void WebView_Navigated(System.Object sender, Xamarin.Forms.WebNavigatedEventArgs e)
        {
            var webView = sender as WebView;
            var html = await webView.EvaluateJavaScriptAsync("document.documentElement.outerHTML");

            System.Diagnostics.Debug.WriteLine(html);
        }

这是输出的前几行:

<html lang=\"en-us\" dir=\"ltr\" class=\"exp-4694 exp-4694T3 js exp-4065 exp-4065T1\"><head data-info=\"{&quot;v&quot;:&quot;1.0.7502.2744&quot;,&quot;a&quot;:&quot;1d1b0ad9-2b52-4c52-a443-3cb3a1c98a83&quot;,&quot;cn&quot;:&quot;OneDeployContainer&quot;,&quot;az&quot;:&quot;{did:92e7dc58ca2143cfb2c818b047cc5cd1, rid: OneDeployContainer, sn: marketingsites-prod-odeastus, dt: 2018-05-03T20:14:23.4188992Z, bt: 2020-07-16T09:31:28.0000000Z}&quot;,&quot;ddpi&quot;:&quot;2&quot;,&quot;dpio&quot;:&quot;&quot;,&quot;dpi&quot;:&quot;2&quot;,&quot;dg&quot;:&quot;uplevel.web.mobile.webkit.ios&quot;,&quot;th&quot;:&quot;default&quot;,&quot;m&quot;:&quot;en-us&quot;,&quot;l&quot;:&quot;en-us&quot;,&quot;mu&quot;:&quot;en-us&quot;,&quot;rp&quot;:&quot;/en-us/&quot;,&quot;f&quot;:&quot;sfwaaa,atperf680t2,5583t1,enablebuynowctrl,5355t1,3857t1,4694t2,3286t1,tasmigration010,cartemberpl,disablenorefunds,daconvertenabled,myflightcf&quot;,&quot;bh&quot;:{}}\" class=\"at-element-marker\">\n        <meta charset=\"UTF-8\">

在其他 webviews(如 Plugin.HybridWebView)中,结果符合预期。我正在尝试找到一种使用“官方”webview 的方法,因为其他大多数都不再维护。

我怎样才能 unencode/unescape 这个字符串或阻止 webview 首先对其进行编码?

实际上 Xamarin.Forms 返回的是本机平台按设计返回的任何内容,平台返回的是 Escaped html,第 3 方 WebView(Plugin.HybridWebView ) 正在做额外的事情取消转义 html 就像剥离无关的东西:'', '"' ...

作为一种解决方法,我们可以通过调用以下行

来获得没有任何转义引号的 html
private async void WebView_Navigated(object sender, WebNavigatedEventArgs e)
        {
            var webView = sender as WebView;
            // instead of getting the html with the EvaluateJavaScriptAsync method
            //var html = await webView.EvaluateJavaScriptAsync("document.documentElement.outerHTML");

            // they can get it with an HttpClient:
            HttpClient client = new HttpClient();
            var html = await client.GetStringAsync((webView.Source as UrlWebViewSource).Url);
        }

在这个问题的 GitHub 错误报告中,一位名为“jgold6”的用户提出了以下建议:

// fetch the document element
var page = await controller.EvaluateJavaScriptAsync("document.documentElement.outerHTML");

// Unescape that damn Unicode Java bull.
page = Regex.Replace(page, @"\[Uu]([0-9A-Fa-f]{4})", m => char.ToString((char)ushort.Parse(m.Groups[1].Value, NumberStyles.AllowHexSpecifier)));
page = Regex.Unescape(page);

这有效地解决了我的问题。这也是 Lucas 上面建议的。感谢大家对此的帮助。