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

标签 javascript vb.net xaml webview uwp

我只能找到 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 value

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

所以如果你想获取一个网页的链接,你需要把所有的链接放到一个字符串中返回。对于 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的代码应该差不多。

关于javascript - UWP Webview 从网页获取链接到数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33574852/

相关文章:

javascript - 插入数组后 Angular 清除对象

javascript - 你可以使用 setInterval 来异步调用函数吗?

c# - Log4Net 如何在没有时间的情况下记录纯文本

c# - WPF:使用 .ico 文件作为工具栏的图像?或者需要转换为 XAML?

c# - 无法在 WPF 圆形按钮 (XAML) 中获取边框

javascript - 根据偏移量更新表格单元格

javascript - Fetch Api 获取参数

mysql - 从数据库中检索空日期到 vb.net

.net - DataTable.Select ("Bla=' Blablabla '").Count Returns ' Count' 不是 'System.Array' 异常的成员

c# - 如何在 UWP MVVM 中使用 CalenderView