Page 1 of 1

Using global variable for comparison in DOMIteration

Posted: Wed Sep 08, 2021 5:28 pm
by jc4golf
I'm trying to use a global variable to compare to an element's inner text. The global variable I'm using is CourseTeeName in the following code, which works:

Code: Select all

procedure SimpleDOMIteration3(const aDocument: ICefDomDocument;
  var x, y, w, h: Integer; var t: String);
var
  TempNode: ICefDomNode;

  procedure ProcessNode(ANode: ICefDomNode);
  var
    Node: ICefDomNode;
  begin
    if ANode <> nil then
    begin
      Node := ANode.FirstChild;
      while Node <> nil do
      begin
        if Node.HasElementAttribute('href') then
        begin
          t := Node.ElementInnerText;
          x := Node.ElementBounds.x;
          y := Node.ElementBounds.y;
          w := Node.ElementBounds.width;
          h := Node.ElementBounds.height;
          if t = CourseTeeName then
            Exit;
        end;
        ProcessNode(Node);
        Node := Node.NextSibling;
      end;
    end;
  end;

begin
  try
    if (aDocument <> nil) then
    begin
      TempNode := aDocument.GetElementById('gvCourses');
      ProcessNode(TempNode);
    end;
  except
    on e: exception do
      if CustomExceptionHandler('SimpleDOMIteration3', e) then
        raise;
  end;
end;

procedure DOMVisitor_OnDocAvailable3(const browser: ICefBrowser;
  const frame: ICefFrame; const document: ICefDomDocument);
var
  x, y, w, h: Integer;
  t: String;
  TempMessage: ICefProcessMessage;
begin
  x := 0;
  y := 0;
  w := 0;
  h := 0;
  t := '';
  SimpleDOMIteration3(document, x, y, w, h, t);
  try
    TempMessage := TCefProcessMessageRef.New(MSGNAME_SEND_TEELIST_COORDINATES);
    TempMessage.ArgumentList.SetString(0, IntToStr(x));
    TempMessage.ArgumentList.SetString(1, IntToStr(y));
    TempMessage.ArgumentList.SetString(2, IntToStr(w));
    TempMessage.ArgumentList.SetString(3, IntToStr(h));
    TempMessage.ArgumentList.SetString(4, t);
    if (frame <> nil) and frame.IsValid then
      frame.SendProcessMessage(PID_BROWSER, TempMessage);
  finally
    TempMessage := nil;
  end;
end;
The following code, which uses the same global variable does not work (if I change the code to compare to a literal it works):

Code: Select all

procedure SimpleDOMIteration1(const aDocument: ICefDomDocument;
  const searchclass: String; var x, y, w, h: Integer; var t: String);
var
  TempNode: ICefDomNode;

  procedure ProcessNode(ANode: ICefDomNode);
  var
    Node: ICefDomNode;
    nodeclass, nodecourse: String;
  begin
    if ANode <> nil then
    begin
      Node := ANode.FirstChild;
      while Node <> nil do
      begin
        nodeclass := Node.GetElementAttribute('class');
        if nodeclass = searchclass then
        begin
          nodecourse := Node.ElementInnerText;
          if nodecourse = CourseTeeName then
          begin
            t := nodecourse;
            x := Node.ElementBounds.x;
            y := Node.ElementBounds.y;
            w := Node.ElementBounds.width;
            h := Node.ElementBounds.height;
            Exit;
          end;
        end;
        ProcessNode(Node);
        Node := Node.NextSibling;
      end;
    end;
  end;
  
  procedure DOMVisitor_OnDocAvailable1(const browser: ICefBrowser;
  const frame: ICefFrame; const document: ICefDomDocument);
var
  x, y, w, h: Integer;
  t: String;
  TempMessage: ICefProcessMessage;
begin
  x := 0;
  y := 0;
  w := 0;
  h := 0;
  t := '';
  SimpleDOMIteration1(document, 'item__name', x, y, w, h, t);
  try
    TempMessage := TCefProcessMessageRef.New(MSGNAME_SEND_COORDINATES);
    TempMessage.ArgumentList.SetString(0, IntToStr(x));
    TempMessage.ArgumentList.SetString(1, IntToStr(y));
    TempMessage.ArgumentList.SetString(2, IntToStr(w));
    TempMessage.ArgumentList.SetString(3, IntToStr(h));
    TempMessage.ArgumentList.SetString(4, t);
    if (frame <> nil) and frame.IsValid then
      frame.SendProcessMessage(PID_BROWSER, TempMessage);
  finally
    TempMessage := nil;
  end;
end;
I can't see what the difference might be.

Re: Using global variable for comparison in DOMIteration

Posted: Thu Sep 09, 2021 12:31 am
by jc4golf
Update:
I forgot that I could send the variable in the message argument list to be used in the DOMIteration. Now that I've made that change, it works perfectly.