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' 
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.