Changes

Jump to: navigation, search

Labs/Ubiquity/Ubiquity 0.1 Author Tutorial

3,252 bytes added, 01:43, 13 January 2009
Adding Commands Programmatically
You should enable Chrome Errors and Warnings if you want the errors in your code to appear in the Firebug console. Use CmdUtils.log() rather than console.log() ''Note: For now, you can only pass one argument to log()''
 
 
== Adding Commands Programmatically ==
 
Here is a snippet of code that shows how a developer can programmatically register a commanded included in a Firefox extension.
 
<pre>
 
// Helper function used to determine the local directory where the
// extension which contains the command is installed. This is used
// to create the URL of the js file which includes the implementation
// of the list of commands you want to add.
function getExtensionDir() {
var extMgr = Components.classes["@mozilla.org/extensions/manager;1"]
.getService(Components.interfaces.nsIExtensionManager);
return extMgr.getInstallLocation( "feedly@devhd" ).getItemLocation( "feedly@devhd" );
}
 
function getBaseUri() {
var ioSvc = Components.classes["@mozilla.org/network/io-service;1"]
.getService(Components.interfaces.nsIIOService);
var extDir = getExtensionDir();
var baseUri = ioSvc.newFileURI(extDir).spec;
return baseUri;
}
 
// your extension needs to call the addUbiquity command each time a new browser
// session is create and your extension is loaded.
function addUbiquityCommands(){
// url of the file which contains the implementation of the commands
// we want to add.
var url = getBaseUri() + "content/app/ubiquity/commands.js"
// link to the ubiquity setup module.
var jsm = {};
Components.utils.import("resource://ubiquity/modules/setup.js", jsm);
// look up the feed manager and add a new command. Note: we are using the
// isBuiltIn=true option so that the command is only added for the duration
// of the browser session. This simplifies the overall lifecycle: if the
// extension is disabled or un-installed, it will be automatically
// "removed" from ubiquity.
jsm.UbiquitySetup.createServices().feedManager.addSubscribedFeed( {
url: url,
sourceUrl: url,
canAutoUpdate: true,
isBuiltIn: true
});
}
</pre>
 
Inside your command implementation, you can use importing modules or looking up a singleton XPCOM component to link your command back to the functionality encapsulated by your extension. Here is a sample command which does that:
 
<pre>
var StreetsUtils = function(){
var that = {};
that.lookupCore = function(){
return Components.classes["@devhd.com/feedly-boot;1"]
.getService(Components.interfaces.nsIFeedlyBoot)
.wrappedJSObject
.lookupCore();
};
return that;
}();
 
CmdUtils.CreateCommand({
name: "my-extension-test",
takes: {"message": noun_arb_text},
icon: "chrome://my-extension-test/skin/icon-16x16.png",
modifiers: {to: noun_type_contact},
description:"Testing the feedly+ubiquity integration",
help:"This is a test help message",
preview: function(pblock, directObj, modifiers) {
var html = "Testing my-extension-test ";
if (modifiers.to) {
html += "to " + modifiers.to.text + " ";
}
if (directObj.html) {
html += "with these contents:" + directObj.html;
} else {
html += "with a link to the current page.";
}
pblock.innerHTML = html;
},
 
execute: function(directObj, headers) {
CmdUtils.log( ">>> my-extension core? " + ( StreetsUtils.lookupCore() != null ) );
}
});
</pre>
= Conclusion =
3
edits

Navigation menu