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.
How to communicate with a modal dialog
- salvadordf
- Posts: 4564
- Joined: Thu Feb 02, 2017 12:24 pm
- Location: Spain
- Contact:
Re: How to communicate with a modal dialog
Hi,
By default CEF creates the JavaScript dialogs but you can replace them with custom dialogs made with Delphi or Lazarus.
See the JSDialog demo in the demos/Delphi_VCL/JavaScript/JSDialog directory for more details.
By default CEF creates the JavaScript dialogs but you can replace them with custom dialogs made with Delphi or Lazarus.
See the JSDialog demo in the demos/Delphi_VCL/JavaScript/JSDialog directory for more details.
- salvadordf
- Posts: 4564
- Joined: Thu Feb 02, 2017 12:24 pm
- Location: Spain
- Contact:
Re: How to communicate with a modal dialog
CEF triggers the TChromium.OnJsdialog event when JavaScript needs to open a dialog.
The code comments of that event in CEF's source are these :
https://bitbucket.org/chromiumembedded/ ... h#lines-79
It's also available here :
https://magpcss.org/ceforum/apidocs3/pr ... ndler.html
The JSDialog demo saves the parameters and sends a message to the main form to show the custom dialog in the main application thread because TChromium.OnJsdialog is executed in a CEF thread called "UI" and you need to create, destroy or modify the VCL controls in the main thread.
TChromium.OnJsdialog sends a CEFBROWSER_SHOWJSDIALOG message and the form executes the JSDialogBrowserFrm.ShowJSDialogMsg procedure. The demo calls "showmessage", "MessageDlg" or "InputBox" depending on the dialog type specified in TChromium.OnJsdialog and then it calls "FCallback.cont" with the results.
The FCallback.cont procedure has these code comments :
https://bitbucket.org/chromiumembedded/ ... h#lines-60
They are also available here :
https://magpcss.org/ceforum/apidocs3/pr ... lback.html
If you need to simulate a click on the "yes" button in those dialogs all you have to do is set the "success" parameter to TRUE when you call FCallback.cont.
The code comments of that event in CEF's source are these :
https://bitbucket.org/chromiumembedded/ ... h#lines-79
Code: Select all
///
// Called to run a JavaScript dialog. If |origin_url| is non-NULL it can be
// passed to the CefFormatUrlForSecurityDisplay function to retrieve a secure
// and user-friendly display string. The |default_prompt_text| value will be
// specified for prompt dialogs only. Set |suppress_message| to true (1) and
// return false (0) to suppress the message (suppressing messages is
// preferable to immediately executing the callback as this is used to detect
// presumably malicious behavior like spamming alert messages in
// onbeforeunload). Set |suppress_message| to false (0) and return false (0)
// to use the default implementation (the default implementation will show one
// modal dialog at a time and suppress any additional dialog requests until
// the displayed dialog is dismissed). Return true (1) if the application will
// use a custom dialog or if the callback has been executed immediately.
// Custom dialogs may be either modal or modeless. If a custom dialog is used
// the application must execute |callback| once the custom dialog is
// dismissed.
///
https://magpcss.org/ceforum/apidocs3/pr ... ndler.html
The JSDialog demo saves the parameters and sends a message to the main form to show the custom dialog in the main application thread because TChromium.OnJsdialog is executed in a CEF thread called "UI" and you need to create, destroy or modify the VCL controls in the main thread.
TChromium.OnJsdialog sends a CEFBROWSER_SHOWJSDIALOG message and the form executes the JSDialogBrowserFrm.ShowJSDialogMsg procedure. The demo calls "showmessage", "MessageDlg" or "InputBox" depending on the dialog type specified in TChromium.OnJsdialog and then it calls "FCallback.cont" with the results.
The FCallback.cont procedure has these code comments :
https://bitbucket.org/chromiumembedded/ ... h#lines-60
Code: Select all
///
// Continue the JS dialog request. Set |success| to true (1) if the OK button
// was pressed. The |user_input| value should be specified for prompt dialogs.
///
https://magpcss.org/ceforum/apidocs3/pr ... lback.html
If you need to simulate a click on the "yes" button in those dialogs all you have to do is set the "success" parameter to TRUE when you call FCallback.cont.
Re: How to communicate with a modal dialog
Hello.
How to automate press OK in this case?

But this procedure is not called in (checking in Debug mode)
How to automate press OK in this case?

