UWP Webview 从网页获取链接到数组

UWP Webview get links from webpage into an array

我只能找到 UWP Win 10 之前的旧答案。我知道如何用旧方法来做,但它给我带来了问题。

目前我所知道的如下,请注意问题似乎出在 VB 中,它没有像我被告知的那样按标签名称命令执行元素。不过,将其更改为内部 HTML,它将用整页填充 html 变量。所以我似乎无法自己获取链接。

感谢任何帮助!谢谢!

XAML

<Page
    x:Class="webviewMessingAround.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:webviewMessingAround"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <WebView x:Name="webview" Source="http://regsho.finra.org/regsho-December.html" DOMContentLoaded="WebView_DOMContentLoaded" />
            <Button x:Name="button" HorizontalAlignment="Left" Margin="145,549,0,0" VerticalAlignment="Top">
                <Button x:Name="button1" Click="button_Click" Content="Button" Height="58" Width="141"/>
            </Button>
        </Grid>
    </Grid>
</Page>

VB代码

 Private Async Sub webview_DOMContentLoaded(sender As WebView, args As WebViewDOMContentLoadedEventArgs) Handles webview.DOMContentLoaded

        Dim html = Await webview.InvokeScriptAsync("eval", ({"document.getElementsByTagName('a');"}))

        'Debug.WriteLine(html)

    End Sub

InvokeScriptAsync只能return脚本调用的字符串结果

Return value

When this method returns, the string result of the script invocation.

所以如果你想从一个网页中获取链接,你需要把所有的链接放到一个字符串中到return。对于 C# 示例:

string html = await webview.InvokeScriptAsync("eval", new string[] { "[].map.call(document.getElementsByTagName('a'), function(node){ return node.href; }).join('||');" });
System.Diagnostics.Debug.WriteLine(html);

这里我用

[].map.call(document.getElementsByTagName('a'), function(node){ return node.href; }).join('||');

将所有链接放入一个字符串中。您可能需要更改此 JavaScript 代码以实现您自己的代码。

在此之后,您可以将字符串拆分成一个数组,如:

var links = html.Split(new[] { "||" }, StringSplitOptions.RemoveEmptyEntries);

虽然我用的是C#,但是VB代码应该是差不多的。