?
Solved

Getting link information using TWebBrowser

Posted on 2003-03-05
9
Medium Priority
?
209 Views
Last Modified: 2010-04-04
I am using TwebBrowser to extract information about internet radio schedules. I have OnClick and OnMouseDown event handlers working and I can extract selected text.

I would like to be able to capture the MouseDown event on links in the web page and get the text of the links whilst preventing the link from being activated.

Thank you for your help.
0
Comment
Question by:MorrisLockwood
9 Comments
 
LVL 6

Accepted Solution

by:
DaFox earned 2000 total points
ID: 8076149
Hi Morris.

Just wrote this piece of code. Tested on Win2k, D5, IE5.5:

{ uses SHDocVw_TLB, MSHTML_TLB }

...

private
  procedure AppMessage(var Msg: TMsg; var Handled: Boolean);

...

procedure TForm1.FormCreate(Sender: TObject);
begin
  Application.OnMessage := AppMessage;
end;

procedure TForm1.AppMessage(var Msg: TMsg; var Handled: Boolean);
var
  Doc: IHTMLDocument2;
  Element: IHTMLElement;
begin
  if (Msg.Message = WM_LBUTTONDOWN) then
  begin
    Doc := WebBrowser1.document as IHTMLDocument2;
    if (Doc <> nil) then
    begin
      Element := Doc.elementFromPoint(LoWord(msg.LParam), HiWord(Msg.lParam));
      try
        ListBox1.Clear;
        ListBox1.Items.add('ClassName::' + Element.ClassName);
        ListBox1.Items.add('innerHTML::' + Element.innerHTML);
        ListBox1.Items.add('innertext::' + Element.innerText);
        ListBox1.Items.add('OuterHTML::' + Element.outerHTML);
        ListBox1.items.add('OuterText::' + Element.outerText);
      except
        raise;
      end;
      if Pos('<a href', LowerCase(Element.outerHTML)) <> 0 then
      begin
        // don't pass msg to webbrowser !
        handled := true;
        // ...  -> Text = Element.innerHTML
      end;
    end;
  end;
end;

hope that helps!

wbr,
Markus
0
 
LVL 9

Expert Comment

by:ginsonic
ID: 8077802
procedure TForm1.WebBrowser1BeforeNavigate2(Sender: TObject;
  const pDisp: IDispatch; var URL, Flags, TargetFrameName, PostData,
  Headers: OleVariant; var Cancel: WordBool);
begin
  ShowMessage(URL);
  Cancel := True;
end;
0
 

Author Comment

by:MorrisLockwood
ID: 8078619
Thank you both.
Unfortunately, ginsonic, your approach only gives the URL whereas I would like the text of the link as seen by the user.

I have not had time to test your approach fully, Markus ; it appears to work most of the time but fails on occassion. It also hung my machine on one occassion. I will be testing it further later today.
Regards  Morris
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:DaFox
ID: 8079783
Morris, please keep me informed about your results. I didn't get any exceptions yesterday!

Markus
0
 

Expert Comment

by:JimMcKeeth
ID: 8085122
Use the method mentioned by Ginsonic (if it works) to get the URL.  Then use an Indy TIdHTTP client socket to retreive the information.
0
 

Author Comment

by:MorrisLockwood
ID: 8093560
Markus,

I am sorry for the delay in getting back to you but I wanted to get a clear understanding of the behaviour of the code.

Fundamentally, the code does what I need, thank you. The instability I mentioned was due to accessing a pdf file and the subsequent events.

However, the recognition that it is a link and setting “Handled” to true does not work for all links. It may be my unfamiliarity with HTML coding but when I access our site www.sentinelsoftware.co.uk and press on some links I get the results shown below. Only one of these is recognised by the code as a link. However I can see a way to side step the problem.

Another oddity is that, with the copy of MSHTML_TLB.PAS that I have (dated 12/11/00) the boolean symbol “true” is overwritten to be a byte and I need to use (0=0) to set the Handled variable to true.  What is the date of your MSHTML_TLB file?

Once again, thank you for your help. Those valuable points go to you !

Regards

Morris
{---------------------------------------------------------}
ClassName::
innerHTML::&nbsp; Sentinel RealTime
innertext::  Sentinel RealTime
OuterHTML::<B>&nbsp; Sentinel RealTime</B>
OuterText::  Sentinel RealTime
{---------------------------------------------------------}
ClassName::
innerHTML::Updates
innertext::Updates
OuterHTML::<B>Updates</B>
OuterText::Updates
{---------------------------------------------------------}
ClassName::
innerHTML::Downloads
innertext::Downloads
OuterHTML::<B>Downloads</B>
OuterText::Downloads
{---------------------------------------------------------}
ClassName::sublink
innerHTML::End of Day
innertext::End of Day
OuterHTML::<A class=sublink href="endofday.html">End of Day</A>
OuterText::End of Day
{---------------------------------------------------------}
0
 
LVL 6

Expert Comment

by:DaFox
ID: 8093613
Morris, thank your for the points.

My MSHTML_TLB.pas is from 30.09.2002, but that's the date when I imported the "Microsoft HTML Object Library (Version 4.0)" ActiveX, so it doesn't mean anything. MSHTML.tlb (in system32 folder) is from 26.07.2001! It was updated with one of the Win2k service packs.

To the link prob: I'll check that. Maybe we'll find a way to get it to work. ;-)

Markus
0
 
LVL 6

Expert Comment

by:DaFox
ID: 8097836
Morris, what do you think about iterating through all parent elements of the clicked element. In you html code you're using image links. That means your html code looks like <a href...><img src...><b>link as text</b>... ...
Our element would be <b>link as text</b> in this case. If we would now go through all parent elements (Element.parentElement) we could check for "a hrefs".
What do you think?

Markus

PS: I don't understand your boolean symbol problem, sorry!
0
 

Author Comment

by:MorrisLockwood
ID: 8101564
Markus, I think that what you propose would work. However, I have side stepped the problem. I have changed the button press so that I use the right button and set Handled to true for all occurences. That is fine for my application which, by the way, is intended to be a recording scheduler for internet radio. That is why I have asked my latest question on recording.

The boolean problem was due to the fact that i was using MSHTML_TLB in which the symbol "true" is overridden by declaring "true" to be a byte.  I have found if you use MSHTLM.PAS instead, the problem goes away.

Regards

Morris
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question