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.
If you find these projects useful please consider becoming a sponsor with Patreon, GitHub or Liberapay.

How To Disable Js window.close and let's everthing stay OK?

Post Reply
crystalxp
Posts: 39
Joined: Tue Jul 04, 2017 8:23 am

How To Disable Js window.close and let's everthing stay OK?

Post by crystalxp »

Hello,Thanks for your great job, I have a problem in disable Js window.close
Some web page will use javascript "window.close" to close web window,but it's bad for me ,becacuse whole application will terminated after that,
so i use onClose event and give a Result:=True Value to prevent this operation.
but after this ,the TChromium's FClosing will changed to true,this cause the OnJsDialog and ShowDevTools function runs in wrong judgement....now time, I Used the class helper to intrude this value.... or maybe OnClose event should Sending the FClosing Value to inner to change it's value...

My question is "How to prevent Js window.close() operation and let's TChromium's statues goes ok"?

Waiting for your suggestion...thank you .
User avatar
salvadordf
Posts: 4620
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: How To Disable Js window.close and let's everthing stay OK?

Post by salvadordf »

Setting TChromium.Options.JavascriptCloseWindows to STATE_DISABLED before the TChromium.CreateBrowser call should stop JavaScript code from closing windows that were not created by JavaScript but I've seen some contradictory information about this setting and I don't have much time to test it.

I'll modify the TChromium.OnClose event the next time there's a new version of the CEF binaries. It will replace the "Result" parameter for other parameter that allows to cancel, close or delay the closing sequence.

If you need to access a protected field or change the way some protected procedure works you can always create a custom class that inherits from TChromium and you will be able to override almost everything. I rarely use private fields in CEF4Delphi and many procedures and functions are virtual to allow full access to inherited classes. If I forgot to declare a procedure as virtual and you need to override it, just tell me and I'll make it virtual.

There is no need to create class helpers.
User avatar
salvadordf
Posts: 4620
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: How To Disable Js window.close and let's everthing stay OK?

Post by salvadordf »

Please, download CEF4Delphi again from GitHub :
https://github.com/salvadordf/CEF4Delphi

Modify the TChromium.OnClose event in your app and set the new aAction parameter to cbaCancel to stop closing the browser.
crystalxp
Posts: 39
Joined: Tue Jul 04, 2017 8:23 am

Re: How To Disable Js window.close and let's everthing stay OK?

Post by crystalxp »

Cool, I have tested it ,and everyone now ok as it is . Thx again for your works
wolf
Posts: 22
Joined: Thu Jan 31, 2019 6:36 pm

Re: How To Disable Js window.close and let's everthing stay OK?

Post by wolf »

Sorry to bring up an old topic.

What's the best way to handle this situation now with Chromium 128+ and the "Chrome style" since OnClose is no longer called.

Is there another way that works similar to OnClose and cbaCancel that we can use now with Chromium 128+?

Or is setting TChromium.Options.JavascriptCloseWindows to STATE_DISABLED the only solution or work around?
User avatar
salvadordf
Posts: 4620
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: How To Disable Js window.close and let's everthing stay OK?

Post by salvadordf »

Hi,

Try setting TChromium.Options.JavascriptCloseWindows to STATE_DISABLED in the parent before the TChromium.CreateBrowser call.

If that doesn't work then try this JavaScript solution :
https://community.plumsail.com/t/any-way-to-prevent-user-from-closing-tab-or-showing-a-warning-before-they-close-it-from-within-forms/10261/7
User avatar
salvadordf
Posts: 4620
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: How To Disable Js window.close and let's everthing stay OK?

Post by salvadordf »

Use the "settings" argument in the TChromium.OnBeforePopup event and set settings.javascript_close_windows to STATE_DISABLED
wolf
Posts: 22
Joined: Thu Jan 31, 2019 6:36 pm

Re: How To Disable Js window.close and let's everthing stay OK?

Post by wolf »

Thanks for your answer.

Setting TChromium.Options.JavascriptCloseWindows to STATE_DISABLED doesn't seem to work in all cases, seems that there are some cases where this option is without effect. I am not able to reproduce it with specific steps, but it happens that browser windows occasionally close.

TChromium.OnBeforePopup isn't called, so using the "settings" argument doesn't help.

I just tried it with MiniBrowser and opening a local html file with this html source:
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script>
<!--
function close_window() {
if (confirm("Close Window?")) {
close();
}
}
-->
</script>
</head>
<body>
<a href="#" onClick="close_window();return false;">Close</a><br>
<br>
<a href="javascript:close_window();">Close2</a><br>
</body>
</html>
This will close MiniBrowser. TChromium.Options.JavascriptCloseWindows is not set in my test as I'm looking for a better and more reliable approach.
User avatar
salvadordf
Posts: 4620
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: How To Disable Js window.close and let's everthing stay OK?

Post by salvadordf »

Implement the TChromium.OnBeforeUnloadDialog event in MiniBrowser to cancel the window close like this :

Code: Select all

procedure TMiniBrowserFrm.Chromium1BeforeUnloadDialog(Sender: TObject;
  const browser: ICefBrowser; const messageText: ustring; isReload: Boolean;
  const callback: ICefJsDialogCallback; out Result: Boolean);
begin
  Result := True;
  callback.cont(False, '');
end;
Then load the HTML file you provided and add an event listener for beforeunload using the DevTools like this :

Code: Select all

window.addEventListener('beforeunload', function (e) {
    e.preventDefault();
    e.returnValue = '';
});
After that click on the "Close" link and select OK.

The OnBeforeUnloadDialog event will prevent the window from closing.
Post Reply