I'd like to write a simple WebExtension that handles certain types of links. I don't want a system wide protocol handler, it should simply work for websites open in my browser.
So for instance when anything causes a magnet:// link to be opened, I'd like to intercept that so that the WebExtension handles it.
And if possible I'd like to prevent system applications from handling it while the WebExtension is enabled.

At first I thought that I could use registerProtocolHandler:

navigator.registerProtocolHandler("magnet", "*%s", "Magnet handler"); 

But I don't think that this would do what I want it to...
My next idea was to use a click event on all a-elements:

document.getElementsByTagName('a').addEventListener('click', event => {
    let link = event.target.href
    if (link.startsWith('magnet://')) {
        // handle magnet link
        return false
    }
}

But that would only work for links that were clicked on. A link that is opened using JavaScript would not be affected, so that wouldn't work either..

share|improve this question

Work is underway here to support custom protocol handlers: https://bugzilla.mozilla.org/show_bug.cgi?id=1271553

share|improve this answer
    
registerProtocolHandler is already available and works just fine for mailto:, magnet:, irc: etc. But it's not what I'm looking for as mentioned in the question. registerProtocolHandler can only register a certain protocol to a website. – Forivin Jan 14 at 0:38
1  
But I believe the intention in that bug is to let extensions register handlers at moz-extension:// urls which would allow the handler to be contained within the extension. I thought that's what you were looking for? – Andrew Swan Jan 14 at 9:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.