/// <summary> /// /// </summary> /// <param name="element"></param> /// <param name="Finder">优先finder设置</param> public void SetFindMethod(IHTMLElement element, FindAttribute Finder) { int count = 0; if (Finder != null) { FindMechanism.Add(Finder); count = GetElementCount(element.tagName); if (count > 1) { FindMechanism.Clear(); } else { SetFrameList(element); ElementType = TagStringToElementType(element); return; } } Finder = GetFinder(element, FindMethods.CssSelector); FindMechanism.Add(Finder); mshtml.IHTMLDOMNode node = element as mshtml.IHTMLDOMNode; count = GetElementCount(node.nodeName); //不支持CssSelector,试用xpath定位 if (count == -1) { FindMechanism.Clear(); Finder = GetFinder(element, FindMethods.XPath); FindMechanism.Add(Finder); } SetFrameList(element); ElementType = TagStringToElementType(element); }
private string getNode(mshtml.IHTMLElement node, bool fullPath) { string nodeExpr = node.tagName; if (nodeExpr == null) // Eg. node = #text { return(null); } if (node.id != "" && node.id != null && !fullPath) { nodeExpr += "#" + node.id; // We don't really need to go back up to //HTML, since IDs are supposed // to be unique, so they are a good starting point. return(nodeExpr); } // Find rank of node among its type in the parent int rank = 0; mshtml.IHTMLDOMNode nodeDom = node as mshtml.IHTMLDOMNode; mshtml.IHTMLDOMNode psDom = nodeDom.previousSibling; psDom = SkipText(psDom); mshtml.IHTMLElement ps = psDom as mshtml.IHTMLElement; while (ps != null) { if (ps.tagName == node.tagName) { rank++; } psDom = psDom.previousSibling; psDom = SkipText(psDom); ps = psDom as mshtml.IHTMLElement; } if (rank > 0) { nodeExpr += ":eq(" + rank + ")"; } else { // First node of its kind at this level. Are there any others? mshtml.IHTMLDOMNode nsDom = nodeDom.nextSibling; while (nsDom != null && nsDom.nodeName == "#text") { nsDom = nsDom.nextSibling; } //nsDom = SkipText(nsDom); mshtml.IHTMLElement ns = nsDom as mshtml.IHTMLElement; while (ns != null) { if (ns.tagName == node.tagName) { // Yes, mark it as being the first one nodeExpr += ":eq(0)"; break; } nsDom = nsDom.nextSibling; ns = nsDom as mshtml.IHTMLElement; } } return(nodeExpr); }
public static bool IsDTDDocument(object document) { // XHtml declare flag string string DocTypeContent = @"-//W3C//DTD"; mshtml.IHTMLDocument3 document3 = (mshtml.IHTMLDocument3)document; mshtml.IHTMLDOMChildrenCollection domChilds = (mshtml.IHTMLDOMChildrenCollection)document3.childNodes; mshtml.IHTMLDOMNode domNode = (mshtml.IHTMLDOMNode)domChilds.item(0); return(domNode.nodeValue.ToString().Contains(DocTypeContent)); }
private static string getNode(mshtml.IHTMLElement node) { string nodeExpr = node.tagName; if (nodeExpr == null) // Eg. node = #text { return(null); } if (node.id != "" && node.id != null) { nodeExpr += "[@id='" + node.id + "']"; // We don't really need to go back up to //HTML, since IDs are supposed // to be unique, so they are a good starting point. return("/" + nodeExpr); } // Find rank of node among its type in the parent int rank = 1; mshtml.IHTMLDOMNode nodeDom = node as mshtml.IHTMLDOMNode; mshtml.IHTMLDOMNode psDom = nodeDom.previousSibling; mshtml.IHTMLElement ps = psDom as mshtml.IHTMLElement; while (ps != null) { if (ps.tagName == node.tagName) { rank++; } psDom = psDom.previousSibling; ps = psDom as mshtml.IHTMLElement; } if (rank > 1) { nodeExpr += "[" + rank + "]"; } else { // First node of its kind at this level. Are there any others? mshtml.IHTMLDOMNode nsDom = nodeDom.nextSibling; mshtml.IHTMLElement ns = nsDom as mshtml.IHTMLElement; while (ns != null) { if (ns.tagName == node.tagName) { // Yes, mark it as being the first one nodeExpr += "[1]"; break; } nsDom = nsDom.nextSibling; ns = nsDom as mshtml.IHTMLElement; } } return(nodeExpr); }
private static bool GetEleParentFrames(mshtml.IHTMLDOMNode root, mshtml.IHTMLDOMNode node, List <mshtml.IHTMLDOMNode> frames) { bool flag = false; if (root == node) { return(true); } bool flag2 = false; switch (root.nodeName.ToLower()) { case "frame": case "iframe": flag2 = true; break; } IHTMLDOMChildrenCollection childNodes = null; if (flag2) { SHDocVw.IWebBrowser2 browser = root as SHDocVw.IWebBrowser2; if (browser != null) { mshtml.IHTMLDocument2 document = browser.Document as mshtml.IHTMLDocument2; if (document != null) { mshtml.IHTMLDOMNode parentElement = document.body.parentElement as mshtml.IHTMLDOMNode; childNodes = parentElement.childNodes as IHTMLDOMChildrenCollection; } } } if (childNodes == null) { childNodes = root.childNodes as IHTMLDOMChildrenCollection; } if (childNodes == null) { return(false); } for (int i = 0; i < childNodes.length; i++) { mshtml.IHTMLDOMNode node3 = childNodes.item(i) as mshtml.IHTMLDOMNode; if (GetEleParentFrames(node3, node, frames)) { if (flag2) { frames.Add(root); } flag = true; } } return(flag); }
public static void 移除指定标记元素(this HtmlDocument doc, params string[] tagNameArray) { foreach (var t in tagNameArray) { var hc = doc.GetElementsByTagName(t).Cast <HtmlElement>(); foreach (var f in hc) { mshtml.IHTMLDOMNode node = f.DomElement as mshtml.IHTMLDOMNode; if (node != null) { node.parentNode.removeChild(node); } } } }
public void InjectJavascript(HTMLDocument document) { string script = "window.guideMe = {userKey:'" + USERKEY + "', trackingId:''};"; string url = JSPATH; if (document != null) { mshtml.IHTMLElementCollection headElementCollection = document.getElementsByTagName("head"); if (headElementCollection != null) { mshtml.IHTMLElement injectedScript = document.getElementById("__guideme_script"); if (injectedScript == null) { mshtml.IHTMLElement headElement = headElementCollection.item(0, 0) as mshtml.IHTMLElement; mshtml.IHTMLElement guideme = (mshtml.IHTMLElement)document.createElement("script"); mshtml.IHTMLScriptElement guidemeScript = (mshtml.IHTMLScriptElement)guideme; guidemeScript.text = script; mshtml.IHTMLDOMNode guidemeNode = (mshtml.IHTMLDOMNode)guideme; guideme.id = "__guideme_script_config"; mshtml.IHTMLElement element = (mshtml.IHTMLElement)document.createElement("script"); mshtml.IHTMLScriptElement scriptElement = (mshtml.IHTMLScriptElement)element; scriptElement.src = url; element.id = "__guideme_script"; mshtml.IHTMLDOMNode node = (mshtml.IHTMLDOMNode)element; mshtml.HTMLBody body = document.body as mshtml.HTMLBody; body.appendChild(guidemeNode); body.appendChild(node); } Marshal.ReleaseComObject(document); } } }
/// <summary> /// 表格弹出菜单事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void tableToolStripMenuItem_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e) { switch (e.ClickedItem.Name) { case "delcolToolStripMenuItem": { GeneralMethodsForDesign.deleteColumn(currentEle); } break; case "inscolToolStripMenuItem": { GeneralMethodsForDesign.insertColumn(currentEle); } break; case "delrowToolStripMenuItem": { GeneralMethodsForDesign.deleteRow(currentEle); break; } case "insrowToolStripMenuItem": { GeneralMethodsForDesign.insertRow(currentEle); break; } case "splitToolStripMenuItem": { #region HTMLEditHelper hp = new HTMLEditHelper(); IHTMLTable tb = hp.GetParentTable(currentEle); IHTMLTableRow tr = hp.GetParentRow(currentEle); IHTMLTableCell td = currentEle as IHTMLTableCell; int rowindex = tr.rowIndex; int rowcount = hp.GetRowCount(tb); int colindex = td.cellIndex; hp.Row_InsertCell(tr, colindex - 1); int currentSpan = 0; for (int k = 0; k < colindex; k++) { currentSpan += (hp.Row_GetCell(tr, colindex) as IHTMLTableCell).colSpan; } for (int i = 0; i < rowcount; i++) { if (i != rowindex) { IHTMLTableRow tr0 = hp.GetRow(tb, i) as IHTMLTableRow; IHTMLTableCell td0 = hp.Row_GetCell(tr0, colindex) as IHTMLTableCell; int rowSpan = 0; int cellnum = 0; do { rowSpan += (hp.Row_GetCell(tr0, cellnum) as IHTMLTableCell).colSpan; td0 = hp.Row_GetCell(tr0, cellnum) as IHTMLTableCell; cellnum++; }while (rowSpan <= currentSpan); if (td0.colSpan > 0) { td0.colSpan += 1; } } else { continue; } } break; #endregion } case "mergerToolStripMenuItem": { #region HTMLEditHelper hp = new HTMLEditHelper(); /* IHTMLTableRow tr = hp.GetParentRow(currentEle); * int colNum = hp.Row_GetCellCount(tr); * MergeCellForm mergeCel = new MergeCellForm(colNum); * if (mergeCel.ShowDialog() == DialogResult.OK) * { * * }*/ IHTMLTableCell c1 = currentEle as IHTMLTableCell; mshtml.IHTMLDOMNode n1 = c1 as mshtml.IHTMLDOMNode; IHTMLTableCell c2 = hp.PreviousSibling(n1) as IHTMLTableCell; IHTMLTableCell c3 = hp.NextSibiling(n1) as IHTMLTableCell; IHTMLElement e1 = c1 as IHTMLElement; IHTMLElement e2 = c2 as IHTMLElement; IHTMLElement e3 = c3 as IHTMLElement; int span = c1.colSpan; string tdtext = e1.innerHTML; if (c2 != null) { span += c2.colSpan; tdtext = e2.innerText + tdtext; hp.RemoveNode(e2, false); } if (c3 != null) { span += c3.colSpan; tdtext += e3.innerText; hp.RemoveNode(e3, false); } c1.colSpan = span; e1.innerHTML = tdtext; break; #endregion } case "otherToolStripMenuItem": { break; } } }