Delphi Chromium - launch a command in Delphi application when button in web page is clicked by user
Asked Answered
F

1

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 ?

Fussy answered 16/5, 2012 at 7:51 Comment(2)
check the event: OnBeforeBrowse... you'll have access to the link requested (request.Url), and so, you will be able to launch your external applicationSunshine
@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 ;-)Pistachio
P
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;
Pistachio answered 16/5, 2012 at 8:28 Comment(7)
+1 ;o)) ShellExecute(Handle, 'open', 'notepad.exe', nil, 'c:\Windows\', SW_SHOWNORMAL); (whatever he wants)Sunshine
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 ?Fussy
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 ?Fussy
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.Pistachio
Eventually, but this needs some work.. you can use a proxy server from Indy components by example.. to catch the request/response...Sunshine
@Rob, then DOM event listener will be on a place. Just another question what I've tried to deal with unnecessarily intricately.Pistachio
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.Bonaire

© 2022 - 2024 — McMap. All rights reserved.