WebBrowse, Invoke javascript, C# API, outer html

來源:互聯網
上載者:User

用webbrowser控制項navigate到某個頁面。

可以使用webbrowser.document.invoke來調用document裡的javascript。

我的目的是要把頁面儲存下來,我一開始用的是document.execcommand方法。這個函數接受很多參數,對應瀏覽器的各個功能表命令,如另存新檔,列印等等。
這些命令參數的英文名是command identifier,具體的參數列表,在下面這個頁面有詳細介紹。我順便把它們附在最後。
(http://www.tutor.nsu.ru/library/default.asp?url=/workshop/author/dhtml/reference/commandids.asp)
我用execcommand("saveas",true,path)進行儲存。
文檔上對saveas的介紹是:其第2個參數是個布爾值,設為真時會顯示另存新檔對話方塊,設為false時不顯示對話方塊。
但是實際上,無論設為真或假,都會顯示對話方塊。對於這一點,有另一篇文章說是為了防止大批量拷貝網頁,阻止盜取他人的資料。
呵呵,挺滑稽的。難道這個小小的參數能擔負起這麼重大的責任?

既然這個視窗一定會出現,那怎麼解決它呢。我想可以用程式來類比人的點擊動作。具體是這麼做的。通過對話方塊標題尋找儲存對話方塊,當然也可以用其他特徵去尋找,
找到對話方塊之後,再定位對話方塊中的儲存按鈕,這兩個尋找都可以用FindWindow來做。找到後給這個對話方塊發個訊息就可以了,用SendMessage,訊息則用WM_CLICK。
因為我用的是c#,而FindWindow,SendMessage並不是Managed 程式碼,那如何在c#中使用它們呢?
下面這篇文章介紹了這方面的知識。
用C#調用Windows API和其它進程通訊 (http://www.cnblogs.com/index/archive/2005/01/16/92651.html)
關鍵就是要引入這些Unmanaged 程式碼,用下面的語句就可以了。
    #region Dll Import
 
    [DllImport("User32.dll",EntryPoint="FindWindow")]
    private static extern IntPtr FindWindow(string lpClassName,
string lpWindowName);
 
    [DllImport("user32.dll",EntryPoint="FindWindowEx")]
    private static extern IntPtr FindWindowEx(IntPtr hwndParent,
IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
 
    [DllImport("User32.dll",EntryPoint="SendMessage")]
    private static extern int SendMessage(IntPtr hWnd,
int Msg, IntPtr wParam, string lParam);
 
    #endregion

我把尋找視窗和儲存按鈕,並發送點擊訊息的代碼寫在另一個線程函數裡,啟動新的線程來運行這個函數。
當程式開始後,就啟動這個線程,讓它不停的監視是否出現這樣的對話方塊。

但這種方法效果很不好,運行速度不快,另外因為我對線程這一塊還不熟,運行起來總是出這麼一個問題:
嘗試讀取或寫入受保護的記憶體。這通常指示其他記憶體已損壞。
一直沒能解決。

後來終於找到另外一個辦法,不在走調用儲存對話方塊這條途徑,而是直接把頁面html代碼儲存到檔案。
這個思路很直觀,其實我早就想過這麼做,但剛開始是用docuent.write()方法來寫,另外也不知道頁面的html代碼是存在
document對象的哪個屬性裡。所以沒辦法做到。
下面的代碼則示範了如何用這個方法來儲存,超級簡單,直觀。
HtmlDocument doc = webBrowser1.Document;
HtmlElementCollection elems = doc.GetElementsByTagName("HTML");
if (elems.Count == 1)
 {

    HtmlElement elem = elems[0];
    StreamWriter wrt = new StreamWriter(path);
    wrt.Write(elem.OuterHtml.ToString());
    }
這樣,我的工作就完成了

 

下面是sendmessage中一些訊息對應的值,這些值在windows api中定義

const int WM_GETTEXT = 0x000D;
const int WM_SETTEXT = 0x000C;
const int WM_CLICK = 0x00F5;

const int WM_CLOSE = 0x0010

 

附上command identifiers
Command identifiers specify an action to take on the given object. Use them with the following methods:

execCommand
queryCommandEnabled
queryCommandIndeterm
queryCommandState
queryCommandSupported
queryCommandValue

Command Identifiers
2D-Position
Allows absolutely positioned elements to be moved by dragging.

AbsolutePosition
Sets an element's position property to "absolute."

BackColor
Sets or retrieves the background color of the current selection.

BlockDirLTR
Not currently supported.

BlockDirRTL
Not currently supported.

Bold
Toggles the current selection between bold and nonbold.

BrowseMode
Not currently supported.

Copy
Copies the current selection to the clipboard.

CreateBookmark
Creates a bookmark anchor or retrieves the name of a bookmark anchor for the current selection or insertion point.

CreateLink
Inserts a hyperlink on the current selection, or displays a dialog box enabling the user to specify a URL to insert as a hyperlink on the current selection.

Cut
Copies the current selection to the clipboard and then deletes it.

Delete
Deletes the current selection.

DirLTR
Not currently supported.

DirRTL
Not currently supported.

EditMode
Not currently supported.

FontName
Sets or retrieves the font for the current selection.

FontSize
Sets or retrieves the font size for the current selection.

ForeColor
Sets or retrieves the foreground (text) color of the current selection.

FormatBlock
Sets the current block format tag.

Indent
Increases the indent of the selected text by one indentation increment.

InlineDirLTR
Not currently supported.

InlineDirRTL
Not currently supported.

InsertButton
Overwrites a button control on the text selection.

InsertFieldset
Overwrites a box on the text selection.

InsertHorizontalRule
Overwrites a horizontal line on the text selection.

InsertIFrame
Overwrites an inline frame on the text selection.

InsertImage
Overwrites an image on the text selection.

InsertInputButton
Overwrites a button control on the text selection.

InsertInputCheckbox
Overwrites a check box control on the text selection.

InsertInputFileUpload
Overwrites a file upload control on the text selection.

InsertInputHidden
Inserts a hidden control on the text selection.

InsertInputImage
Overwrites an image control on the text selection.

InsertInputPassword
Overwrites a password control on the text selection.

InsertInputRadio
Overwrites a radio control on the text selection.

InsertInputReset
Overwrites a reset control on the text selection.

InsertInputSubmit
Overwrites a submit control on the text selection.

InsertInputText
Overwrites a text control on the text selection.

InsertMarquee
Overwrites an empty marquee on the text selection.

InsertOrderedList
Toggles the text selection between an ordered list and a normal format block.

InsertParagraph
Overwrites a line break on the text selection.

InsertSelectDropdown
Overwrites a drop-down selection control on the text selection.

InsertSelectListbox
Overwrites a list box selection control on the text selection.

InsertTextArea
Overwrites a multiline text input control on the text selection.

InsertUnorderedList
Toggles the text selection between an ordered list and a normal format block.

Italic
Toggles the current selection between italic and nonitalic.

JustifyCenter
Centers the format block in which the current selection is located.

JustifyFull
Not currently supported.

JustifyLeft
Left-justifies the format block in which the current selection is located.

JustifyNone
Not currently supported.

JustifyRight
Right-justifies the format block in which the current selection is located.

LiveResize
Causes the MSHTML Editor to update an element's appearance continuously during a resizing or moving operation, rather than updating only at the completion of the move or resize.

MultipleSelection
Allows for the selection of more than one site selectable element at a time when the user holds down the SHIFT or CTRL keys.

Open
Not currently supported.

Outdent
Decreases by one increment the indentation of the format block in which the current selection is located.

OverWrite
Toggles the text-entry mode between insert and overwrite.

Paste
Overwrites the contents of the clipboard on the current selection.

PlayImage
Not currently supported.

Print
Opens the print dialog box so the user can print the current page.

Redo
Not currently supported.

Refresh
Refreshes the current document.

RemoveFormat
Removes the formatting tags from the current selection.

RemoveParaFormat
Not currently supported.

SaveAs
Saves the current Web page to a file.

SelectAll
Selects the entire document.

SizeToControl
Not currently supported.

SizeToControlHeight
Not currently supported.

SizeToControlWidth
Not currently supported.

Stop
Not currently supported.

StopImage
Not currently supported.

StrikeThrough
Not currently supported.

Subscript
Not currently supported.

Superscript
Not currently supported.

UnBookmark
Removes any bookmark from the current selection.

Underline
Toggles the current selection between underlined and not underlined.

Undo
Not currently supported.

Unlink
Removes any hyperlink from the current selection.

Unselect
Clears the current selection.

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.