public DocHTML(HTMLDocument doc) { this.mDoc = doc; this.mDoc2 = (IHTMLDocument2)this.mDoc; this.mDoc3 = (IHTMLDocument3)this.mDoc; this.mDoc4 = (IHTMLDocument4)this.mDoc; this.mDoc5 = (IHTMLDocument5)this.mDoc; }
public DocHTML(HTMLDocument doc) { mDoc = doc; mDoc2 = (IHTMLDocument2)mDoc; mDoc3 = (IHTMLDocument3)mDoc; mDoc4 = (IHTMLDocument4)mDoc; mDoc5 = (IHTMLDocument5)mDoc; }
public DocumentAdapter( object document ) { //_documentUri = documentUri; _raw = document; _document = document as IHTMLDocument; _document2 = document as IHTMLDocument2; _document3 = document as IHTMLDocument3; _document4 = document as IHTMLDocument4; _document5 = document as IHTMLDocument5; }
public DocumentAdapter(object document) { //_documentUri = documentUri; _raw = document; _document = document as IHTMLDocument; _document2 = document as IHTMLDocument2; _document3 = document as IHTMLDocument3; _document4 = document as IHTMLDocument4; _document5 = document as IHTMLDocument5; }
static void DispatchChangeEvent(WebControl awc, int iIndex, string sControl, IHTMLSelectElement ihie, IHTMLOptionElement ihoe, IHTMLDocument2 oDoc2) { ihoe.selected = true; object dummy = null; IHTMLDocument4 oDoc4 = (IHTMLDocument4)oDoc2; object eventObj = oDoc4.CreateEventObject(ref dummy); IHTMLEventObj2 obj2 = (IHTMLEventObj2)eventObj; IHTMLSelectElement hsec = ihie as IHTMLSelectElement; awc.ReportNavState("Before FireEvent"); hsec.FireEvent("onchange", ref eventObj); awc.ReportNavState("After FireEvent"); }
public void Click() { object refObj = null; IHTMLDocument4 document = node.document as IHTMLDocument4; IHTMLElement3 element = (IHTMLElement3)node; IHTMLEventObj eventObject = document.CreateEventObject(ref refObj); object eventRef = eventObject; element.FireEvent("onMouseDown", ref eventRef); element.FireEvent("onMouseUp", ref eventRef); node.click(); parent.WaitForLoadToComplete(); }
public SpellingHighlighter(ISpellingChecker spellingChecker, IHighlightRenderingServicesRaw highlightRenderingServices, IDisplayServicesRaw displayServices, IMarkupServicesRaw markupServices, IHTMLDocument4 htmlDocument) { _spellingChecker = spellingChecker; _highlightRenderingServices = highlightRenderingServices; _displayServices = displayServices; _markupServicesRaw = markupServices; _markupServices = new MshtmlMarkupServices(_markupServicesRaw); _htmlDocument = htmlDocument; _tracker = new HighlightSegmentTracker(); //the timer to handle interleaving of spell checking _timer = new SpellingTimer(TIMER_INTERVAL); _timer.Start(); _timer.Tick += new EventHandler(_timer_Tick); _workerQueue = new Queue(); }
public SpellingHighlighter(ISpellingChecker spellingChecker, IHighlightRenderingServicesRaw highlightRenderingServices, IDisplayServicesRaw displayServices, IMarkupServicesRaw markupServices, IHTMLDocument4 htmlDocument) { _spellingChecker = spellingChecker; _highlightRenderingServices = highlightRenderingServices; _displayServices = displayServices; _markupServicesRaw = markupServices; _markupServices = new MshtmlMarkupServices(_markupServicesRaw); _htmlDocument = htmlDocument; _tracker = new HighlightSegmentTracker(); //the timer to handle interleaving of spell ghecking _timer = new SpellingTimer(TIMER_INTERVAL); _timer.Start(); _timer.Tick += new EventHandler(_timer_Tick); _workerQueue = new Queue(); }
static void DispatchChangeEventTry2( WebControl awc, int iIndex, string sControl, IHTMLSelectElement ihie, IHTMLOptionElement ihoe, IHTMLDocument2 oDoc2) { object dummy = null; IHTMLDocument4 oDoc4 = (IHTMLDocument4) oDoc2; object eventObj = oDoc4.CreateEventObject(ref dummy); IHTMLEventObj2 obj2 = (IHTMLEventObj2) eventObj; IHTMLElement3 ihe3 = (IHTMLElement3) ihie; awc.ResetNav(); awc.ReportNavState("Before FireEvent"); // ihe3.FireEvent("onchange", ref eventObj); HtmlElement head = awc.AxWeb.Document.GetElementsByTagName("head")[0]; HtmlElement scriptEl = awc.AxWeb.Document.CreateElement("script"); IHTMLScriptElement element = (IHTMLScriptElement) scriptEl.DomElement; // element.text = $"function changeSelect() {{ $'({sControl}').trigger('change'); }}"; element.text = "function triggerOnChange() " + "{{ " + // "alert('im here'); " + $"var ctl = document.getElementById('{sControl}'); " + // "alert(ctl); "+ "var evt = document.createEvent('HTMLEvents'); " + "evt.initEvent('change', false, true); " + "ctl.dispatchEvent(evt);" + "}} "; // element.text = $"function triggerOnChange() {{ alert('{sControl}');}}"; head.AppendChild(scriptEl); // ArbWeb.AwMainForm.DebugModelessWait(); awc.AxWeb.Document.InvokeScript("triggerOnChange"); // ArbWeb.AwMainForm.DebugModelessWait(); awc.ReportNavState("After FireEvent"); awc.WaitForBrowserReady(); awc.WaitDoLog(500); }
private void Type(string value) { object refObj = null; IHTMLDocument4 document = node.document as IHTMLDocument4; IHTMLElement3 element = (IHTMLElement3)node; string val = ""; foreach (char c in value) { IHTMLEventObj eventObject = document.CreateEventObject(ref refObj); eventObject.keyCode = c; object eventRef = eventObject; val += c; element.FireEvent("onKeyDown", ref eventRef); element.FireEvent("onKeyPress", ref eventRef); node.setAttribute("value", val, 0); element.FireEvent("onKeyUp", ref eventRef); } }
static void DispatchEventNew(ArbWebControl awc, int iIndex, string sControl, IHTMLSelectElement ihie, IHTMLOptionElement ihoe, IHTMLDocument2 oDoc2) { ihoe.selected = true; object dummy = null; IHTMLDocument4 oDoc4 = awc.Document4; object eventObj = oDoc4.CreateEventObject(ref dummy); var obj = eventObj; obj.initEvent("onchange", true, true); IHTMLEventObj2 obj2 = (IHTMLEventObj2)eventObj; HTMLSelectElementClass hsec = ihie as HTMLSelectElementClass; awc.ReportNavState("Before FireEvent"); hsec.selectedIndex = iIndex; obj2.fromElement = hsec; obj2.srcElement = hsec; obj2.propertyName = sControl; bool f; oDoc4.FireEvent("change", ref eventObj); awc.ReportNavState("After FireEvent"); }
/// <summary> /// Helper method which will retrieve the IHTMLDocument2 for the supplied window, /// or return the first if none is supplied. /// </summary> /// <param name="browserWindow">The WindowDetails to get the IHTMLDocument2 for</param> /// <param name="document2">Ref to the IHTMLDocument2 to return</param> /// <returns>The WindowDetails to which the IHTMLDocument2 belongs</returns> private static DocumentContainer CreateDocumentContainer(WindowDetails browserWindow) { DocumentContainer returnDocumentContainer = null; WindowDetails returnWindow = null; IHTMLDocument2 returnDocument2 = null; // alternative if no match WindowDetails alternativeReturnWindow = null; IHTMLDocument2 alternativeReturnDocument2 = null; // Find the IE windows foreach (WindowDetails ieWindow in GetIEWindows()) { LOG.DebugFormat("Processing {0} - {1}", ieWindow.ClassName, ieWindow.Text); Accessible ieAccessible = null; WindowDetails directUIWD = IEHelper.GetDirectUI(ieWindow); if (directUIWD != null) { ieAccessible = new Accessible(directUIWD.Handle); } if (ieAccessible == null) { if (browserWindow != null) { LOG.InfoFormat("Active Window is {0}", browserWindow.Text); } if (!ieWindow.Equals(browserWindow)) { LOG.WarnFormat("No ieAccessible for {0}", ieWindow.Text); continue; } LOG.DebugFormat("No ieAccessible, but the active window is an IE window: {0}, ", ieWindow.Text); } try { // Get the Document IHTMLDocument2 document2 = getHTMLDocument(ieWindow); if (document2 == null) { continue; } // Get the content window handle for the shellWindow.Document IOleWindow oleWindow = (IOleWindow)document2; IntPtr contentWindowHandle = IntPtr.Zero; if (oleWindow != null) { oleWindow.GetWindow(out contentWindowHandle); } if (contentWindowHandle != IntPtr.Zero) { // Get the HTMLDocument to check the hasFocus // See: http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/60c6c95d-377c-4bf4-860d-390840fce31c/ IHTMLDocument4 document4 = (IHTMLDocument4)document2; if (document4.hasFocus()) { LOG.DebugFormat("Matched focused document: {0}", document2.title); // Look no further, we got what we wanted! returnDocument2 = document2; returnWindow = new WindowDetails(contentWindowHandle); break; } try { if (ieWindow.Equals(browserWindow)) { returnDocument2 = document2; returnWindow = new WindowDetails(contentWindowHandle); break; } else if (ieAccessible != null && returnWindow == null && document2.title.Equals(ieAccessible.IEActiveTabCaption)) { LOG.DebugFormat("Title: {0}", document2.title); returnDocument2 = document2; returnWindow = new WindowDetails(contentWindowHandle); } else { alternativeReturnDocument2 = document2; alternativeReturnWindow = new WindowDetails(contentWindowHandle); } } catch (Exception) { alternativeReturnDocument2 = document2; alternativeReturnWindow = new WindowDetails(contentWindowHandle); } } } catch (Exception e) { LOG.ErrorFormat("Major problem: Problem retrieving Document from {0}", ieWindow.Text); LOG.Error(e); } } // check if we have something to return if (returnWindow != null) { // As it doesn't have focus, make sure it's active returnWindow.Restore(); returnWindow.GetParent(); // Create the container try { returnDocumentContainer = new DocumentContainer(returnDocument2, returnWindow); } catch (Exception e) { LOG.Error("Major problem: Problem retrieving Document."); LOG.Error(e); } } if (returnDocumentContainer == null && alternativeReturnDocument2 != null) { // As it doesn't have focus, make sure it's active alternativeReturnWindow.Restore(); alternativeReturnWindow.GetParent(); // Create the container try { returnDocumentContainer = new DocumentContainer(alternativeReturnDocument2, alternativeReturnWindow); } catch (Exception e) { LOG.Error("Major problem: Problem retrieving Document."); LOG.Error(e); } } return(returnDocumentContainer); }
private bool SetupDocumentChangeNotification(IHTMLDocument4 pDoc4) {//SetWinEventHook(EVENT_OBJECT_CREATE). könnte für DOM Changes auch hergenommen werden if (pDoc4 == null) return false; IMarkupContainer2 m_pMarkupContainer2 = pDoc4 as IMarkupContainer2; if (m_pMarkupContainer2 == null) return false; //To stop receiving change notifications, call Release on the IHTMLChangeLog pointer. //m_pChangeLog = null; int iret2 = m_pMarkupContainer2.CreateChangeLog(this, out m_pChangeLog, true, true); int iret = m_pMarkupContainer2.RegisterForDirtyRange(this, out m_pDirtyRangeCookie); return (iret == Hresults.S_OK) ? true : false; }
private bool SetupDocumentChangeNotification(IHTMLDocument4 pDoc4) { if (pDoc4 == null) return false; IMarkupContainer2 m_pMarkupContainer2 = pDoc4 as IMarkupContainer2; if (m_pMarkupContainer2 == null) return false; //To stop receiving change notifications, call Release on the IHTMLChangeLog pointer. //m_pChangeLog = null; //int iret = m_pMarkupContainer2.CreateChangeLog(this, out m_pChangeLog, true, true); int iret = m_pMarkupContainer2.RegisterForDirtyRange(this, out m_pDirtyRangeCookie); return (iret == Hresults.S_OK) ? true : false; }
/// <summary> /// Helper method which will retrieve the IHTMLDocument2 for the supplied window, /// or return the first if none is supplied. /// </summary> /// <param name="browserWindowDetails">The WindowDetails to get the IHTMLDocument2 for</param> /// <param name="document2">Ref to the IHTMLDocument2 to return</param> /// <returns>The WindowDetails to which the IHTMLDocument2 belongs</returns> private static DocumentContainer GetDocument(WindowDetails activeWindow) { DocumentContainer returnDocumentContainer = null; WindowDetails returnWindow = null; IHTMLDocument2 returnDocument2 = null; // alternative if no match WindowDetails alternativeReturnWindow = null; IHTMLDocument2 alternativeReturnDocument2 = null; // Find the IE window foreach (WindowDetails ieWindow in WindowDetails.GetAllWindows("IEFrame")) { LOG.DebugFormat("Processing {0} - {1}", ieWindow.ClassName, ieWindow.Text); Accessible ieAccessible = null; WindowDetails directUIWD = GetDirectUI(ieWindow); if (directUIWD != null) { ieAccessible = new Accessible(directUIWD.Handle); } if (ieAccessible == null) { LOG.InfoFormat("Active Window is {0}", activeWindow.Text); if (!ieWindow.Equals(activeWindow)) { LOG.WarnFormat("No ieAccessible for {0}", ieWindow.Text); continue; } LOG.DebugFormat("No ieAccessible, but the active window is an IE window: {0}, ", ieWindow.Text); } try { // Get the Document IHTMLDocument2 document2 = null; uint windowMessage = User32.RegisterWindowMessage("WM_HTML_GETOBJECT"); if (windowMessage == 0) { LOG.WarnFormat("Couldn't register WM_HTML_GETOBJECT"); continue; } WindowDetails ieServer = ieWindow.GetChild("Internet Explorer_Server"); if (ieServer == null) { LOG.WarnFormat("No Internet Explorer_Server for {0}", ieWindow.Text); continue; } LOG.DebugFormat("Trying WM_HTML_GETOBJECT on {0}", ieServer.ClassName); UIntPtr response; User32.SendMessageTimeout(ieServer.Handle, windowMessage, IntPtr.Zero, IntPtr.Zero, SendMessageTimeoutFlags.SMTO_NORMAL, 1000, out response); if (response != UIntPtr.Zero) { document2 = (IHTMLDocument2)Accessible.ObjectFromLresult(response, typeof(IHTMLDocument).GUID, IntPtr.Zero); if (document2 == null) { LOG.Error("No IHTMLDocument2 found"); continue; } } else { LOG.Error("No answer on WM_HTML_GETOBJECT."); continue; } // Get the content window handle for the shellWindow.Document IOleWindow oleWindow = (IOleWindow)document2; IntPtr contentWindowHandle = IntPtr.Zero; if (oleWindow != null) { oleWindow.GetWindow(out contentWindowHandle); } if (contentWindowHandle != IntPtr.Zero) { // Get the HTMLDocument to check the hasFocus // See: http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/60c6c95d-377c-4bf4-860d-390840fce31c/ IHTMLDocument4 document4 = (IHTMLDocument4)document2; if (document4.hasFocus()) { LOG.DebugFormat("Matched focused document: {0}", document2.title); // Look no further, we got what we wanted! returnDocument2 = document2; returnWindow = new WindowDetails(contentWindowHandle); break; } try { if (ieWindow.Equals(activeWindow)) { returnDocument2 = document2; returnWindow = new WindowDetails(contentWindowHandle); break; } else if (ieAccessible != null && returnWindow == null && document2.title.Equals(ieAccessible.IEActiveTabCaption)) { LOG.DebugFormat("Title: {0}", document2.title); returnDocument2 = document2; returnWindow = new WindowDetails(contentWindowHandle); } else { alternativeReturnDocument2 = document2; alternativeReturnWindow = new WindowDetails(contentWindowHandle); } } catch (Exception) { alternativeReturnDocument2 = document2; alternativeReturnWindow = new WindowDetails(contentWindowHandle); } } } catch (Exception e) { LOG.Error(e); LOG.DebugFormat("Major problem: Problem retrieving Document from {0}", ieWindow.Text); } } // check if we have something to return if (returnWindow != null) { // As it doesn't have focus, make sure it's active returnWindow.Restore(); returnWindow.GetParent(); // Create the container returnDocumentContainer = new DocumentContainer(returnDocument2, returnWindow); } if (returnDocumentContainer == null && alternativeReturnDocument2 != null) { // As it doesn't have focus, make sure it's active alternativeReturnWindow.Restore(); alternativeReturnWindow.GetParent(); // Create the container returnDocumentContainer = new DocumentContainer(alternativeReturnDocument2, alternativeReturnWindow); } return(returnDocumentContainer); }
public HtmlRenderer(IArachnodeDAO arachnodeDAO) { Type htmldoctype = Type.GetTypeFromCLSID(Iid_Clsids.CLSID_HTMLDocument, true); //Using Activator inplace of CoCreateInstance, returns IUnknown //which we cast to a IHtmlDocument2 interface MSHTML = (IHTMLDocument4)Activator.CreateInstance(htmldoctype); IPersistStreamInit ips = (IPersistStreamInit)MSHTML; ips.InitNew(); IOleObject oleObject = (IOleObject)MSHTML; //Set client site int iret = oleObject.SetClientSite(this); CrawlRequestTimeoutInMinutes = 1; //Getting exceptions when trying to get change notification through IPropertyNotifySink and connectionpointcontainer. //So, using this technique. Thread t = new Thread(() => { string previousReadyState = ""; while (true) { try { if (_parse.WaitOne()) { if (DateTime.Now.Subtract(_parseStartTime).TotalMinutes < CrawlRequestTimeoutInMinutes) { string currentReadyState = ((IHTMLDocument2)MSHTML).readyState; if (string.Compare(currentReadyState, previousReadyState, true) != 0) { previousReadyState = currentReadyState; ReadyState = ReadyState.Uninitialized; switch (currentReadyState.ToLower()) { case "loading": ReadyState = ReadyState.Loading; break; case "loaded": ReadyState = ReadyState.Loaded; break; case "interactive": ReadyState = ReadyState.Interactive; ModifyDOM(((IHTMLDocument2)MSHTML), true); break; case "complete": ReadyState = ReadyState.Complete; break; default: ReadyState = ReadyState.Uninitialized; break; } if (ReadyStateChange != null) { ReadyStateChange(this, new ReadyStateChangeEventArgs(ReadyState, ((IHTMLDocument2)MSHTML))); } } if (ReadyState == ReadyState.Complete) { EndRendering(); previousReadyState = ""; } } else { throw new Exception("The AbsoluteUri timed out while rendering."); } Thread.Sleep(100); } } catch (Exception exception) { EndRendering(); previousReadyState = ""; arachnodeDAO.InsertException(AbsoluteUri, AbsoluteUri, exception, false); } } }); t.SetApartmentState(ApartmentState.STA); t.Start(); }
/// <summary> /// Fires event on the document object /// </summary> /// <param name="doc"></param> /// <param name="eventName"></param> /// <param name="e"></param> /// <returns></returns> private bool FireEvent(IHTMLDocument4 doc, string eventName, EventArgs e) { int x = Cursor.Position.X; int y = Cursor.Position.Y; if (e is MouseEventArgs) { x = (e as MouseEventArgs).X; y = (e as MouseEventArgs).Y; } //object dummy = null; //object oEvt = doc.CreateEventObject(ref dummy); object oEvt = doc.CreateEventObject(); var evt = (IHTMLEventObj2)oEvt; //cast //set various properties of the event here. evt.clientX = x; evt.clientY = y; //fire return doc.FireEvent(eventName, oEvt); }
/// <summary> /// Public constructor. /// </summary> /// <param name="html">Html string to be processed.</param> public HtmlProcessor(string html) { if (string.IsNullOrEmpty(html)) { throw new ArgumentException("HTML string can not be null or empty.", "html"); } this.documentSource = html; this.doc2 = new HTMLDocumentClass(); this.doc2.write(new object[] { html }); //while (this.doc2.readyState != "complete" && this.doc2.readyState != "interactive") //{ // System.Windows.Forms.Application.DoEvents(); //} this.doc3 = (IHTMLDocument3)this.doc2; this.doc4 = (IHTMLDocument4)this.doc2; this.doc5 = (IHTMLDocument5)this.doc2; this.InnerText = (this.doc2.body == null) ? null : this.doc2.body.innerText; this.Title = this.doc2.title; this._elements = new List <DomElement>(); IHTMLElementCollection collection = this.doc2.all; foreach (var item in collection) { var element = (IHTMLElement)item; if (element != null) { DomElement domElement = new DomElement { Class = element.className, InnerHtml = element.innerHTML.TrimSafe(), OuterHtml = element.outerHTML.TrimSafe(), InnerText = element.innerText.TrimSafe(), Id = element.id, TagName = element.tagName.ToLowerInvariant() }; domElement.Attributes = new NameValueCollection(); if (element.outerHTML.IsNullOrEmpty()) { continue; } //@"<INPUT onblur=google&&google.fade&&google.fade() class=lst title='Google Search' value=TESTING maxLength=2048 size=55 name=q autocomplete='off' init='true'/>"; string node = element.outerHTML.Substring(0, element.outerHTML.IndexOf(">") + 1); string pattern = @" (?:<)(?<Tag>[^\s/>]+) # Extract the tag name. (?![/>]) # Stop if /> is found # -- Extract Attributes Key Value Pairs -- ((?:\s+) # One to many spaces start the attribute (?<Key>[^=]+) # Name/key of the attribute (?:=) # Equals sign needs to be matched, but not captured. (?([\x22\x27]) # If quotes are found (?:[\x22\x27]) (?<Value>[^\x22\x27]+) # Place the value into named Capture (?:[\x22\x27]) | # Else no quotes (?<Value>[^\s/>]*) # Place the value into named Capture ) )+ # -- One to many attributes found!"; //http://omegacoder.com/?p=512 var attr = (from Match mt in Regex.Matches(node, pattern, RegexOptions.IgnorePatternWhitespace) select new { Name = mt.Groups["Tag"], Attrs = (from cpKey in mt.Groups["Key"].Captures.Cast <Capture>().Select((a, i) => new { a.Value, i }) join cpValue in mt.Groups["Value"].Captures.Cast <Capture>().Select((b, i) => new { b.Value, i }) on cpKey.i equals cpValue.i select new KeyValuePair <string, string>(cpKey.Value, cpValue.Value)).ToDictionary(kvp => kvp.Key, kvp => kvp.Value) }).FirstOrDefault(); if (attr != null) { var attributes = attr.Attrs; foreach (KeyValuePair <string, string> kvp in attributes) { if (!string.IsNullOrEmpty(kvp.Value)) { domElement.Attributes.Add(kvp.Key.ToLowerInvariant(), kvp.Value); } } } this._elements.Add(domElement); } } }