본문 바로가기
Delphi Tip/인터넷

Capture element click in Internet Explorer

by MonoSoft 2021. 11. 30.
728x90
반응형

Capture element click in Internet Explorer

 

 

I’m trying to write an automatic IE form filler for myself. 

I’m able to get the URL (from IE not TWebBrowser) and

if it’s “managed” by my app fill in the fields and submit it.

I’d like to ask the user if they want to add a non-managed

URL to the managed list when the submit button is clicked when

a password field is found on the page.

It seems to me that HTMLFormElementEvents2 would be the way to go.

However, I have no idea how to do it. I’m just a weekend

warrior with just enough knowledge to be dangerous lol.

 

Below is a basic example of what I’m trying to do.

 

Any help or direction to online tutorials would be appreciated.

 

{code}

 

function Alert: Boolean; begin

  ShowMessage('You clicked submit');

end;

 

procedure TForm1.Button5Click(Sender: TObject);

var

  ShellWindow: IShellWindows;

  WB: IWebbrowser2;

  spDisp: IDispatch;

  IDoc1: IHTMLDocument2;

  Document: Variant;

  k, m: Integer;

  ovElements : OleVariant;

  MyElement: HTMLFormElementEvents2;

  i: Integer;

begin

  ShellWindow := CoShellWindows.Create; // get the running instance of Internet Explorer

 

  for k := 0 to ShellWindow.Count do

  begin

    spDisp := ShellWindow.Item(k);

    if spDisp = nil then Exit;

    // QueryInterface determines if an interface can be used with an object

    spDisp.QueryInterface(iWebBrowser2, WB); // Do the same for MyElement object

    spDisp.QueryInterface(HTMLFormElementEvents2, myelement);

    if WB <> nil then

    begin

      WB.Document.QueryInterface(IHTMLDocument2, iDoc1);

      if iDoc1 <> nil then

      begin

        WB := ShellWindow.Item(k) as IWebbrowser2;

        begin

          Document := WB.Document; // count forms on document and iterate through its forms

          for m := 0 to Document.forms.Length - 1 do

          begin

            ovElements := Document.forms.Item(m).elements; // iterate through elements

            for i := 0 to ovElements.Length - 1 do // when a text field found enter a value

            try

              if (CompareText(ovElements.item(i).tagName, 'INPUT') = 0) and

                 (CompareText(ovElements.item(i).type, 'TEXT') = 0) then

                ovElements.Item(i).Value := 'Some useful information';

              if (CompareText(ovElements.item(i).tagName, 'INPUT') = 0) and

                 (CompareText(ovElements.item(i).type, 'SUBMIT') = 0) then

              // I want to do something if the user clicks this item. I have no idea how this should be used.

              // this is just a non working example MyElement.onclick(ovElements.item(i)) := Alert;

            except

            end;

         end;

       end;

    end;

end;

end;

end;

{code} 


"Delphi 7 running on Windows 7"

728x90
반응형

댓글