8

我正在尝试使用以下代码(在 XE2 下)枚举 DOM 节点。

我从 SO 中给出的答案中借用了大部分内容,但由于某种原因,它没有做任何事情。

IOW, ProcessDOM() 永远不会被调用。

而且,我束手无策。

有人可以告诉我我在这里做错了什么。

提前致谢。

procedure ProcessNode(ANode: ICefDomNode);
var
  Node1: ICefDomNode;
begin
  if Assigned(ANode) then begin
    Node1 := ANode.FirstChild;
    while Assigned(Node1) do begin
      {Do stuff with node}
      ProcessNode(Node1);
      Node1 := Node1.NextSibling;
    end;
  end;
end;

procedure ProcessDOM(const ADocument: ICefDomDocument);
begin
  ProcessNode(ADocument.Body);
end;

procedure TMainForm.Chrome1LoadEnd(Sender: TObject; const ABrowser: ICefABrowser; const AFrame: ICefAFrame; AStatus: Integer);
begin
  if Assigned(AFrame) then AFrame.VisitDomProc(ProcessDOM);
end;
4

3 回答 3

1

我遇到了同样的问题,我使用了 dcef3 附带的演示 guiclient。使用以下它可以工作。

type TCustomRenderProcessHandler = class(TCefRenderProcessHandlerOwn)
  protected
    function OnProcessMessageReceived(const browser: ICefBrowser; sourceProcess: TCefProcessId; const message: ICefProcessMessage): Boolean; override;
end;

Chromium1.browser.SendProcessMessage(PID_RENDERER, TCefProcessMessageRef.New('visitdom')); 

function TCustomRenderProcessHandler.OnProcessMessageReceived(browser: ICefBrowser; sourceProcess: TCefProcessId; message: ICefProcessMessage): Boolean;
begin 
  if (message.Name = 'visitdom') then begin
    browser.MainFrame.VisitDomProc(
        procedure(const doc: ICefDomDocument) 
        begin
          ProcessNode(Doc.Body);
        end);
    Result := True;
  end; 
end;

initialization
  CefRenderProcessHandler := TCustomRenderProcessHandler.Create;
于 2013-03-09T22:40:40.450 回答
0

You need to add a procedure to the handler. procedure ProcessNode(ANode: ICefDomNode);

Read this: 1

于 2013-04-14T19:49:20.390 回答
0

正如这篇博客所指出的,访问渲染页面的 DOM 时的主要困难是您只能在与该页面的关联渲染器相同的进程中这样做。

您不能从浏览器线程访问 dom,您必须在渲染器线程中进行。

首先,将消息(如访问域)从浏览器进程转发到渲染进程

procedure TMainForm.crmLoadEnd(Sender: TObject; const browser: ICefBrowser;
  const frame: ICefFrame; httpStatusCode: Integer);
var
  msg : ICefProcessMessage;
begin
  if IsMain(browser, frame) then
    FLoading := False;

  msg := TCefProcessMessageRef.New('visitdom');
  browser.SendProcessMessage(PID_RENDERER, msg);
end;

其次,创建一个 TCustomRenderProcessHandler 来处理消息,将结果发送回浏览器进程。

function TCustomRenderProcessHandler.OnProcessMessageReceived(
  const browser: ICefBrowser; sourceProcess: TCefProcessId;
  const message: ICefProcessMessage): Boolean;
begin
  Result := False;
  if (message.Name = 'visitdom') then
  begin
      browser.MainFrame.VisitDomProc(
        procedure(const doc: ICefDomDocument)
          function ProcessNode(ANode: ICefDomNode) : String;
          var
            Node: ICefDomNode;
          begin
            Result := 'Not Found';
            if Assigned(ANode) then
            begin
              Node := ANode.FirstChild;
              while Assigned(Node) do
              begin
                if Node.ElementTagName='DIV' then
                begin
                  if Node.GetElementAttribute('class')='tv-panels' then
                  begin
                    Result := 'Found';
                    Exit;
                  end;
                end;
                ProcessNode(Node);
                Node := Node.NextSibling;
              end;
            end;
          end;
        var msg : ICefProcessMessage;
        begin
          msg := TCefProcessMessageRef.New('visitdom');
          msg.ArgumentList.SetString(0, processNode(doc.Body));
          browser.SendProcessMessage(PID_BROWSER, msg);
        end);
      Result := True;
  end;
end;

第三,在浏览器进程上,创建一个处理程序来处理从渲染进程发回的消息。

procedure TMainForm.crmProcessMessageReceived(Sender: TObject;
  const browser: ICefBrowser; sourceProcess: TCefProcessId;
  const message: ICefProcessMessage; out Result: Boolean);
begin
  Result := False;
  if (message.Name = 'visitdom') then
  begin
    StatusBar.SimpleText := message.ArgumentList.GetString(0);
    Result := True;
  end;
end;

请注意,在调试时,在渲染过程中放置​​断点永远不会起作用。它永远不会到达那里。

于 2016-05-12T03:11:08.930 回答