If your requirement is to inform a user that they leave the website, you can add the "beforeunload" event listener into your main client library that will display the popup
(example here: https://dev.to/melvin2016/how-to-show-a-are-you-sure-to-leave-leave-site-etc-dialog-box-in-the-brows...)
But in my sight, it could be safer to have another special redirect page with that message, the same way it is, for example, on LinkedIn. And you can use that redirect page for all external links. (The resulting links will be something like /content/<project>/redirect.html?path=<originalUrl>)
You even can use something like LinkRewriter service to update all external links to this behavior, in order to cover all the components + custom links from RichText component. Or use rewrite on dispatcher side, if you have AEM on-premise. However, custom components that use javascript redirection should be covered in their (FE) code anyway, so maybe the first approach with JS event is better though.