I have been following the Ubiquity project from Mozilla Labs, and I gotta say, it’s pretty rad. If you are a Javascript Ninja or an aspiring one, Ubiquity can make your web surfing super slick.
We use an internal wiki here at the office, using the Dokuwiki framework. I personally look in the wiki for something at least 5 times a day. Usually, this involves:
- Opening a new browser tab
- Go to our Intranet page
- Click on the wiki link
- Search the wiki (let’s say for “configuration”)
- Look through the list of search hits
- Maybe repeat step 4
As I was looking at the Ubiquity tutorials, it became apparent that with the right Ubiquity command, the whole workflow could be simplified to:
- Control-Space
- Type “dw configuration”
- Review search hits
- Maybe repeat step 2
Ok, so not a profound difference in the number of steps, but I can do all of these steps without letting my hands leave the keyboard. Some Firefox purists out there may argue that I could do the same with the first set of steps, but there are fewer Alts and Controls in the second set — anyway, this is just an experiment, so lighten up, ok?
I won’t paste the whole code here, but I wanted to point out a few neat tricks that are part of the command:
- XML RPC
A transport protocol for web services, XML Remote Procedure Calls let you invoke a web service with some arguments, and get a result back. Dokuwiki has an XML-RPC interface, where one can perform basic searching and editing functionality. Note: this interface is disabled on the main Dokuwiki site, and is something a system administrator needs to manually enable on an installation of Dokuwiki.In this chunk of code, I set up the xmlrpc url, and create an xml blob that will contain the remote command that I want to execute (wiki.getAllPages). This will return a list of all the pages in the Dokuwiki instance.
... // the command preview preview: function(pblock, args) { if ( args.object.text != '' ) { // set the url for the xmlrpc command var url = 'http://www.dokuwiki.org/lib/exe/xmlrpc.php'; // set up the data to POST var data = '<?xml version="1.0"?><methodCall><methodName>' + 'wiki.getAllPages</methodName></methodCall>'; // provide some feedback to the user pblock.innerHTML = 'Searching for page matches for ' + args.object.text + '...'; // hold on to a reference to the currently-scoped object var me = this; jQuery.ajax( { async: true, contentType: 'text/xml', data: data, dataType: 'xml', url: url, type: 'POST', success: function(x) { ... } }); } }, ... - jQuery
Ubiquity has a version of jQuery built into it, so you have the full power of jQuery at your disposal. I used this to do some ajax calls to the XML RPC interface of Dokuwiki.You can see this in action in the above code excerpt.
- XPath
No mention of XML would be complete without some way to navigate your XML document using XPath. To get the search results and other information, I used some of Firefox’s built in objects to extract the relevant search hits.The object returned from the success method above is an xml document (since I specified the dataType in the jQuery.ajax call). With an xml document, one can perform XPath tricks with the evaluate() method. This code selects the “name” elements where the page id is the same as the command argument (in this case “configuration”).
// get the pages with IDs that match the input string var pages = x.evaluate( '//struct/member/name[text()="id"]' + '/../value/string[contains(text(),"' + args.object.text + '")]', x, null, XPathResult.ANY_TYPE, null ); var nuggets = []; // return the top 10 results, if multiple matches are found var item = pages.iterateNext(); while ( item && nuggets.length < 10 ) { nuggets.push( item.textContent ); item = pages.iterateNext(); } // if there are many hits, show a list of matches if ( nuggets.length > 1 ) { CmdUtils.previewList( pblock, nuggets, function(){ me.gotoUrl(nuggets[arguments[0]]); }); } // if there is one hit, generate an html preview in the // ubiquity preview frame else { pblock.innerHTML = 'Found match, generating preview...'; ...
That was my first stab at it, and it has a couple quirks, but I find myself using it to find information really quickly. I think there is a great amount of potential here, especially with the collection of commands that are already built in to Ubiquity.
For the fearless, the full command is available for download here: dokuwiki search.






4 Comments
Interesting approach, but for your needs there might be a simpler way: OpenSearch. You can learn more about it at http://www.dokuwiki.org/search#opensearch
This is a great post on Ubiquity! For those who want to capitalize on built-in Firefox functionalities, you can actually add any DocuWiki as a search engine on your Firefox search bar!
1. Open your wiki, and search for anything.
2. Click to expand the list of search engines (just to the right of the textbox), and click on ‘Add “Your site”‘.
3. Then, you can just search there directly.
Note: This actually applies to pretty much any ‘search results’ style page. If Firefox doesn’t automatically detect your search engine while you’re there, you can use their plugin finder here: https://addons.mozilla.org/en-US/firefox/browse/type:4/cat:all?sort=name
Andreas,
This exercise was designed to learn more about building Ubiquity commands and getting familiar with DokuWiki’s xml-rpc interface.
That said, OpenSearch is definitely cool, and totally simplifies this process. Indeed, you could do the same type of thing with keywords in bookmarks and search engines, too.
Thanks for the tip!
David,
It looks like you and Andreas are on the same wavelength! OpenSearch makes my life easier — especially when I’m trolling archives for the cause of some obscure bug.