Question

I am writing a bootstrap Firefox addon and need to register a new protocol/schema handler (e.g. foo:somthing). I have looked all over and I only see ways to do this using chrome.manifest, which bootstrapped add-ons cannot use.

So, does anyone know a way or is it possible to register a custom protocol handler in a bootstrapped add-on?

Was it helpful?

Solution 2

Although @nmair's answer is a good general thing I'll keep in mind, I was able to find a better solution to my own problem. I noticed that there was an HTML5 method that would try to ask the user to register a handler for a protocol/schema, and after picking around in omni.ja (firefox source code), I found it's definition. After fiddling around, I wrote this:

var handler = Cc["@mozilla.org/uriloader/web-handler-app;1"]
    .createInstance(Ci.nsIWebHandlerApp);
handler.name='My Protocol';
handler.uriTemplate='chrome://myprotocol/content/launcher.xul#%s';

var eps=Cc["@mozilla.org/uriloader/external-protocol-service;1"].
    getService(Ci.nsIExternalProtocolService);
var handlerInfo=eps.getProtocolHandlerInfo('myprotocol');
handlerInfo.possibleApplicationHandlers.appendElement(handler, false);
handlerInfo.alwaysAskBeforeHandling=false;        // don't ask the user
handlerInfo.preferredApplicationHandler=handler;  // set my handler as default
hi=handlerInfo;

var hs=Cc["@mozilla.org/uriloader/handler-service;1"].
getService(Ci.nsIHandlerService);
hs.store(handlerInfo);

Registers the protocol, no restart or components required.

OTHER TIPS

Yes, but you'll have to do the work that the add-on/component manager would do for you, in particular calling .registerFactory yourself.

There is already a test demonstrating how to register components in general, and protocol handlers in particular, at runtime yourself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top