Disclosure Statement: This site contains affiliate links, which means that I may receive a commission if you make a purchase using these links. As an eBay Partner, I earn from qualifying purchases.
If you find these projects useful please consider becoming a sponsor with Patreon, GitHub or Liberapay.

is it possible to send a message from a webpage to the delphi application?

Post Reply
francesco.faleschini
Posts: 22
Joined: Tue Nov 09, 2021 9:08 am

is it possible to send a message from a webpage to the delphi application?

Post by francesco.faleschini »

I would like to close the delphi form that displays the chromium instance from the displayed web page.

Simple example: the webpage has a Close button and on clicking it i tell the delphi form to close.

Is it possible to achieve this result? May be there is some javascript function that sends a message to the windows app?

Thank you!
Aeran
Posts: 1
Joined: Fri Nov 05, 2021 8:23 am

Re: is it possible to send a message from a webpage to the delphi application?

Post by Aeran »

You can OnConsoleMessage event. it's not an elegant method but it's pretty simple :)

HTML Code:

<html>
<head>
<script type="text/javascript">
function closeApp() {
console.log('<cmd_closeApp>');
}
</script
</head>
<body>
<button onclick="closeApp();">Click Me! </button>
</body>
</html>

Delphi Code:

unit Unit1;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, uCEFChromiumCore, uCEFChromium;

const
WM_CLOSEAPP_MESSAGE = WM_USER + $100;

type
TForm1 = class(TForm)
Chromium1: TChromium;
procedure Chromium1ConsoleMessage(Sender: TObject;
const browser: ICefBrowser; level: Cardinal; const message,
source: ustring; line: Integer; out Result: Boolean);
private
{ Private declarations }
procedure WmCloseApp(var aMsg:TMessage);message WM_CLOSEAPP_MESSAGE;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}


procedure TForm1.Chromium1ConsoleMessage(Sender: TObject;
const browser: ICefBrowser; level: Cardinal; const message, source: ustring;
line: Integer; out Result: Boolean);
begin
if CompareStr(message,'<cmd_closeApp>')=0 then
PostMessage(Handle,WM_CLOSEAPP_MESSAGE ,0,0);
end;

procedure TForm1.WmCloseApp(var aMsg: TMessage);
begin
// .. start destruction procedures...
// ..
end;

end.
dilfich
Posts: 368
Joined: Thu Nov 30, 2017 1:17 am

Re: is it possible to send a message from a webpage to the delphi application?

Post by dilfich »

Probably it is necessary ;)
https://www.briskbard.com/forum/viewtopic.php?f=8&t=1838
User avatar
salvadordf
Posts: 4580
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: is it possible to send a message from a webpage to the delphi application?

Post by salvadordf »

The console trick is the easiest way to send information from the web browser to the application but if you need more options read the code comments in the JSExtension demo :
https://github.com/salvadordf/CEF4Delphi/blob/fef34ac1e908e91f277a9a150eab75f70c4b5ed3/demos/Delphi_VCL/JavaScript/JSExtension/uJSExtension.pas#L122
francesco.faleschini
Posts: 22
Joined: Tue Nov 09, 2021 9:08 am

Re: is it possible to send a message from a webpage to the delphi application?

Post by francesco.faleschini »

Thanks to all.

I think the console message trick suffices my requirement, I will experiment with it.

Bye!
Post Reply