advertisement

Print

What Is Greasemonkey
Pages: 1, 2, 3, 4

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
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
    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!

Mark Pilgrim is an accessibility architect who can be found stirring up trouble at diveintomark.org.


Return to the O'Reilly Network