You can do this by way of an Automator service, which will add a contextual (right-click) menu item when you click on a link in Safari (or Chrome).
Read the Mac Automation Scripting Guide: Making a Systemwide Service, which has a step-by-step guide on how to create an Automator service.
It will look something like this during construction:

This service, as you can see, is active only in Safari when there is a block of text selected. Unfortunately, you can't limit it to just hyperlinks, but it does include them.
If the service is available (i.e. you are in Safari and have selected some text), the menu item corresponding to the service appears in the right-click submenu called "Services", and it'll be named according to what you saved the workflow as in Automator. In my case, the service is named "Process URL".
When clicked, it executes the AppleScript you can see in the workflow (it does this in the background, invisibly).
This is where your suggestion of using JavaScript was a good one to employ: the AppleScript executes a bit of JavaScript in Safari (provided you have the right permissions enabled for it to do so, ticked in Safari's "Develop" menu).
This JavaScript returns one of two results:
▸ If the text selected is plain text with no hyperlink underneath it, it returns an empty string;
▸ If the text selected is hyperlink text, it returns the hyperlinked URL.
It stores this in the variable href
, which you can then do whatever you want with.
The good thing about hyperlinks is that you don't have to click and drag to select the text (although you can if you wish, and only part of the text needs to be selected). Instead, you can just right-click on a link, and it automatically selects it for you, making it ideal to activate the service is this manner.