Page 1 of 1

Re: How to communicate with a modal dialog

Posted: Thu Dec 26, 2019 3:00 pm
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.

Re: How to communicate with a modal dialog

Posted: Fri Dec 27, 2019 5:53 pm
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.

Re: How to communicate with a modal dialog

Posted: Mon Sep 21, 2020 2:33 pm
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)

Re: How to communicate with a modal dialog

Posted: Mon Sep 21, 2020 3:13 pm
by salvadordf
Try the TChromium.OnBeforeUnloadDialog event.

Re: How to communicate with a modal dialog

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

Re: How to communicate with a modal dialog

Posted: Wed Feb 26, 2025 12:53 pm
by maxedi
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

Re: How to communicate with a modal dialog

Posted: Wed Feb 26, 2025 4:05 pm
by salvadordf
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.

Re: How to communicate with a modal dialog

Posted: Wed Feb 26, 2025 10:11 pm
by maxedi
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.
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.

Re: How to communicate with a modal dialog

Posted: Thu Feb 27, 2025 12:52 pm
by salvadordf
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.