I made an app years ago that did this. The trick was to first detect the URL that the user clicked on, then open in a new window vs load in the existing one. Looking at the old code, I’m not sure if it’ll help you or not — but I copied it into a Gist, below.
Hopefully it’ll start you off in at least knowing what else to possibly search for. Good luck!