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.
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: 4052
- Joined: Thu Feb 02, 2017 12:24 pm
- Location: Spain
- Contact:
- salvadordf
- Posts: 4052
- 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: 4052
- 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!