Monday, October 21, 2019

Get the Url of a Hyperlink in a TWebBrowser Document

Get the Url of a Hyperlink in a TWebBrowser Document The TWebBrowser Delphi component provides access to the Web browser functionality from your Delphi applications. In most situations you use the TWebBrowser to display HTML documents to the user - thus creating your own version of the (Internet Explorer) Web browser. Note that the TWebBrowser can also display Word documents, for example. A very nice feature of a Browser is to display link information, for example, in the status bar, when the mouse hovers over a link in a document. The TWebBrowser does not expose an event like OnMouseMove. Even if such an event would exist it would be fired for the TWebBrowser component - NOT for the document being displayed inside the TWebBrowser. In order to provide such information (and much more, as you will see in a moment) in your Delphi application using the TWebBrowser component, a technique called events sinking must be implemeted. WebBrowser Event Sink To navigate to a web page using the TWebBrowser component you call the Navigate method. The Document property of the TWebBrowser returns an IHTMLDocument2 value (for web documents). This interface is used to retrieve information about a document, to examine and modify the HTML elements and text within the document, and to process related events. To get the href attribute (link) of an a tag inside a document, while the mouse hovers over a document, you need to react on the onmousemove event of the IHTMLDocument2. Here are the steps to sink events for the currently loaded document: Sink the WebBrowser controls events in the DocumentComplete event raised by the TWebBrowser. This event is fired when the document is fully loaded into the Web Browser.Inside DocumentComplete, retrieve the WebBrowsers document object and sink the HtmlDocumentEvents interface.Handle the event you are interested in.Clear the sink in the in BeforeNavigate2 - that is when the new document is loaded in the Web Browser. HTML Document OnMouseMove Since we are interested in the HREF attribute of an A element - in order to show the URL of a link the mouse is over, we will sink the onmousemove event. The procedure to get the tag (and its attributes) below the mouse can be defined as: var   Ã‚  htmlDoc : IHTMLDocument2; ... procedure TForm1.Document_OnMouseOver; var   Ã‚   element : IHTMLElement; begin   Ã‚   if htmlDoc nil then Exit;   Ã‚   element : htmlDoc.parentWindow.event.srcElement;   Ã‚   elementInfo.Clear;   Ã‚   if LowerCase(element.tagName) a then   Ã‚   begin   Ã‚  Ã‚  Ã‚   ShowMessage(Link, HREF : element.getAttribute(href,0)]) ;   Ã‚   end   Ã‚   else if LowerCase(element.tagName) img then   Ã‚   begin   Ã‚  Ã‚  Ã‚   ShowMessage(IMAGE, SRC : element.getAttribute(src,0)]) ;   Ã‚   end   Ã‚   else   Ã‚   begin   Ã‚  Ã‚  Ã‚   elementInfo.Lines.Add(Format(TAG : %s,[element.tagName])) ;   Ã‚   end; end; (*Document_OnMouseOver*) As explained above, we attach to the onmousemove event of a document in the OnDocumentComplete event of a TWebBrowser: procedure TForm1.WebBrowser1DocumentComplete(   Ã‚  ASender: TObject;   Ã‚  const pDisp: IDispatch;   Ã‚  var URL: OleVariant) ; begin   Ã‚   if Assigned(WebBrowser1.Document) then   Ã‚   begin   Ã‚  Ã‚  Ã‚   htmlDoc : WebBrowser1.Document as IHTMLDocument2;   Ã‚  Ã‚  Ã‚   htmlDoc.onmouseover : (TEventObject.Create(Document_OnMouseOver) as IDispatch) ;   Ã‚   end; end; (*WebBrowser1DocumentComplete*) And this is where the problems arise! As you might guess the onmousemove event is *not* a usual event - as are those we are used to work with in Delphi. The onmousemove expects a pointer to a variable of type VARIANT of type VT_DISPATCH that receives the IDispatch interface of an object with a default method that is invoked when the event occurs. In order to attach a Delphi procedure to onmousemove you need to create a wrapper that implements IDispatch and raises your event in its Invoke method. Heres the TEventObject interface: TEventObject class(TInterfacedObject, IDispatch) private   Ã‚   FOnEvent: TObjectProcedure; protected   Ã‚   function GetTypeInfoCount(out Count: Integer): HResult; stdcall;   Ã‚   function GetTypeInfo(Index, LocaleID: Integer; out TypeInfo): HResult; stdcall;   Ã‚   function GetIDsOfNames(const IID: TGUID; Names: Pointer; NameCount, LocaleID: Integer; DispIDs: Pointer): HResult; stdcall;   Ã‚   function Invoke(DispID: Integer; const IID: TGUID; LocaleID: Integer; Flags: Word; var Params; VarResult, ExcepInfo, ArgErr: Pointer): HResult; stdcall; public   Ã‚   constructor Create(const OnEvent: TObjectProcedure) ;   Ã‚   property OnEvent: TObjectProcedure read FOnEvent write FOnEvent;   Ã‚   end; Heres how to implement event sinking for a document displayed by the TWebBrowser component - and get the info of a HTML element below the mouse. TWebBrowser Document Event Sinking Example Download Drop a TWebBrowser (WebBrowser1) on a Form (Form1). Add a TMemo (elementInfo)... unit Unit1;interfaceuses  Ã‚   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,  Ã‚   Dialogs, OleCtrls, SHDocVw, MSHTML, ActiveX, StdCtrls;type  Ã‚   TObjectProcedure procedure of object;  Ã‚   TEventObject class(TInterfacedObject, IDispatch)  Ã‚   private  Ã‚  Ã‚  Ã‚   FOnEvent: TObjectProcedure;  Ã‚   protected  Ã‚  Ã‚  Ã‚   function GetTypeInfoCount(out Count: Integer): HResult; stdcall;  Ã‚  Ã‚  Ã‚   function GetTypeInfo(Index, LocaleID: Integer; out TypeInfo): HResult; stdcall;  Ã‚  Ã‚  Ã‚   function GetIDsOfNames(const IID: TGUID; Names: Pointer; NameCount, LocaleID: Integer; DispIDs: Pointer): HResult; stdcall;  Ã‚  Ã‚  Ã‚   function Invoke(DispID: Integer; const IID: TGUID; LocaleID: Integer; Flags: Word; var Params; VarResult, ExcepInfo, ArgErr: Pointer): HResult; stdcall;  Ã‚   public  Ã‚  Ã‚  Ã‚   constructor Create(const OnEvent: TObjectProcedure) ;  Ã‚  Ã‚  Ã‚   property OnEvent: TObjectProcedure read FOnEvent writ e FOnEvent;  Ã‚   end;  Ã‚   TForm1 class(TForm)  Ã‚  Ã‚  Ã‚   WebBrowser1: TWebBrowser;  Ã‚  Ã‚  Ã‚   elementInfo: TMemo;  Ã‚  Ã‚  Ã‚   procedure WebBrowser1BeforeNavigate2(ASender: TObject; const pDisp: IDispatch; var URL, Flags, TargetFrameName, PostData, Headers: OleVariant; var Cancel: WordBool) ;  Ã‚  Ã‚  Ã‚   procedure WebBrowser1DocumentComplete(ASender: TObject; const pDisp: IDispatch; var URL: OleVariant) ;  Ã‚  Ã‚  Ã‚   procedure FormCreate(Sender: TObject) ;  Ã‚   private  Ã‚  Ã‚  Ã‚   procedure Document_OnMouseOver;  Ã‚   public  Ã‚  Ã‚  Ã‚   { Public declarations }  Ã‚   end;var  Ã‚   Form1: TForm1;  Ã‚   htmlDoc : IHTMLDocument2;implementation{$R *.dfm}procedure TForm1.Document_OnMouseOver;var  Ã‚   element : IHTMLElement;begin  Ã‚   if htmlDoc nil then Exit;  Ã‚   element : htmlDoc.parentWindow.event.srcElement;  Ã‚   elementInfo.Clear;  Ã‚   if LowerCase(element.tagName) a then  Ã‚   begin  Ã‚  Ã‚  Ã‚   elementInfo.Lines. Add(LINK info...) ;  Ã‚  Ã‚  Ã‚   elementInfo.Lines.Add(Format(HREF : %s,[element.getAttribute(href,0)])) ;  Ã‚   end  Ã‚   else if LowerCase(element.tagName) img then  Ã‚   begin  Ã‚  Ã‚  Ã‚   elementInfo.Lines.Add(IMAGE info...) ;  Ã‚  Ã‚  Ã‚   elementInfo.Lines.Add(Format(SRC : %s,[element.getAttribute(src,0)])) ;  Ã‚   end  Ã‚   else  Ã‚   begin  Ã‚  Ã‚  Ã‚   elementInfo.Lines.Add(Format(TAG : %s,[element.tagName])) ;  Ã‚   end;end; (*Document_OnMouseOver*)procedure TForm1.FormCreate(Sender: TObject) ;begin  Ã‚   WebBrowser1.Navigate(http://delphi.about.com) ;  Ã‚   elementInfo.Clear;  Ã‚   elementInfo.Lines.Add(Move your mouse over the document...) ;end; (*FormCreate*)procedure TForm1.WebBrowser1BeforeNavigate2(ASender: TObject; const pDisp: IDispatch; var URL, Flags, TargetFrameName, PostData, Headers: OleVariant; var Cancel: WordBool) ;begin  Ã‚   htmlDoc : nil;end; (*WebBrowser1BeforeNavigate2*)procedure TForm1.WebBrowser1DocumentComplete(ASend er: TObject; const pDisp: IDispatch; var URL: OleVariant) ;begin  Ã‚   if Assigned(WebBrowser1.Document) then  Ã‚   begin  Ã‚  Ã‚  Ã‚   htmlDoc : WebBrowser1.Document as IHTMLDocument2;  Ã‚  Ã‚  Ã‚   htmlDoc.onmouseover : (TEventObject.Create(Document_OnMouseOver) as IDispatch) ;  Ã‚   end;end; (*WebBrowser1DocumentComplete*){ TEventObject }constructor TEventObject.Create(const OnEvent: TObjectProcedure) ;begin  Ã‚   inherited Create;  Ã‚   FOnEvent : OnEvent;end;function TEventObject.GetIDsOfNames(const IID: TGUID; Names: Pointer; NameCount, LocaleID: Integer; DispIDs: Pointer): HResult;begin  Ã‚   Result : E_NOTIMPL;end;function TEventObject.GetTypeInfo(Index, LocaleID: Integer; out TypeInfo): HResult;begin  Ã‚   Result : E_NOTIMPL;end;function TEventObject.GetTypeInfoCount(out Count: Integer): HResult;begin  Ã‚   Result : E_NOTIMPL;end;function TEventObject.Invoke(DispID: Integer; const IID: TGUID; LocaleID: Integer; Flags: Word; var Params; VarResult, ExcepIn fo, ArgErr: Pointer): HResult;begin  Ã‚   if (DispID DISPID_VALUE) then  Ã‚   begin  Ã‚  Ã‚  Ã‚   if Assigned(FOnEvent) then FOnEvent;  Ã‚  Ã‚  Ã‚   Result : S_OK;  Ã‚   end  Ã‚   else Result : E_NOTIMPL;end;end.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.