0
votes

I want to register a custom file protocol in Chrome or Firefox, much like the way Electron does (please see their API for doing this: https://www.electronjs.org/docs/api/protocol). I am wondering if there is a way to implement this as a browser extension, or I have to modify the source code of the browsers.

I expect that the API would look like this (just a pseudocode to help explain what I mean):

registerHandler('myprotocol://', req => {
    response('<body>You requested: ' + req.url);
});

Clarification: navigator.registerProtocolHandler is NOT what I need. What it does is to register a protocol that, when clicked, opens an external application to deal with that. But what I want is a protocol handler scheme that works in a request--response way, e.g. can be used in JS/CSS/HTML queries and responds with a content that can be rendered within the browser.

1
Extensions don't have an API for that. The only thing possible is to hook XHR/fetch and some other DOM API prototypes in page context to handle the URLs. - wOxxOm
@wOxxOm I see, thank you. You may post it as an answer so I can accept it. - GZZ

1 Answers

0
votes

I would answer my own question because I found exactly what I need here: https://github.com/mozilla/libdweb

This is an experimental feature of Firefox Nightly that allows one to register a custom protocol and serve all requests to that protocol using firefox addon. This is not a WebExtension standard nor does it work on browser other than Nightly, but I'm glad to hear that someone is doing this.