How to add a confirmation prompt window to warn before closing a window for any application?
You clearly know what you want, and that is great, but you might have a hard time convincing other people to make it for you to your exact specifications.

Safari on macOS does not have any scripting hooks you can use to prevent closing any Safari window with the red button, so it won't be trivial to make Safari do what you want.

One possible avenue to consider is writing a webpage that intercepts the window close event to install a custom behaviour. This might be satisfactory, depending on exactly what problem you're trying to solve.

If you're not totally set on using Safari, you could try developing an extension for Firefox that implements the behaviour you want. I don't actually know for sure if that is possible.

You could try visiting the Mozilla Forums and submitting a feature request to have your feature added to Firefox core, but it is extremely unlikely that anybody will listen and add your feature.

If you want this badly enough that you're willing to pay money for it, you could try putting out an offer on a marketplace for freelance programmers (like Fiverr) and maybe somebody can help you create this feature that is very important to you.