Hi! Just to make sure, you have you look at this already: https://video2.skills-academy.com/en-us/windows-server/identity/ad-fs/operations/home-realm-discovery-customization there are built-in way to by pass the HRD in certain contexts.
I publish some custom modification back in the days, in case that helps too: https://video2.skills-academy.com/en-us/archive/blogs/pie/customize-the-home-realm-discovery-page-to-ask-for-upn-right-away.
If none of these helps, you can give us an example of what the complete URL look like and we can probably assist you with the JavaScript bit.