Newsgroups : Borland : borland.public.delphi.nativeapi.win32 : 2005 Jul : TWebBrowser question

www.cryer.info
Managed Newsgroup Archive

TWebBrowser question

Subject:TWebBrowser question
Posted by:"TMA" (tmasoftwar..@ig.com.br)
Date:Fri, 15 Jul 2005 11:33:44

Hi,

I need to write a function to retrieve the contents of an URL and store it
to a TWebBrowser. Here is the code i wanted to use:

procedure TmyForm.Button1Click(Sender: TObject);
var WB: TWebBrowser;
begin
    WB := GetURLContent(edURL.Text);
    If Assigned(WB) Then // content were sucessfully downloaded
       do some processing to wb here
    Else
        ShowMessage('Page is unavailable!');
end;

function GetURLContent(URL: String): TWebBrowser;
var TimeOut: Integer;
begin
   Result := TWebBrowser.Create(Nil);
    With Result Do
         Begin
               Parent := Self;
               Silent := True;
               Navigate(edURL.Text);
               TimeOut := 50;
               Repeat
                    Application.ProcessMessages;
                    Sleep(200);
                    Dec(TimeOut);
                    If TimeOut < 1 Then
                        Begin
                              FreeAndNil(Result);
                              Break;
                        End;
               Until ReadyState = 4;
         End;
end;

Main Problem: It seems this loop REPEAT .. UNTIL READYSTATE = 4 keeps
TWebBrowser from downloading the URL. TimeOut will ALWAYS occurr if i do
that.

Secondary Problem: TWebBrowser pops up this Script Error dialog (not
displayied in an IE window for the same URL). It would be great to have
access to some code to handle it so I could click YES (keep running scripts
on this page) for all the pop up windows. Setting SILENT := TRUE avoids this
dialog, but I heard it doesnt work on all URL's.

Workaround 1: I tried to use a NAVIGATECOMPLETE2 event to catch this
READYSTATE = 4, but 1st) i would have to use another structure (not a simple
function) to get the results ; 2nd) it's triggered EVERYTIME a frame is
downloaded, so if i have like 10 frames in the HTML code, it'll be triggered
10 times; 3rd) i've noticed that sometimes it's not triggered when the HTML
page is fully downloaded (which is what I really wanted it to do)

Workaround 2: I tried to use a timer instead of the REPEAT ... UNTIL thing.
It seems to fix the problem that TWebBrowser won't download the page, but
again, it wouldn't be possible to write a function that is completed after a
timeout or a completion.

I'd appreciate if somebody could tell me a correct way to call this REPEAT
... UNTIL thing cos it would be better to what i'm trying to do.
Thanks in advance.

Replies:

www.cryer.info
Managed Newsgroup Archive