开发者

TWebBrowser - Hook receive event

开发者 https://www.devze.com 2023-01-14 06:34 出处:网络
I am trying to automatically submit a form, and save the resulting image that gets shown in the TWebBrowser object.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号