2013-04-02 25 views
1

WebKitBrowser中有一種方法SelectedText,但是,沒有SelectAll。有沒有辦法爲我做類似如下:如何在Webkit.Net中選擇用戶控件WebKitBrowser的所有文本?

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

我的目標是使網頁完全(包括造型),然後複製網頁的內容在平面的文字,如果我使用複製/粘貼在我的瀏覽器中。

獲取InnerText或直接使用HTML不是一種選擇。

我試過WebBrowser UC WebBrowser1.Document.ExecCommand捕捉文本,但我無法讓樣式正常工作。我現在正在嘗試WebKit,並且我非常接近我想要的。任何幫助? WebKit是否存在ExecCommand("SelectAll",...)

回答

0

我通過添加JavaScript來下載的HTML年底解決了這個問題。從代碼中調用js是非常棘手的,因爲它看起來像WebKit.Net直接做這件事很麻煩。下面是不是我的最終的生產代碼,但將有助於任何人在前進的道路上的相同點:

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(); 
} 

我想我發現了沒有使用剪貼板的解決方案。最終純文本複製html存儲在_plain

0

嘗試使用Web客戶端

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

但是,謝謝您下載URL的HTML。我不是在尋找HTML,我希望顯示的內容爲純文本。如果你在你喜歡的網絡瀏覽器中進入一個網頁,然後按Ctrl + A和Ctrl + C,然後轉到記事本,然後按Ctrl + V,那麼記事本的內容就是我想要的字符串。也就是說,我希望應用樣式和任何腳本,然後獲取網頁顯示內容的平面文本。 –

+0

考慮使用像htmlagilitypack這樣的東西,從HTML單獨獲取文本部分 –

+0

http://htmlagilitypack.codeplex.com/sourcecontrol/changeset/view/62772#52179 –

相關問題