Search code examples
delphichromiumonload-event

How ascertain that loaded page triggering OnLoadEnd corresponds to HTML of LoadString?


When I issue a

Chromium.Browser.MainFrame.LoadString(HTML,'code_url');

it appears that the OnLoadEnd event sometimes still loads the HTML of my previous LoadString (maybe several frames are being loaded separately).

I am using

if not frame.IsMain then
  Exit;

to make sure only the main frame is being considering in OnLoadEnd, but there are occasions in which a CefGetBitmap returns the bitmap of the previously loaded page (HTML).

My question: how do I know OnLoadEnd is triggered while the ChromiumFMX component is already displaying the correct page?


Solution

  • When the HTML is very concise (small), it is loaded extremely rapidly, and the OnLoadEnd event is triggered before the ChromiumFMX component is updated. To handle ChromiumFMX content, wait until the OnLoadEnd procedure has finished. This can be achieved, for instance, by waiting for it to complete in a loop.

    procedure TfrmChromiumThumbnailing.ChromiumLoadEnd(Sender: TObject;
      const browser: ICefBrowser; const frame: ICefFrame;
      httpStatusCode: Integer; out Result: Boolean);
    begin
      if (frame <> nil) and frame.IsMain then
        PageLoaded := True;
    end;
    

    In other code:

    PageLoaded := False;
    ChromiumFMX.Browser.MainFrame.LoadString(HTML,'code_url');
    while not PageLoaded do
      Application.ProcessMessages;
    <handle ChromiumFMX content>