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

Post Reply
User avatar
salvadordf
Posts: 4016
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: How to communicate with a modal dialog

Post by salvadordf »

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

Re: How to communicate with a modal dialog

Post by salvadordf »

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

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.
  ///
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

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.
  ///
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.
gresaggr
Posts: 22
Joined: Mon Mar 04, 2019 12:41 pm

Re: How to communicate with a modal dialog

Post by gresaggr »

Hello.
How to automate press OK in this case?
Image

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;
But this procedure is not called in (checking in Debug mode)
User avatar
salvadordf
Posts: 4016
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: How to communicate with a modal dialog

Post by salvadordf »

Try the TChromium.OnBeforeUnloadDialog event.
gresaggr
Posts: 22
Joined: Mon Mar 04, 2019 12:41 pm

Re: How to communicate with a modal dialog

Post by gresaggr »

salvadordf wrote: Mon Sep 21, 2020 3:13 pm Try the TChromium.OnBeforeUnloadDialog event.
As always - perfect answer!
Thank you!
Post Reply