using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; #if WIN32 using MSHTML; #else using mshtml; #endif namespace sharpknife.Utils { class HtmlUtils { public static IHTMLHeadElement GetHead(IHTMLDocument2 document) { IHTMLElementCollection elements = ((IHTMLElementCollection)document.all).tags("head"); foreach (IHTMLElement element in elements) { return (IHTMLHeadElement)element; } return null; } public static IHTMLElementCollection GetElementCollection(IHTMLDocument2 document, string elementType) { IHTMLElementCollection elements = ((IHTMLElementCollection)document.body.all).tags(elementType); return elements; } public static string GetFrameContent(IHTMLDocument2 document) { string html = string.Empty; WebClient client = new WebClient(); //FramesCollection frames = document.frames; IHTMLElementCollection frames = ((IHTMLElementCollection)document.body.all).tags("iframe"); //string uri = document.url; string uri = new Uri(document.url).GetComponents(UriComponents.Host, UriFormat.UriEscaped); foreach (IHTMLElement elm in frames) { string src = elm.getAttribute("src"); if (!string.IsNullOrEmpty(src)) { if (!StringUtils.Contains(src, "http")) { src = UriUtils.Combine(uri, src); } string content = client.DownloadString(src); html += content; } } frames = ((IHTMLElementCollection)document.body.all).tags("frame"); foreach (IHTMLElement elm in frames) { string src = elm.getAttribute("src"); if (!string.IsNullOrEmpty(src)) { if (!StringUtils.Contains(src, "http")) { src = UriUtils.Combine(uri, src); } string content = client.DownloadString(src); html += content; } } return html; } public static IHTMLWindow2[] GetFrames(IHTMLDocument2 document) { // TODO: create HTMLFrame and HTMLFrameCollection classes FramesCollection frames = document.frames; IHTMLWindow2[] windows = new IHTMLWindow2[frames.length]; for (int i = 0; i < frames.length; i++) { object refIndex = i; IHTMLWindow2 frame = (IHTMLWindow2)frames.item(ref refIndex); windows[i] = frame; //if (frame.name == "timer") //{ // if (frame.document != null && frame.document.body != null) // { // html = frame.document.body.innerHTML; // } // break; //} } return windows; } public static IHTMLElement GetElement(IHTMLDocument2 document2, string id) { IHTMLDocument3 document = (IHTMLDocument3)document2; return document.getElementById(id); } public static IHTMLInputElement GetInputByName(IHTMLDocument2 document2, string name) { IHTMLElementCollection elements = ((IHTMLElementCollection)document2.body.all).tags("input"); foreach (IHTMLInputElement element in elements) { if (!string.IsNullOrEmpty(element.name) && element.name == name) { return element; } } throw new KeyNotFoundException(); } public static IHTMLTableRow GetParentRow(IHTMLDocument2 document, IHTMLTableCell td) { IHTMLElementCollection elements = GetElementCollection(document, "tr"); foreach (IHTMLElement element in elements) { IHTMLTableRow row = (IHTMLTableRow)element; IHTMLElementCollection cells = row.cells; foreach (IHTMLElement cell in cells) { if (StringUtils.Equals(((IHTMLElement)td).innerHTML, cell.innerHTML)) { return row; } } } return null; } } }