I wanted to practice my Javascript and I liked the idea of Ubiquity, so I decided to write some search commands for it. Ubiquity already has a utility function to create them, makeSearchCommand, but it’s too limiting. For example, if I do not provide a search query argument, it still takes me to the results page, when in fact it should take me to the home page of the site I’m searching, like Open Search Plugins do. A kind person in #ubiquity in the Mozilla IRC servers filed a ticket saying that makeSearchCommand should allow this behavior. In the mean time, I created some utility functions which would make it simple to create a search command that would do this, but for this I had to use CmdUtils.CreateCommand. But since I wanted to create many of these search commands, I came up with a way to make the creation of these commands a simple function call.
Aside from this, I wanted to make it so that anyone could easily add their own search commands by only providing three necessary pieces of information: the site’s readable name, the command that should be used for it, and the template search URL with {QUERY} as a place holder for the actual query, to mimic makeSearchCommand’s functionality in this aspect. This information is stored in an array of JSON objects which are iterated through and a search command created for each. I also made it possible to automatically find the site’s favicon to use as the command’s icon which adds a nice little touch.
I’m actually really new to Javascript, this is the first thing I write in Javascript from scratch (Other things I’ve done are just little changes to existing scripts), so I am pretty sure it can be done better, but for a first timer I believe I came up with a nice little way to create search commands.
Source
The source is available as a gist on Github. This allows for simple version controlling without providing a completely dedicated repository which would be overkill. The included sites are unnecessary as there are already Ubiquity search commands for them pre-installed when you install Ubiquity. They are simply there to provide you an example and sense of structure.
Installation
First of all, you will of course want to have the Ubiquity extension installed for Firefox. Then you should go to the Command Editor (Right click on the link, copy link location, then paste it into the address bar in a new tab). Once there, paste the script source into the big text box, and change/add/remove any of the sites you want, it should be pretty straightforward. You can test the script on the fly without having to save it or anything by triggering Ubiquity and typing the command names. If the command doesn’t show up or work, it is most likely because you ‘broke’ it, most likely a syntax error (Don’t forget the commas). You can check for any errors by going to Tools > Error Console in the Firefox menu. Once you’re done, you should click on the button on the top right that says Save to File, and it will save the script and subscribe you to it.
Usage
Simply trigger Ubiquity with your shortcut (Option+Space on the Mac, by default), and then type the command you specified. It might not show an icon right away because it is being downloaded. After a few uses though, the icon should show up.
Using the search commands is easy. You can type in your command followed by a search query, that is the default behavior. If you type the command and execute it without a search query, it takes you directly to the site. If you have text highlighted/selected, and you execute the command, it will use the selected text as the search query. Finally, if you have text selected but you do not want to use the selected text as the search query, you can type the command followed by go to go directly to the site. By “directly to the site”, I mean that if your search command is for google for example, it will take you to http://www.google.com (The root of the site), just like Open Search Plugins do.
Removal
To ‘unsubscribe’ from the script, simply go to the Ubiquity Main Page and on the bottom right you will see a column under a header that says Subscribed Feeds. Find your script there and simply click Unsubscribe.