使用CEF浏览器我尝试挂钩OnLoadEnd事件以遍历DOM树. 由于一些奇怪的原因,我得到了两次VisitDom. procedure TForm1.FormCreate(Sender: TObject);begin FBrowser := TChromium.Create(Self); FBrowser.Parent := TWinControl(S
由于一些奇怪的原因,我得到了两次VisitDom.
procedure TForm1.FormCreate(Sender: TObject); begin FBrowser := TChromium.Create(Self); FBrowser.Parent := TWinControl(Self); FBrowser.OnLoadEnd := BrowserOnLoadEnd; FBrowser.Load('http://google.com'); end; procedure VisitDom(const Document: ICefDomDocument); begin ShowMessage(Document.Document.Name); end; procedure TForm1.BrowserOnLoadEnd(Sender: TObject; const Browser: ICefBrowser; const Frame: ICefFrame; HttpStatusCode: Integer; out Result: Boolean); var Visitor: TCefFastDomVisitor; begin if HttpStatusCode = 200 then begin Visitor := TCefFastDomVisitor.Create(VisitDom); FBrowser.Browser.MainFrame.VisitDom(Visitor); end; end;
知道为什么多次调用OnLoadEnd?
似乎OnLoadEnd是使用HttpStatusCode = 200为页面所拥有的每个资产调用的,例如:图像,外部脚本等.解决方案是检查正在加载的主框架 – Frame.IsMain = True.
if (HttpStatusCode = 200) and Frame.IsMain then begin Visitor := TCefFastDomVisitor.Create(VisitDom); FBrowser.Browser.MainFrame.VisitDom(Visitor); end;