?
Solved

Getting link information using TWebBrowser

Posted on 2003-03-05
9
Medium Priority
?
207 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: 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!

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses
Course of the Month13 days, 5 hours left to enroll

777 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