【发布时间】:2015-12-10 13:56:54
【问题描述】:
我正在尝试为 Windows 10 构建一个通用 rss 应用程序,该应用程序可以下载全文页面的内容以进行离线咨询。
所以在 stackoverflow 上花了很多时间后,我找到了一些代码:
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 没有实现任何复制/粘贴方法或类似方法,因此无法轻松完成。
最后我在 stackoverflow (Copying the content from a WebView under WinRT) 上发现了这篇文章,它似乎正在使用以下解决方案处理与我相同的问题;
使用 webview 中的 InvokeScript 方法通过 javascript 函数复制和粘贴内容。
它说:“首先,这个javascript函数必须存在于webview中加载的HTML中。”
function select_body() {
var range = document.body.createTextRange();
range.select();
}
然后“使用以下代码:”
// 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函数,如何注入它?
【问题讨论】:
标签: javascript c# webview windows-runtime winrt-xaml