问题
I can only find old outdated answers for before UWP Win 10. I know how to do it the old ways, but it is giving me problems.
What I have so far is below, note the problem seem to lie in the VB where it isn't doing the element by tag name command like I've been told it should. Change that to inner HTML though, and it will populate the html variable with the full page. So I just can't get the links themselves it seems.
Any help is appreciated! Thanks!
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 Code
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
回答1:
The InvokeScriptAsync can only return the string result of the script invocation.
Return value
When this method returns, the string result of the script invocation.
So if you want get links form a web page, you need put all the links into a string to return. For a C# example:
string html = await webview.InvokeScriptAsync("eval", new string[] { "[].map.call(document.getElementsByTagName('a'), function(node){ return node.href; }).join('||');" });
System.Diagnostics.Debug.WriteLine(html);
Here I use
[].map.call(document.getElementsByTagName('a'), function(node){ return node.href; }).join('||');
to put all the links into a string. You may need to change this JavaScript code to implement your own.
After this you can split the string into a array like:
var links = html.Split(new[] { "||" }, StringSplitOptions.RemoveEmptyEntries);
Although I used C# for example, but the VB code should be similar.
来源:https://stackoverflow.com/questions/33574852/uwp-webview-get-links-from-webpage-into-an-array