delphichromiumtchromium

Chromium: How to get all form of a loaded page


I try to get the name of all forms of the loaded page. I have done this:

procedure TForm2.Button2Click(Sender: TObject);
var
  L: TStringList;
begin
  L := TStringList.Create;

  try
    Chromium1.Browser.MainFrame.VisitDomProc(
      procedure (const doc: ICefDomDocument)
        procedure IterateNodes(Node: ICefDomNode);
        begin
          if not Assigned(Node) then Exit;
          repeat
            if Node.ElementTagName = 'FORM' then
              L.Add(Node.GetElementAttribute('name'));

            if Node.HasChildren then IterateNodes(Node.FirstChild);

            Node := Node.NextSibling;
          until not Assigned(Node);
        end;
      begin
        IterateNodes(doc.Body);
      end
    );

    ShowMessage(L.Text);
  finally
    FreeAndNil(L);
  end;
end;

But I don't have any result. Any idea?

Thanks


Solution

  • With XE2 Update 4

    I have realized that the program flow continues when running the procedure parameter so that upon reaching the ShowMessage still has not run this procedure and therefore the TStringList is empty.

    I have put a boolean variable control and it worked right, but this is not a elegant solution.

    Here the new code:

    procedure TForm2.Button2Click(Sender: TObject);
    var
      L: TStringList;
      Finish: Boolean;
    begin
      L := TStringList.Create;
      Finish := False;
    
      try
        Chromium1.Browser.MainFrame.VisitDomProc(
          procedure (const doc: ICefDomDocument)
            procedure IterateNodes(Node: ICefDomNode);
            begin
              if not Assigned(Node) then Exit;
              repeat
                if SameText(Node.ElementTagName, 'FORM') then
                begin
                  L.Add(Node.GetElementAttribute('name'));
                end;
    
                if Node.HasChildren then
                  IterateNodes(Node.FirstChild);
    
                Node := Node.NextSibling;
              until not Assigned(Node);
            end;
          begin
            IterateNodes(doc.Body);
            Finish := True;
          end
        );
    
        repeat Application.ProcessMessages until (Finish);
        ShowMessage(L.Text);
      finally
        FreeAndNil(L);
      end;
    end;