在 WInRT 应用程序中下载 WebView 内容
Download WebView content in WInRT application
我正在尝试为 Windows 10 构建一个通用的 rss 应用程序,它可以下载完整文章页面的内容以供离线咨询。
所以在 Whosebug 上花了很多时间之后,我找到了一些代码:
HttpClientHandler handler = new HttpClientHandler { UseDefaultCredentials = true, AllowAutoRedirect = true };
HttpClient client = new HttpClient(handler);
HttpResponseMessage response = await client.GetAsync(ni.Url);
response.EnsureSuccessStatusCode();
string html = await response.Content.ReadAsStringAsync();
但是这个解决方案在某些动态调用内容的网页上不起作用。
所以剩下的备选方案似乎是:将网页加载到 WinRT 的 Webview 控件中,然后以某种方式复制并粘贴呈现的文本。
但是,Webview 没有实现任何 copy/paste 方法或类似方法,因此无法轻松实现。
最后我在 Whosebug (Copying the content from a WebView under WinRT) 上发现了这个 post,它似乎正在处理与我的问题完全相同的问题,解决方案如下;
使用 Web 视图中的 InvokeScript 方法通过 javascript 函数复制和粘贴内容。
它说:"First, this javascript function must exist in the HTML loaded in the webview."
function select_body() {
var range = document.body.createTextRange();
range.select();
}
然后是"use the following code:"
// call the select_body function to select the body of our document
MyWebView.InvokeScript("select_body", null);
// capture a DataPackage object
DataPackage p = await MyWebView.CaptureSelectedContentToDataPackageAsync();
// extract the RTF content from the DataPackage
string RTF = await p.GetView().GetRtfAsync();
// SetText of the RichEditBox to our RTF string
MyRichEditBox.Document.SetText(Windows.UI.Text.TextSetOptions.FormatRtf, RTF);
但是它没有说的是 如果我正在加载的页面中不存在 javascript 函数,如何注入它?
如果你有这样的 WebView:
<WebView Source="http://kiewic.com" LoadCompleted="WebView_LoadCompleted"></WebView>
结合使用InvokeScriptAsync
和eval()
获取文档内容:
private async void WebView_LoadCompleted(object sender, NavigationEventArgs e)
{
WebView webView = sender as WebView;
string html = await webView.InvokeScriptAsync(
"eval",
new string[] { "document.documentElement.outerHTML;" });
// TODO: Do something with the html ...
System.Diagnostics.Debug.WriteLine(html);
}
我正在尝试为 Windows 10 构建一个通用的 rss 应用程序,它可以下载完整文章页面的内容以供离线咨询。
所以在 Whosebug 上花了很多时间之后,我找到了一些代码:
HttpClientHandler handler = new HttpClientHandler { UseDefaultCredentials = true, AllowAutoRedirect = true };
HttpClient client = new HttpClient(handler);
HttpResponseMessage response = await client.GetAsync(ni.Url);
response.EnsureSuccessStatusCode();
string html = await response.Content.ReadAsStringAsync();
但是这个解决方案在某些动态调用内容的网页上不起作用。
所以剩下的备选方案似乎是:将网页加载到 WinRT 的 Webview 控件中,然后以某种方式复制并粘贴呈现的文本。 但是,Webview 没有实现任何 copy/paste 方法或类似方法,因此无法轻松实现。
最后我在 Whosebug (Copying the content from a WebView under WinRT) 上发现了这个 post,它似乎正在处理与我的问题完全相同的问题,解决方案如下;
使用 Web 视图中的 InvokeScript 方法通过 javascript 函数复制和粘贴内容。
它说:"First, this javascript function must exist in the HTML loaded in the webview."
function select_body() {
var range = document.body.createTextRange();
range.select();
}
然后是"use the following code:"
// call the select_body function to select the body of our document
MyWebView.InvokeScript("select_body", null);
// capture a DataPackage object
DataPackage p = await MyWebView.CaptureSelectedContentToDataPackageAsync();
// extract the RTF content from the DataPackage
string RTF = await p.GetView().GetRtfAsync();
// SetText of the RichEditBox to our RTF string
MyRichEditBox.Document.SetText(Windows.UI.Text.TextSetOptions.FormatRtf, RTF);
但是它没有说的是 如果我正在加载的页面中不存在 javascript 函数,如何注入它?
如果你有这样的 WebView:
<WebView Source="http://kiewic.com" LoadCompleted="WebView_LoadCompleted"></WebView>
结合使用InvokeScriptAsync
和eval()
获取文档内容:
private async void WebView_LoadCompleted(object sender, NavigationEventArgs e)
{
WebView webView = sender as WebView;
string html = await webView.InvokeScriptAsync(
"eval",
new string[] { "document.documentElement.outerHTML;" });
// TODO: Do something with the html ...
System.Diagnostics.Debug.WriteLine(html);
}