Page 1 of 1

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

Posted: Tue Nov 23, 2021 10:40 am
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!

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

Posted: Tue Nov 23, 2021 10:33 pm
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.

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

Posted: Thu Nov 25, 2021 6:38 am
by dilfich
Probably it is necessary ;)
https://www.briskbard.com/forum/viewtopic.php?f=8&t=1838

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

Posted: Thu Nov 25, 2021 9:20 am
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

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

Posted: Tue Nov 30, 2021 4:47 pm
by francesco.faleschini
Thanks to all.

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

Bye!