4

I'm using Chromium component in a Delphi application.

I'd like the following behaviour:

When user clicks a specific button in a web page, the Delphi application (the 'container') must execute a command (launch an external executable with ...).

Is it possible ?

Whiler
  • 7,757
  • 3
  • 28
  • 55
henry60
  • 443
  • 10
  • 23
  • check the event: OnBeforeBrowse... you'll have access to the link requested (*request.Url*), and so, you will be able to launch your external application – Whiler May 16 '12 at 08:20
  • 1
    @Whiler, not only to request link, I was thinking more about something like: `if (request.Method = 'POST') and (navType = NAVTYPE_FORMSUBMITTED) and (request.Url = 'https://login.example.com/login') then` if this condition passes, return True to `Result` and execute whatever you want. That's why I've asked what's behind ;-) – TLama May 16 '12 at 08:26

1 Answers1

7

Update:

Since you've actually asked for DOM event listener for click events, check the following example listening the Google search button click event (the element with ID gbqfba):

uses
  ShellAPI, cefvcl, ceflib;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Chromium1.Load('www.google.com');
end;

procedure OnClickEvent(const AEvent: ICefDomEvent);
begin
  ShellExecute(Form1.Handle, nil, 'notepad.exe', nil, nil, SW_SHOWNORMAL);
end;

procedure OnExploreDOM(const ADocument: ICefDomDocument);
var
  DOMNode: ICefDomNode;
begin
  DOMNode := ADocument.GetElementById('gbqfba');
  if Assigned(DOMNode) then
    DOMNode.AddEventListenerProc('click', True, OnClickEvent);
end;

procedure TForm1.Chromium1LoadEnd(Sender: TObject; const browser: ICefBrowser;
  const frame: ICefFrame; httpStatusCode: Integer; out Result: Boolean);
begin
  if Assigned(frame) then
  begin
    // here you should check the frame.Url to verify if you're on the right URL
    // before you try to search for the element and attach the event if found
    frame.VisitDomProc(OnExploreDOM);
  end;
end;
TLama
  • 71,521
  • 15
  • 192
  • 348
  • 1
    +1 ;o)) `ShellExecute(Handle, 'open', 'notepad.exe', nil, 'c:\Windows\', SW_SHOWNORMAL);` (whatever he wants) – Whiler May 16 '12 at 08:32
  • 1
    Thank You very much, this solves partially my problem: in fact I need the following thing: is it possible to have, when a button in web page is clicked, a notification from Chromium component to host application ? – henry60 May 17 '12 at 09:54
  • Ok, and what if I want an 'after' notification ? I mean, in Your example, if I want a notification when browser has received the response to POST ? – henry60 May 17 '12 at 11:30
  • Good question, but it won't be so easy, IMHO the only clean way is through request callbacks, because the browser itself is not able to know what or where you'll be navigated when e.g. the login after the POST request succeed, fails or unexpected error occurs. But this is out of scope of this question, try to post the new question with this specific problem. – TLama May 17 '12 at 15:24
  • Eventually, but this needs some work.. you can use a proxy server from Indy components by example.. to catch the request/response... – Whiler May 17 '12 at 20:10
  • @Rob, then DOM event listener will be on a place. Just another question what I've tried to deal with unnecessarily intricately. – TLama May 18 '12 at 19:33
  • 1
    I'm not sure what you mean by "on a place," but this solution looks like the kind of solution I was expecting. As for your compatibility note, I think the only reason you code requires Delphi 2009 is for the anonymous functions. If they were standalone functions instead, then the code would work with any Delphi version the Chrome control works with. – Rob Kennedy May 18 '12 at 20:17