2015-11-06 41 views
3

我只能在UWP Win 10之前找到舊的過時的答案。我知道如何以舊的方式做到這一點,但它給我帶來了問題。UWP Webview從網頁獲取鏈接到數組

我到目前爲止是在下面,請注意,問題似乎在於它不是通過標記名稱命令執行元素,就像我已經被告知它應該。儘管將其改爲內部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 

回答

2

InvokeScriptAsync只能返回腳本調用的字符串結果。

返回值

此方法返回時,腳本調用的字符串結果。

所以,如果你想獲得鏈接形成一個網頁,你需要把所有的鏈接到一個字符串返回。對於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代碼應該是相似的。