Simon Wpgn Lewis
Simon Wpgn Lewis

Reputation: 133

how can I detect if a button was pressed on a simple webpage using embeddedwb

Am using embeddedwb on my application and I have a simple webpage with a button

<input name=mpi type=submit value=" Continue ">

I was trying with but it was no good

if E.outerHTML = '<input name=mpi type=submit value=" Continue ">' then
begin
  if rLoginGate.IsConnectedToLoginGate then
  begin
    ToggleChatBtn;
  end;
end;

now what I want to do is when I press the button I need my application to pick it up and run a simple command like a messagebox anyone got any ideas??

thanks

Upvotes: 1

Views: 514

Answers (1)

MartynA
MartynA

Reputation: 30715

A way to do this is using the HTML DOM Object model interfaces in MSHTML.PAS.

My earlier answer, here: Detect when the active element in a TWebBrowser document changes shows how to access this via that Document object of a TWebBrowser. TEmbeddedWB provides access it via its Document object, too.

That answer and the comments to it shows how to catch events related to a specific node in the document, and also specific event(s).

Of course, if the HTML is under your control, you can make things easier for yourself by giving the HTML node(s) you're interested in an ID or attribute that's easy to find via the DOM model.

The following shows how to modify the code example in my linked answer to attach an OnClick handler to a specific element node:

procedure TForm1.btnLoadClick(Sender: TObject);
var
  V : OleVariant;
  Doc1 : IHtmlDocument;
  Doc2 : IHtmlDocument2;
  E : IHtmlElement;
begin
  //  First, navigate to About:Blank to ensure that the WebBrowser's document is not null
  WebBrowser1.Navigate('about:blank');

  //  Pick up the Document's IHTMLDocument2 interface, which we need for writing to the Document
  Doc2 := WebBrowser1.Document as IHTMLDocument2;

  //  Pick up the Document's IHTMLDocument3 interface, which we need for finding e DOM
  // Element by ID
  Doc2.QueryInterface(IHTMLDocument3, Doc);
  Assert(Doc <> Nil);

  //  Load the WebBrowser with the HTML contents of a TMemo
  V := VarArrayCreate([0, 0], varVariant);
  V[0] := Memo1.Lines.Text;
  try
    Doc2.Write(PSafeArray(TVarData(v).VArray));
  finally
    Doc2.Close;
  end;

  //  Find the ElementNode whose OnClick we want to handle
  V := Doc.getElementById('input1');
  E := IDispatch(V) as IHtmlElement;
  Assert(E <> Nil);

  //  Create an EventObject as per the linked answer
  DocEvent := TEventObject.Create(Self.AnEvent, False) as IDispatch;

  //  Finally, assign the input1 Node's OnClick handler
  E.onclick := DocEvent;
end;

PS: It's a while since I used TEmbeddedWB and it may turn out that there's a more direct way of doing this stuff, because it was subject to a lot of changes after I stopped using it (in the D5 era). Even so, you won't be wasting your time studying this stuff, because COM events are applicable to all sorts of things, not just the HTML DOM model.

Upvotes: 3

Related Questions