So I am trying to access DOM when the render process completes rendering the content of the browser.
But I found that OnLoadEnd will be called multiple times. Is there another function that will be called after the render process completely finish the rendering process other than CefLoadHandler::OnLoadEnd()?
I noticed that for Google Website, this OnLoadEnd will be called once. But other websites, like https://www.rhs.org.uk/, the OnLoadEnd will be called endless times.
What I did is the following:
// const char parseDOMMessage[] = "RenderProcessNowParsingDOM";
void CDlgWebView::OnLoadEnd( int httpStatusCode)
{
// Browser process will call OnLoadEnd to indicate the completion of render process
// We will send a message back to the render process to parse the DOM
if (fCefBrowser) {
CefRefPtr<CefProcessMessage> parseMessage =
CefProcessMessage::Create(parseDOMMessage);
fCefBrowser->SendProcessMessage(PID_RENDERER, parseMessage);
}
}
bool DemoCefApp::DemoCefRenderProcessHandler::OnProcessMessageReceived(CefRefPtr<CefBrowser> browser,
CefProcessId source_process,
CefRefPtr<CefProcessMessage> message) {
if (message->GetName() == "RenderProcessNowParsingDOM") {
CefRefPtr<CefFrame> mainFrame = browser->GetMainFrame();
CefRefPtr<CefDOMVisitor> visitor = new DemoCefDomVisitor();
mainFrame->VisitDOM(visitor);
return true;
}
return false;
}
void DemoCefApp::DemoCefDomVisitor::Visit(CefRefPtr<CefDOMDocument> document) {
CefString title;
if (document) {
title = document->GetTitle();
std::string t = title.ToString();
// convert to std::wstring
std::wstring ws;
ws.assign(t.begin(), t.end());
LPCWSTR pcwstr = ws.c_str();
OutputDebugString(pcwstr); // it was printed out the title of the URL endless times
}
Any help please?