Well, I've covered myself with a few lines of Rust that do exactly that.
By the way, there are a few subtleties which your script doesn't handle which bit me ;) Off the top of my head, some full-screen windows (mpv IIRC) behave a bit differently and would be missed.
and then trigger it with eg.:
``` bindsym $mod+x exec --no-startup-id "xargs -n1 kill -USR2 < /tmp/focus_last.pid" ```