我在使用 TChromium (DCEF1) 的 OnLoadEnd 事件时遇到问题。
我有一个带有 TButton 和 TChromium 的表单。
按钮的 OnClick 事件调用一个函数,该函数列出了已加载页面的表单。如果我等待页面加载完成然后单击按钮,此功能可以正常工作;但是如果我从 TChromium OnLoadEnd 事件处理程序调用此函数,则永远不会调用回调函数,因此,我得到一个空列表。
按钮代码(将注释读入代码):
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 代码:
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;
获取表单名称的方法代码(将注释读入代码):
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;