Code: Select all
...
FChromium.OnJsdialog := ChromiumJsdialog;
...
procedure TMyThread_Chromium.ChromiumJsdialog(Sender: TObject; const browser: ICefBrowser; const originUrl: ustring; dialogType: TCefJsDialogType; const messageText, defaultPromptText: ustring; const callback: ICefJsDialogCallback; out suppressMessage, Result: boolean);
begin
callback.Cont(True, '');
Result := True;
end;
- salvadordf
- Posts: 4564
- Joined: Thu Feb 02, 2017 12:24 pm
- Location: Spain
- Contact:
Re: How to communicate with a modal dialog
Try the TChromium.OnBeforeUnloadDialog event.
Re: How to communicate with a modal dialog
As always - perfect answer!
Thank you!
Re: How to communicate with a modal dialog
I have a couple questions regarding the default modal dialogs.
I have my own layer/framework on top of CEF4Delphi using primary offscreen rendering and supporting different versions of CEF4Delphi/CEF. Compiled in XE5 with small changes in the core CEF4Delphi code. In a project when no OnJsdialog handle is set I noticed that the default dialogs are working in V100 and not working in V106 onward (V127, V131). I'm not sure between V100 and V106 because I did not implemented the support for versions in between (yet). When not working all just fast forward to cancel or ok result not showing anything interactive. Is this expected or maybe I miss something crucial that somehow allowed them to work in older versions?
The second question is that when working (in V100) the dialogs are subject to the well-known Delphi (I suppose) modal bug. It's where the dialog is there but falls behind the main window for some reason related to parenting of windows. I know that I can fight this bug when all the windows are mine, but in this case the popup dialogs belong to CEF framework. Can this be fixed somehow?
Overall I suspect that the best scenario is to implement my own standard dialogs handlers. Since this is the main post about this functionality, I hope the answer will help others also
Thanks
I have my own layer/framework on top of CEF4Delphi using primary offscreen rendering and supporting different versions of CEF4Delphi/CEF. Compiled in XE5 with small changes in the core CEF4Delphi code. In a project when no OnJsdialog handle is set I noticed that the default dialogs are working in V100 and not working in V106 onward (V127, V131). I'm not sure between V100 and V106 because I did not implemented the support for versions in between (yet). When not working all just fast forward to cancel or ok result not showing anything interactive. Is this expected or maybe I miss something crucial that somehow allowed them to work in older versions?
The second question is that when working (in V100) the dialogs are subject to the well-known Delphi (I suppose) modal bug. It's where the dialog is there but falls behind the main window for some reason related to parenting of windows. I know that I can fight this bug when all the windows are mine, but in this case the popup dialogs belong to CEF framework. Can this be fixed somehow?
Overall I suspect that the best scenario is to implement my own standard dialogs handlers. Since this is the main post about this functionality, I hope the answer will help others also

Thanks
- salvadordf
- Posts: 4564
- Joined: Thu Feb 02, 2017 12:24 pm
- Location: Spain
- Contact:
Re: How to communicate with a modal dialog
Hi,
Please, download the latest CEF4Delphi version from GitHub.
I just uploaded a modified version of SimpleOSRBrowser that shows custom JavaScript dialogs in order to avoid a known issue :
https://github.com/salvadordf/CEF4Delphi/issues/548
You can use a custom panel to show the same information from the JS dialogs and bring it to the front of the browser instead of using showmessage, MessageDlg or InputBox.
Please, download the latest CEF4Delphi version from GitHub.
I just uploaded a modified version of SimpleOSRBrowser that shows custom JavaScript dialogs in order to avoid a known issue :
https://github.com/salvadordf/CEF4Delphi/issues/548
You can use a custom panel to show the same information from the JS dialogs and bring it to the front of the browser instead of using showmessage, MessageDlg or InputBox.
Re: How to communicate with a modal dialog
Thanks, so you basically recommend to avoid the default dialogs of CEF at any cost? Personally I don't find their design appealing (the ones I saw in V100) so it wouldn't be a hard decision.salvadordf wrote: Wed Feb 26, 2025 4:05 pm You can use a custom panel to show the same information from the JS dialogs and bring it to the front of the browser instead of using showmessage, MessageDlg or InputBox.
- salvadordf
- Posts: 4564
- Joined: Thu Feb 02, 2017 12:24 pm
- Location: Spain
- Contact:
Re: How to communicate with a modal dialog
CEF browsers in OSR mode use the Alloy runtime style and there's a known issue with JavaScript dialogs when the Alloy style is enabled (see the previous link).
That's why I updated the SimpleOSRBrowser demo and suggested using custom dialogs.
That's why I updated the SimpleOSRBrowser demo and suggested using custom dialogs.