public void SetAutoWrap(bool value) { mshtml.HTMLDocument doc = this.wb.Document.DomDocument as mshtml.HTMLDocument; if (doc != null) { mshtml.HTMLBody body = doc.body as mshtml.HTMLBody; if (body != null) { body.noWrap = !value; } } }
/// <summary> /// 将选择文本粘帖到主浏览器激活控件上 /// </summary> /// <param name="text"></param> private void DefaultRule(string text) { IHTMLElement active = hostBrowser.GetActiveElement() as mshtml.IHTMLElement; if (active is mshtml.HTMLInputElement) { mshtml.HTMLInputElement input = active as mshtml.HTMLInputElement; input.value = text; } else if (active is mshtml.HTMLTextAreaElement) { mshtml.HTMLTextAreaElement area = active as mshtml.HTMLTextAreaElement; area.innerText = text; } else if (active is mshtml.HTMLBody) { string contentEditable = active.getAttribute("contentEditable", 0) as string; if (!string.IsNullOrEmpty(contentEditable) && contentEditable == "true") { mshtml.HTMLBody body = active as mshtml.HTMLBody; body.innerText = text; return; } IHTMLElement frame = BrowserExtensions.GetFrame(active) as mshtml.IHTMLElement; if (frame != null) { mshtml.HTMLBody body = active as mshtml.HTMLBody; body.innerText = text; } } else { IHTMLElementCollection ec = hostBrowser.GetElementsByTagName(true, "TEXTAREA") as IHTMLElementCollection; if (ec.length > 0) { mshtml.HTMLTextAreaElement area = ec.item(null, 0) as mshtml.HTMLTextAreaElement; area.innerText = text; } else { ec = hostBrowser.GetElementsByTagName(true, "INPUT") as IHTMLElementCollection; mshtml.HTMLInputElement input = ec.item(null, 0) as mshtml.HTMLInputElement; input.value = text; } } }
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); } } }
private void watchExam_Tick(object sender, EventArgs e) { //第一次考试的结果,目的要取得整个页面源代码 if (isFirstExam) { try { //使用Microsoft Internet Controls取得所有的已经打开的IE(以Tab计算) SHDocVw.ShellWindows sws = new SHDocVw.ShellWindows(); //sws为当前打开的所有IE窗口每个一个Tab都可以操作,每个Tab对应Com Object的SHDocVw.InternetExplorer foreach (SHDocVw.InternetExplorer iw in sws) { //提交后查看考试结果 if (iw.LocationName.Contains("答题后")) //iw.LocationURL 当前链接路径 查看考试结果 { //取得每个Tab之后,就可以通过InternetExplorer的Document取得每个页面的Dom mshtml.HTMLDocumentClass doc = (mshtml.HTMLDocumentClass)iw.Document; mshtml.HTMLBody body = (mshtml.HTMLBody)doc.body; htmlStr = body.innerHTML.ToString(); isFirstExam = false; //取得Dom之后,基本上就已经取得了操作IE的所有权限了,可以继续使用HTML Object Library对页面进行操作 //或者通过注册JavaScript,对页面进行操作: //mshtml.IHTMLScriptElement script = dom.createElement("script") as mshtml.IHTMLScriptElement; \\创建script标签 //script.text = "$(\"[name='wd']\").val('刘德华');"; \\通过Jquery,对百度进行操作 //mshtml.HTMLBody body = dom.body as mshtml.HTMLBody; \\取得body对象 //body.appendChild((mshtml.IHTMLDOMNode)script); \\注册JavaScript //关闭 //iw.Quit(); } } } catch { } } else //第二次进入考试,在这操作答案 { //使用Microsoft Internet Controls取得所有的已经打开的IE(以Tab计算) SHDocVw.ShellWindows sws = new SHDocVw.ShellWindows(); //sws为当前打开的所有IE窗口每个一个Tab都可以操作,每个Tab对应Com Object的SHDocVw.InternetExplorer foreach (SHDocVw.InternetExplorer iw in sws) { if (iw.LocationName.Contains("答题前")) //iw.LocationURL 当前链接路径 考试 { //全部答案 string daan = ""; //提取答案块 Regex regex = new Regex(@"{""answer"".*}]'\s*/>", RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.CultureInvariant); if (regex.IsMatch(htmlStr)) { MatchCollection matchCollection = regex.Matches(htmlStr); daan = matchCollection[0].ToString().Replace("/>", "").Trim().Replace("]'", ""); } //把答案分组 string strTmp = "},{"; string[] lstDaAn = Regex.Split(daan, strTmp, RegexOptions.IgnoreCase); for (int i = 0; i < lstDaAn.Length; i++) { if (lstDaAn[i].Contains("{")) { lstDaAn[i] = lstDaAn[i].Replace("{", ""); } //第一条会有个{ 最后一条最后有个} if (lstDaAn[i].Contains("}")) { lstDaAn[i] = lstDaAn[i].Replace("}", ""); } //"type":"single" 单选 "realAnswer":"D" if (lstDaAn[i].Contains("\"type\":\"single\"")) { //正确答案选项 string xuanxiang = lstDaAn[i].Substring(lstDaAn[i].IndexOf("\"realAnswer\":\"") + "\"realAnswer\":\"".Length, 1); //题目 string content = lstDaAn[i].Substring(lstDaAn[i].IndexOf("\"content\":\"") + "\"content\":\"".Length, lstDaAn[i].IndexOf("\",\"filePath\"") - lstDaAn[i].IndexOf("\"content\":\"") - "\"content\":\"".Length); //自动答题 //取得每个Tab之后,就可以通过InternetExplorer的Document取得每个页面的Dom HTMLDocument doc = (HTMLDocument)iw.Document; //通过DOM操作IE页面 //mshtml.IHTMLElementCollection inputs = (mshtml.IHTMLElementCollection)doc2.all.tags("INPUT"); //mshtml.HTMLInputElement input1 = (mshtml.HTMLInputElement)inputs.item("kw1", 0); //input1.value = "test"; //mshtml.IHTMLElement element2 = (mshtml.IHTMLElement)inputs.item("su1", 0); //element2.click(); mshtml.IHTMLElementCollection inputs = (mshtml.IHTMLElementCollection)doc.all.tags("li"); IEnumerable <IHTMLElement> EnHEColl = inputs.Cast <IHTMLElement>(); IHTMLElement he1 = EnHEColl.FirstOrDefault(p => p.innerHTML != null && p.innerHTML.Contains(content)); MessageBox.Show(he1.innerText); //mshtml.HTMLInputElement input1 = (mshtml.HTMLInputElement)inputs.item("kw1", 0); } //"type":"multiple" 多选 else if (lstDaAn[i].Contains("\"type\":\"multiple\"")) { //正确答案选项 string xuanxiang = lstDaAn[i].Substring(lstDaAn[i].IndexOf("\"realAnswer\":\"") + "\"realAnswer\":\"".Length, lstDaAn[i].IndexOf("\",\"realScore\"") - lstDaAn[i].IndexOf("\"realAnswer\":\"") - "\"realAnswer\":\"".Length); //题目 string content = lstDaAn[i].Substring(lstDaAn[i].IndexOf("\"content\":\"") + "\"content\":\"".Length, lstDaAn[i].IndexOf("\",\"filePath\"") - lstDaAn[i].IndexOf("\"content\":\"") - "\"content\":\"".Length); //自动答题 } //"type":"judge"判断 else if (lstDaAn[i].Contains("\"type\":\"judge\"")) { //正确答案选项 string xuanxiang = lstDaAn[i].Substring(lstDaAn[i].IndexOf("\"realAnswer\":\"") + "\"realAnswer\":\"".Length, 1); //题目 string content = lstDaAn[i].Substring(lstDaAn[i].IndexOf("\"content\":\"") + "\"content\":\"".Length, lstDaAn[i].IndexOf("\",\"filePath\"") - lstDaAn[i].IndexOf("\"content\":\"") - "\"content\":\"".Length); //自动答题 } } } } } }
/// <summary> /// Method to allow the user to preview the html page /// </summary> public void Preview() { body = (HTMLBody)doc.body; bool isPreview = (body.contentEditable == "true"); for (int i = 0; i < tsTopToolBar.Items.Count; i++) { tsTopToolBar.Items[i].Enabled = !isPreview; } if (isPreview) { body.contentEditable = "false"; cmsHtml.Enabled = false; tsbPreview.Enabled = true; } else { cmsHtml.Enabled = true; body.contentEditable = "true"; } }