1

From MDN:

Dialog boxes are modal windows — they prevent the user from accessing the rest of the program's interface until the dialog box is closed.

I tried to capture the event when a dialog box appear on the screen using window.confirm but it seems it doesn't exist on the window level.

My question: Is there any way to mimic the click when a dialog box appears on screen?

The above code listen and print all the events made on the page, but when I click on the button nothing printed, and when I click "ok" or "cancel" it print "FocusEvent" - I guess that this is the focus on the clicked button.

document.querySelectorAll('button')[0].addEventListener('click', function(){
    window.confirm('confirmation box','click');    
});


for (var key in window) {
    if (key.search('on') === 0) {
        document.querySelectorAll('ol')[0].innerHTML += '<li>'+key.slice(2)+'</li>';
        window.addEventListener(key.slice(2), function() {
            document.querySelectorAll('ol')[1].innerHTML += '<li>'+key.slice(2)+'-'+this.event+'</li>';
            document.querySelectorAll('ol')[1].scrollTop = document.querySelectorAll('ol')[1].scrollHeight;
        });
    };
};
<button>open dialog</button>
<ol style="display: none;"></ol>
<ol style="height: 200px; overflow-y: scroll"></ol>

This confirm box seems on higher level from the window(??)

I did search a lot online but the best (and only) clues lead to 404 pages:

Is it possible?

0stone0
  • 34,288
  • 4
  • 39
  • 64
A. Meshu
  • 4,053
  • 2
  • 20
  • 34
  • 1
    There is no event handler for “confirm box is shown” or anything like that defined by any standard. If this is about detecting confirm calls by other scripts(?), then I guess you could try and overwrite `window.confirm` with your own function, that fires a custom event or does whatever else you need to do, and then calls the “original”. (You would need to preserve the reference to the original in a variable.) – CBroe Dec 03 '20 at 13:22
  • 1
    @CBroe thank you for comment. But where this "event" happen? Is is possible to reach this level? PS I did find this: https://stackoverflow.com/questions/12747904/js-override-confirm that do what you suggested. – A. Meshu Dec 03 '20 at 13:31
  • You have to consider, that the browser can open windows, which are not accessible from JS, for security reasons (e.g. do you really want to share your screen / download that file?). If `confirm`, for whatever reason, uses the same interface, then you'll have no access to it. They'll also be most likely from a different language, e.g. c++ in the case of chrome/v8. – ASDFGerte Dec 03 '20 at 13:33
  • 1
    I don’t know what “event” you are referring to here; as I said, there _is_ no event for “confirm is shown” or anything like that. Sounds a lot like a [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) right now; a proper description of what you actually need to _achieve_ with this in the end, would be good at this point. – CBroe Dec 03 '20 at 13:35
  • @ASDFGerte i see. So actually you CAN'T modify it but you CAN override it? Can't understand this logic.. – A. Meshu Dec 03 '20 at 13:36
  • @CBroe seems you are absolutely right with that XY. Back to the drawing table... Thank you for your time. – A. Meshu Dec 03 '20 at 13:40
  • I am not saying, that `confirm` does use something like that, i don't really know, but i see a real possibility for it. If you think about e.g. the "do you really want to share your screen" question, there is no reason to prevent JS from overwriting the creation of it. All that's needed to be prevented, is the script maliciously interacting with the open prompt (tricking the user into accepting something he/she does not want, giving the website privileges that it should not have). – ASDFGerte Dec 03 '20 at 13:44
  • I'm aware about the security concerns if this kind of operation can be done. Thank again for your effort. – A. Meshu Dec 03 '20 at 13:45
  • _“seems you are absolutely right with that XY. Back to the drawing table...”_ - so what actual problem are you trying to solve here then? Do you need to prevent the confirm window from appearing, do you need to trigger other stuff before/after it from an outside context, …? – CBroe Dec 03 '20 at 13:50
  • I tried to mimic the click event on those native dialogs - but it turn out that only the user can do that. So as you suggested i'm gonna override this dialog box and use my custom code. – A. Meshu Dec 03 '20 at 14:06
  • If you post it as an answer i will accept it. Thanks again. – A. Meshu Dec 03 '20 at 14:07
  • You probably want to search Google for "window.confirm callback". In any case, you will probably find most devs use custom dialogs instead; isolated from the browser window. – Kalnode Dec 03 '20 at 16:26

0 Answers0