简体   繁体   English

如何在Webkit.Net中选择用户控件WebKitBrowser的所有文本?

[英]How do I select all text of the user control WebKitBrowser in Webkit.Net?

There is a method SelectedText in WebKitBrowser, however, no SelectAll . WebKitBrowser中有一个方法SelectedText ,但是没有SelectAll Is there a way for me to do something like the following: 有没有一种方法可以让我执行以下操作:

string GetAllTextOfBrowserAsPlainText(WebKitBrowser webKitBrowser)
{
    webKitBrowser.SelectAll(); //Doesn't exist
    return webKitBrowser.SelectedText;
}

My goal is to render a webpage entirely (including styling) and then copy the content of a webpage in plane text as if I used copy/paste in my browser. 我的目标是完全渲染网页(包括样式),然后将网页内容复制为平面文字,就像在浏览器中使用复制/粘贴一样。

Getting the InnerText or working with the HTML directly is not an option. 不能选择获取InnerText或直接使用HTML。

I have tried the WebBrowser UC with WebBrowser1.Document.ExecCommand to capture the text however I couldn't get the styling to work properly. 我已经尝试使用WebBrowser1.Document.ExecCommand来捕获WebBrowser UC,但是我无法使样式正常工作。 I am now trying WebKit, and I am so close to getting what I want. 我现在正在尝试WebKit,而且我已经很接近获得想要的东西。 Any help? 有什么帮助吗? Does ExecCommand("SelectAll",...) exist for WebKit? WebKit是否存在ExecCommand("SelectAll",...)

Try using WebClient 尝试使用WebClient

       using (WebClient wc = new WebClient())
      string mystring= wc.DownloadString("http://yoururl.com");

I solved the problem by adding javascript to the end of the downloaded html. 我通过在下载的html的末尾添加javascript解决了该问题。 Calling js from code was tricky, since it seems like WebKit.Net has trouble with doing this directly. 从代码中调用js非常棘手,因为WebKit.Net似乎很难直接执行此操作。 The following is not my final production code, but will help anybody in the same spot along the way: 以下不是我的最终产品代码,但会在整个过程中帮助任何人:

private WebKitBrowser _browser = ...;
private string _selectAllCopyScript = "<script>document.execCommand('SelectAll', false, null);document.execCommand('Copy', false, null); </script>";
private string _plain = ...;

Form1()
{
    ...
    _browser.DocumentCompleted += OnDocumentCompleted;
}

private string GetAllTextOfBrowserAsPlainText(String html)
{
    _browser.Focus();
    _browser.DocumentText = html + _selectAllCopyScript; //Calls OnDocumentCompleted when done
}

private void OnDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    _plain = Clipboard.GetText();
    Clipboard.Clear();
}

I wish I found a solution which didn't have to use the clipboard. 我希望我找到了一个不必使用剪贴板的解决方案。 The final plain text copied html is stored in _plain . 复制的最终纯文本html存储在_plain

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM