The Anatomy of a User Script
Hopefully I've piqued your interest. But how does this all work? It's surprisingly simple. Let's crack open the Butler user script and see how it works.
Every user script has a section of metadata, which tells Greasemonkey about the script itself, where it came from, and when to run it. You can use this to provide users with information about your script, such as its name and a brief description of what it does. You can also provide a default configuration for where the script should run: one page, one site, or a selection of multiple sites.
The metadata section of the Butler user script looks like this:
// ==UserScript==
// @name Butler
// @namespace http://diveintomark.org/projects/butler/
// @description Link to competitors from Google search results
// @include http://*.google.*/*
// ==/UserScript==
There are five separate pieces of metadata here, wrapped in a set of Greasemonkey-specific comments. Let's take them in order, starting with the wrapper:
Wrapper
// ==UserScript==
//
// ==/UserScript==
These comments are significant and must match this pattern exactly. Greasemonkey uses them to signal the start and end of a user script's metadata section. This section can be defined anywhere in your script, but it's usually near the top.
You can specify the items of your user script metadata in any order. I like @name, @namespace, @description, @include, and finally @exclude, but there is nothing special about this order.
Name
// @name Butler
This is the name of your user script. It is displayed in the install dialog when you first install the script and later in the Manage User Scripts dialog. It should be short and to the point.
@name is optional. If present, it can appear only once. If not
present, the default is the filename of the user script, minus the .user.js
extension.
Namespace
// @namespace http://diveintomark.org/projects/butler/
This is a URL, which Greasemonkey uses to distinguish user scripts that have the same name but are written by different authors. If you have a domain name, you can use it (or a subdirectory) as your namespace.
@namespace is optional. If present, it can appear only once. If not present, the default is the domain from which the user downloaded the user script.
Description
// @description Link to competitors from Google search results
This is a human-readable description of what the user script does. It is displayed in the install dialog when you first install the script and later in the Manage User Scripts dialog. It should be no longer than two sentences.
@description is optional. If present, it can appear only once. If not present, the default is an empty string.
Though @description is not mandatory, don't forget to include it. Even if you are writing user scripts only for yourself, you will eventually end up with dozens of them, and administering them all in the Manage User Scripts dialog will be much more difficult without descriptions.
Include
The @include directive lists the URLs and wildcards that tell Greasemonkey where to run this user script:
// @include http://*.google.*/*
There is also an @exclude directive, which shares the same syntax. The @include and @exclude directive can each be a URL; a URL with the * character as a simple wildcard for part of the domain name or path; or simply the * wildcard character by itself. In this case, we are telling Greasemonkey to execute the Butler script on all Google sites in all countries, all subdomains (such as http://froogle.google.com/), and on all pages within any of those Google properties. Everywhere Google is, we want to be there.
@include and @exclude are optional. You can specify as many included and excluded URLs as you like, but you must specify each on its own line. If neither is specified, Greasemonkey will execute your user script on all sites (as if you had specified @include *).
Coding a User Script
User scripts are written in JavaScript. On every page you visit, Greasemonkey looks through the list of installed user scripts, determines which ones apply to this page (based on the @include and @exclude directives), and executes them after the page is loaded but before it is rendered. The scripts themselves can do anything you like. Butler works on a number of different Google pages, and does different things to each type of page, so it contains some code to check where exactly we are and calls the appropriate methods.
For example, on Google web search results pages, Butler adds the "Enhanced by Butler" banner along the top, removes the ads along the top and right sides of the results, adds the "try your search on..." line, and may also add other "try your search on..." lines for inline movie reviews, news headlines, weather forecasts, and product results. The code to do this is straightforward:
var href = window.location.href;
// Google web search
if (href.match(/^http:\/\/www\.google\.[\w\.]+\/search/i)) {
Butler.addLogo();
Butler.removeSponsoredLinks();
Butler.addOtherWebSearches();
Butler.addOtherInlineMovieReviews();
Butler.addOtherInlineNewsResults();
Butler.addOtherInlineForecasts();
Butler.addOtherInlineProductSearches();
}
Each of these functions is defined elsewhere in the Butler user script. (Greasemonkey user scripts are always self-contained. If you need to bundle multiple interdependent scripts, you're probably better off writing a browser extension.)
Managing User Scripts
The Butler user script barely scratches the surface of what Greasemonkey can do. There are literally thousands of user scripts, some targeting a single page or a single site, others that work on every page. Because you can have multiple user scripts installed, Greasemonkey provides a graphical interface for managing them.
From the Firefox Tools menu, select "Manage User Scripts" (See Figure 6). Here you can see all the user scripts you have installed, change their configuration, disable them temporarily, or uninstall them completely. You can even edit a user script "live" and see your changes immediately, without restarting Firefox. This is enormously helpful while you're developing your own user scripts.

Figure 6: Manage User Scripts
Finding User Scripts
Many user scripts are available at the Greasemonkey script repository. Here are some of my favorites:
- Book Burro adds a floating window to book retailers like Amazon.com and Barnes & Noble. While you're browsing for a book, you can get price comparisons from other retailers--without leaving the page! (See Figure 7.)

Figure 7
- Nice
Titles changes link tooltips from boring yellow to a sleek translucent
window instead.
- Recent Searches tracks your Google searches--both what you searched for and where you ended up--and displays them on the Google home page and search results pages.
- Google SearchKeys adds keyboard shortcuts to Google search results, so you can press "1" to go to the first search result link, "2" to go to the second, and so on.
- Magic Line remembers everything you read and lets you find anything later by pressing Ctrl + Shift + L and autocompleting your search based on page titles, URLs, keywords, categories, page excerpts, and specially marked links. It's like your own personal command line for the web.
Have fun remixing the web!

|