Search code examples
google-chrome-extensionfirefox-addon

Register custom file protocols in Chrome/Firefox extension?


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.


Solution

  • 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.