OnLoadEnd called several times from the Chromum Embedded Framework

Using CEF Browser I am trying to connect to the OnLoadEnd event to traverse the DOM tree.

For some strange reason, I get a VisitDom call 2 times.

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; 

Any idea why OnLoadEnd is being called multiple times?

+4
source share
1 answer

It seems that OnLoadEnd is called with HttpStatusCode = 200 for each asset that the page has: images, external scripts, etc.

The solution is to check the loading of the main frame - Frame.IsMain = True.

 if (HttpStatusCode = 200) and Frame.IsMain then begin Visitor := TCefFastDomVisitor.Create(VisitDom); FBrowser.Browser.MainFrame.VisitDom(Visitor); end; 
+5
source

Source: https://habr.com/ru/post/1403765/


All Articles