Not a user script, but the LibRedirect extension does most of that already
https://libredirect.manerakai.com/
I think it doesn't check whether the chosen instance is up before redirecting, but switching to another one if that one is down is a single click.