How to intercept modal dialogs?

We want to intercept all modal dialogs (alert(), confirm(), etc.) from a given tab. The add-on code will then close or suppress them. Do you know something that makes this possible? Thanks.

Without digging in, you can tap into the XUL and attach a mutation observer to the overlay that gets shown. Each tab is a notificationbox element. If you inspect that, we see it has a stack element, which stacks a browser element and a tabmodalprompt. This second element is what gets toggled to visible when an alert etc happens.

That rectangle is around one tab. Now what would be better is to search dxr for tabmodalprompt and see if any custom events are triggered when it is shown etc, then you would listen to that. That would be the better approach then using mutation observers.