How to get the ids of all open popups? (to close them all)

I did find a relevant post I stumbled on in the past (hint: use a message handler).

Scroll up a few posts to find a version of your 'dodgy solution' :grinning_face_with_smiling_eyes:

The above aside, I do wish there was a built-in to retrieve information about all open popups (and their z-order) of a session.