Skip to content

Instantly share code, notes, and snippets.

@yajd
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yajd/e935341d7d1ebf90e280 to your computer and use it in GitHub Desktop.
Save yajd/e935341d7d1ebf90e280 to your computer and use it in GitHub Desktop.
nsIProtocolHandler
var {classes: Cc, interfaces: Ci, utils: Cu, results: Cr, manager: Cm} = Components; //can make this const if not in scratchpad
Cm.QueryInterface(Ci.nsIComponentRegistrar);
Cu.import('resource://gre/modules/Services.jsm');
Cu.import('resource://gre/modules/XPCOMUtils.jsm');
var nsIProtocolHandler = Ci.nsIProtocolHandler; //const
var unloaders = [];
function makeProtocolHandler(aProtocol, aClassID, aDefaultPort) {
var obj = {
classID: Components.ID(aClassID),
classDescription: 'blah blah blah',
contractID: '@mozilla.org/network/protocol;1?name=' + aProtocol,
QueryInterface: XPCOMUtils.generateQI([nsIProtocolHandler]),
scheme: aProtocol,
defaultPort: aDefaultPort,
protocolFlags: nsIProtocolHandler.URI_NORELATIVE | nsIProtocolHandler.URI_NOAUTH | nsIProtocolHandler.URI_LOADABLE_BY_ANYONE, //You must specify either URI_LOADABLE_BY_ANYONE, URI_DANGEROUS_TO_LOAD, URI_IS_UI_RESOURCE, or URI_IS_LOCAL_FILE in order for your protocol to work.
newURI: function(aSpec, aOriginCharset, aBaseURI) {
var uri = Cc['@mozilla.org/network/simple-uri;1'].createInstance(Ci.nsIURI);
uri.spec = aSpec;
console.log('uri=', uri);
return uri;
},
newChannel: function(aURI) {
//throw Cr.NS_ERROR_NOT_IMPLEMENTED;
/* Get twitterName from URL */
var postProtocolPath = aURI.spec.split(":")[1];
var uri = Services.io.newURI("http://twitter.com/" + postProtocolPath, null, null);
var channel = Services.io.newChannelFromURI(uri); //, null).QueryInterface(Ci.nsIHttpChannel); //i dont think i need to QI nsIHttpChannel
/* Determines whether the URL bar changes to the URL */
//channel.setRequestHeader("X-Moz-Is-Feed", "1", false);
channel.originalURI = aURI;
return channel;
},
getURIFlags: function(aURI) 0 // i dont think i need this? do I?
};
if (aDefaultPort == undefined || aDefaultPort == null) {
aDefaultPort = -1;
} else {
obj.allowPort = function(port, scheme) {
return port == aDefaultPort;
};
}
return obj;
}
var myComponents = [myCustomBlah];
function myCustomBlah() {}
myCustomBlah.prototype = makeProtocolHandler('mycustomblah', 'b14c2b67-8680-4c11-8d63-9403c7d4f757');
//const NSGetFactory = XPCOMUtils.generateNSGetFactory(components);
function registerComponents() {
for (let [y, cls] in Iterator(myComponents)) {
console.info('y: ', y, 'cls: ', cls);
try {
var factory = {
_cls: cls,
createInstance: function(outer, iid) {
if (outer) {
throw Cr.NS_ERROR_NO_AGGREGATION;
}
return new cls();
}
};
Cm.registerFactory(cls.prototype.classID, cls.prototype.classDescription, cls.prototype.contractID, factory);
unloaders.push(function() {
Cm.unregisterFactory(factory._cls.prototype.classID, factory);
});
} catch (ex) {
console.warn('failed to register module: ', cls.name, 'exception thrown: ', ex);
}
}
}
function unregisterComponents() {
for (var i = 0; i < unloaders.length; i++) {
unloaders[i]();
}
}
registerComponents(); //run this to make it work. once this is run follwoing examples above: typing "about:yabba" will take you to bings homepage
//unregisterComponents(); //do this to remove it //after running this typing about:yabba will take you to problem loading page
@yajd
Copy link
Author

yajd commented Jul 23, 2014

README

Rev1

Rev2

  • Changed from smtp to mycustomblah but using the same class id

Rev3

  • Beautified

Rev4

  • Copy and paste, it works. If you type mycustomblah:rawr it will do a twitter lookup on user rawr.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment