I have a problem using the OnLoadEnd event of a TChromium (DCEF1).
I have a form with a TButton and a TChromium.
The OnClick event of the button calls a function which lists the forms of the loaded page. If I wait the page finish loading and then click the button, this function works OK; but if I call this function from the TChromium OnLoadEnd event handler the callback function is never called and thus, I get a empty list.
Button code (read the comments into the code):
procedure TForm2.Button3Click(Sender: TObject);
var
Q: TWebChromium;
begin
Q := TWebChromium.Create(Chromium1); // <- class to access DOM
Q.WebFormNames; // <- method to get forms name
ShowMessage(Q.Forms.Text); // <- show forms
end;
OnLoadEnd code:
procedure TForm2.Chromium1LoadEnd(Sender: TObject; const browser: ICefBrowser;
const frame: ICefFrame; httpStatusCode: Integer; out Result: Boolean);
begin
if (browser <> nil) and (browser.GetWindowHandle = TChromium(Sender).BrowserHandle) and ((frame = nil) or (frame.IsMain)) then
begin
Button3Click(nil);
end;
end;
Method code to obtain forms name (read the comments into the code):
procedure TWebChromium.WebFormNames;
var
Finish: Boolean;
EndTime: TTime;
begin
FForms.Clear; // <- property (TStringList)
if not Assigned(FWebBrowser) then // <- FWebBrowser: property that contain the TChromium
raise Exception.Create('WebBrowser not assigned');
if not (FWebBrowser is TChromium) then
raise Exception.Create('The WebBrowser property is not a TChromium.');
Finish := False;
TChromium(FWebBrowser).Browser.MainFrame.VisitDomProc(
procedure (const doc: ICefDomDocument) // <- this procedure is not called if this method is called from OnLoadEnd event
begin
FForms.CommaText := GetFormsName(doc.Body);
Finish := True;
end
);
EndTime := IncSecond(Time, 4);
repeat Application.ProcessMessages until Finish or (Time > EndTime);
if Time > EndTime then
raise Exception.Create('Time out');
end;
this code
I've tested and worked (when I've been posting that answer, I've been using version before trunk from that post date). Actually still works with a snapshot I have from the past. In current trunk DOM visiting doesn't seem to work at all. – EarwitnessOnLoadEnd
event. The document is ready to be visited. – Earwitness