TWebBrowser - Hook receive event
I am trying to automatically submit a form, and save the resulting image that gets shown in the TWebBrowser object.
The image gets loaded over several chained javascript requests (ajax), until it finally appear开发者_JS百科s in the document.
What is the best way to get this image? I thought of hooking the receive function to be able to see the http response (which would basically be my image).
Another possibility could be to load the image from cache / memory...
I don't have any idea how to practically do this, I hope someone can help.
Thanks.
you can retrieve all the url images using the images property from the IHTMLDocument2 object.
see this sample using the OnDocumentComplete Event.
procedure TForm2.WebBrowser1DocumentComplete(ASender: TObject; const pDisp: IDispatch; var URL: OleVariant);
var
HTMLDocument2: IHTMLDocument2;
i : Integer;
Item : IHTMLElement;
ImageUrl : string;
begin
HTMLDocument2 := (WebBrowser1.Document AS IHTMLDocument2);
for i := 0 to HTMLDocument2.images.length -1 do
begin
Item := HTMLDocument2.images.item(i, null) As IHTMLElement;
ImageUrl:=item.getAttribute('src',0);
//do your stuff with the url image retrieved
end;
end;
You can use the OnDocumentComplete
or OnNavigateComplete2
events (see the SHDocVw help) or wait for the WebBrowser
to be in a ReadyState READYSTATE_COMPLETE
and then read from the WebBrowser.Document
.
But you can also (simpler IMO) use a TIdHTTP.Get
to directly get the response stream.
To be more scalable with your application you can directly try EmbeddedWB. It wraps IWebBrowser2 and very handy to use. Embarcadero use EmbeddedWB in their RADStudio.
精彩评论