3

The web page here cropped up in another question here about retrieving imags from a spreadsheet.

If you navigate to the page in FF you'll see that there are two images, at the LHS of the blue header strips.

However, if I load the page into a TWebBrowser and run the following code

procedure TForm1.GetImageCount;
var
  Count : Integer;
  Doc : IHtmlDocument2;
begin
  Doc := IDispatch(WebBrowser1.Document) as IHtmlDocument2;
  Count := Doc.images.length;
  ShowMessageFmt('ImageCount: %d', [Count]);
end;

, the message box reports a count of 1 rather than the expected (by me, anyway) 2. I can easily access and save to disk the first image displayed, but not the second or any subsequent one, because they aren't in the IHtmlDocument2 Images collection of the loaded page.

So my question is, how do I get at the second image to save it to disk?

The FF debugger shows that the web page is crammed with javascript, which I imagine may be how the second image gets to be displayed, but I've no idea how to go about getting it.

Any ideas?

MartynA
  • 30,454
  • 4
  • 32
  • 73
  • 2
    Hi Martyn, the second image is located in an iframe, you need to process the iframe in the ondocumentcomplete event. – whosrdaddy Apr 02 '19 at 17:05
  • Hi,@whosrdaddy. I will have a go ... – MartynA Apr 02 '19 at 17:12
  • Also you have to take into account that web pages have generaly two way from transfering images to client computer. First is as file which in most cases gets saved into Temporary Internet Folder so that it doesen't have to be redownloaded every time. Another way that webpages transfer images to client computer is through special image data stream. Such approach is generally used for images that change often so they are not store in Temrporary Internet Folder. Also bare in mind that second aproach is also being used when webiste owners don't want other people to copy their images – SilverWarior Apr 03 '19 at 09:28
  • 1
    You will also need to take account for cross-domain frames. you will not be able to access the Frames collection in that case (there are other ways though). basically @whosrdaddy should post his comment as answer. – kobik Apr 03 '19 at 09:59
  • @whosrdaddy: Thanks. At thte moment, I can't get past the point where I access the first (and only) frame element as an IThmlWindow2, which seems to be the only thing I can access it as, but cannot drill into it because its Document member is Nil. – MartynA Apr 03 '19 at 10:44
  • @MartynA: did my answer help you or not? – whosrdaddy Apr 05 '19 at 12:23
  • @whosrdaddy I'm really soory for my delay following this up, but I've been busy dealing with a major family problem for the past couple of days. I'll try to get back to this over the weekend or early next week. – MartynA Apr 05 '19 at 18:16

1 Answers1

2

The second image in the site you linked is located in an iframe. You can access the iframe from the OnDocumentComplete event:

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.OleCtrls, SHDocVw, MsHtml;

type
  TForm1 = class(TForm)
    WebBrowser1: TWebBrowser;
    procedure WebBrowser1DocumentComplete(ASender: TObject;
      const pDisp: IDispatch; const URL: OleVariant);
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}



procedure TForm1.FormShow(Sender: TObject);
begin
 WebBrowser1.Navigate('https://www.nbbclubsites.nl/club/8000/uitslagen');
end;

procedure TForm1.WebBrowser1DocumentComplete(ASender: TObject; const pDisp: 

IDispatch; const URL: OleVariant);

var
  currentBrowser: IWebBrowser;
  topBrowser: IWebBrowser;
  Doc : IHtmlDocument2;

begin
  currentBrowser := pDisp as IWebBrowser;
  topBrowser := (ASender as TWebBrowser).DefaultInterface;
  if currentBrowser = topBrowser then
   begin
    // master document
    Doc := currentBrowser.Document as IhtmlDocument2;
    ShowMessageFmt('ImageCount: %d', [Doc.images.length]);
   end
  else
  begin
   // iframe
   Doc := currentBrowser.Document as IhtmlDocument2;
   ShowMessageFmt('ImageCount: %d', [Doc.images.length]);
  end;
end;

end.

Saving the actual image is already covered in another question

whosrdaddy
  • 11,720
  • 4
  • 50
  • 99