Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 646
cadetill Avatar asked Oct 06 '22 08:10

cadetill


2 Answers

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;
like image 91
cadetill Avatar answered Oct 10 '22 03:10

cadetill


I managed to get the whole page like this:

  1. Inject a DOM element - text.
ChromiumWB.Browser.MainFrame.ExecuteJavaScript('$("body").prepend(''<input type="text" id="msoftval" value=""/>'')', '', 0);
  1. Use jquery or js to get body html into injected element.
mResult := '';
ChromiumWB.Browser.MainFrame.ExecuteJavaScript('$("#msoftval").val($("body").html());', '', 0);
ChromiumWB.Browser.MainFrame.VisitDomProc(getResult);
while mResult = '' do Application.ProcessMessages;
Memo1.Text := mResult;
  1. wait untill 'VisitDomProc' finish- make it sync.
procedure TForm44.getResult(const doc: ICefDomDocument);
var
  q: ICefDomNode;
begin
  q := doc.GetElementById('msoftval');
  if Assigned(q) then
    mResult := q.GetValue
  else
    mResult := '-';
end;
like image 20
user2005049 Avatar answered Oct 10 '22 04:10

user2005049