Chapter 4. Extending Google

Hacks 4762

Google is of the Web, but this doesn’t mean it’s trapped in your browser. Google has become so much a part of the fabric of our everyday lives that it shows up just about everywhere: Google via instant messaging [Hack #52], from a chat room [Hack #50], on your mobile phone [Hack #51]; you can even tweak your browser [Hack #53] to take Google with you to every page you visit.

This chapter is a tour of some of the more interesting ways Google has leapt out of the pages of cyberspace onto your desktop, and into what hackers affectionately call meat space: everyday life, to you and me.

Keep Tabs on Your Searches with Google Alerts

Receive alerts in your email Inbox when something you’re after makes its way into the Google Web index, a Google News story, or a post at Google Groups.

There are two classes of search that one generally runs in Google. One is of the sort that you generally run just once: you’re trying to find information on some topic, a phone number, or that URL you visited yesterday but have since forgotten.

Then there’s the search you’d run every day if you could. You’re interested in a particular subject matter and want to know the moment Google finds and indexes something new on the topic.

Google Alerts notifies you of any new web pages or news stories that match your search criteria.

Tip

Google’s Web index does not consider a page “new” based on the date it was created. Instead, it considers a page new based on the date it was found and indexed by the Googlebot.

Google Alerts (http://www.google.com/alerts) allows you to monitor Google’s Web index, Google News stories, and posts at Google Groups. To set up a Google Alert, visit the Google Alerts page. In the Create a Google Alert form (shown in Figure 4-1), type in a search query and choose whether to monitor news, the Web, both News & Web, or Groups.

Monitoring the Web, News stories, or Groups postings with Google Alerts
Figure 4-1. Monitoring the Web, News stories, or Groups postings with Google Alerts

Keep in mind that even though the form is small, you have the full range of Google special syntax at your disposal. For example, if you want to find news about Google Hacks, but not every story that mentions the words Google and hacks, enclose the query in quotes as you would a standard web query.

You have a choice when it comes to how often you’re notified: as it happens, once a day or once a week. Provide your email address and click the Create Alert button, and you’ll receive a confirmation email message a few moments later. Follow the link provided in the email message—thus confirming that your email address is legitimate and that it was you who requested the Google Alert—and you’re all set.

Tip

Be careful of the update frequency option: monitoring Google News’ 4,500 sources for even a slightly common word, phrase, or name and choosing to receive notification “as it happens” can fill your inbox with an avalanche of email.

Each alert you receive includes your search query, the found page’s title, a snippet of content, and the URL (for Web index results) or story title, description, and source (for News stories). You can set up to 50 alerts per email address.

While all you need to sign up for Google Alerts is a valid email address, you can also sign up for a more hands-on approach to managing your alerts. On the Google Alerts page, click the “sign in to manage your alerts” link, and you’ll find the Manage your Alerts page shown in Figure 4-2.

The Google Alerts management form
Figure 4-2. The Google Alerts management form

If you haven’t already, you’ll need to sign up for a free Google account. Membership has its privileges:

  • You’re provided with a nice overview of your active alerts.

  • If you don’t sign up to manage your Google Alerts, you can’t edit the Google Alerts that you create. All you can do is delete them and create new ones.

  • Google Alerts are delivered in HTML format as a default; by signing up, you can switch to text and back again.

In addition to monitoring Google for specific mentions of your business, your web site, or even your name, there are some other ways to use alerts to stay on top of the Web. Monitoring Google’s Web index allows you to find search engines or directories of information that you might have missed otherwise. For example, I keep tabs on Google to find pages that don’t tend to appear out of thin air all that often, such as those containing "online museum" or "online reference service".

I tend to use broader search queries when monitoring Google News. While watching the Google Web index for "online database" or "new search engine" might net me thousands of results—and those long after the sites were actually new—online news stories about new online databases and search engines tend to crop up less frequently and provide a higher signal-to-noise ratio.

Google Your Desktop

Google your desktop and the rest of your filesystem, mailbox, and instant messenger conversations—even your browser cache.

Not content just to help you find things on the Internet, Google takes on that teetering pile on your desktop—your computer’s desktop, that is.

The Google Desktop (http://desktop.google.com) is your own private little Google server. It sits in the background, slogging through your files and folders, indexing your incoming and outgoing email messages, listening in on your instant messenger chats, and browsing the Web right along with you. Just about anything you see and summarily forget, the Google Desktop sees and memorizes—it’s like a photographic memory for your computer.

And it operates in real time.

Beyond the initial sweep, that is. When you first install Google Desktop, it uses any idle time to meander your filesystem, email application, instant messages, and browser cache. Imbued with a sense of politeness, the indexer shouldn’t interfere at all with your use of your computer; it springs into action only when you step away, take a phone call, or doze off for 30 seconds or more. Pick up the mouse or touch the keyboard, and the Google Desktop scuttles off into the corner, waiting patiently for its next opportunity to look around.

Its initial inventory taken, the Google Desktop server sits back and waits for something of interest to come along. Send or receive an email message, strike up an AIM conversation with a friend, or get started on that PowerPoint presentation, and it’s noticed and indexed within seconds.

The full-text Google Desktop indexes:

  • Text files, Microsoft Office documents, and PDFs

  • Address Book entries and calendars

  • Email handled through most major email programs including Outlook, Outlook Express, and Thunderbird

  • Instant Messenger conversations

  • Web pages you visit

Additionally, any other files you have lying about—photographs, MP3s, movies—are indexed by their filename. So if the Google Desktop can’t tell a portrait of Uncle Alfred (uncle_alfred.jpg) from a song by Uncle Cracker (uncle_cracker__double_wide__who_s_your_uncle.mp3), it files both in a search for uncle.

And the point of all this is to make your computer searchable with the ease, speed, and familiar interface you’ve come to expect of Google. The Google Desktop has its own home page on your computer, shown in Figure 4-3, whether you’re online or not. Type in a search query as you would at Google proper and click the Search Desktop button to search your personal index. Or click Search the Web to send your query to Google.

The Google Desktop home page
Figure 4-3. The Google Desktop home page

But we’re getting a little ahead of ourselves here.

Let’s take a few steps back, download and install the Google Desktop, and work our way back to searching again.

Installing the Google Desktop

The Google Desktop is a Windows-only application, requiring Windows XP or Windows 2000 Service Pack 3 or later. The application itself is tiny, but it consumes about 500 MB of room on your hard drive and works best with 400 MHz of computing horsepower and 128 MB of memory.

Point your browser at http://desktop.google.com, download, and run the Google Desktop installer. It installs the application, embeds a little swirly icon in your taskbar, and drops a shortcut onto your desktop. When it’s finished installing and setting itself up, your default browser pops open and you’re asked to step through a few preferences, as shown in Figure 4-4.

Setting Google Desktop preferences
Figure 4-4. Setting Google Desktop preferences

As you step through the installation preferences, you’ll notice warnings about privacy, such as the one in Figure 4-5.

Advanced Features warning
Figure 4-5. Advanced Features warning

Google Desktop indexes just about everything on your machine, so it makes sense that Google is very careful about enabling features that communicate information back to the Google servers. If you’re trying Google Desktop for the first time, you might want to err on the side of caution and disable Advanced Features; you can always enable them later.

Warning

Know that if you enable the “search across computers” option, you’ll be sending the contents of your documents to Google’s Servers. If you don’t mind the idea of your files being posted to Google’s servers, you can conveniently search your home computer from your work computer, and from other computers you use. But keep in mind that all of that personal data moving around the Web could be intercepted by a third-party at some point. The computer rights group Electronic Frontier Foundation advises people not to use Google Desktop; you can find its reasoning at its web site (http://www.eff.org/news/archives/2006_02.php#004400).

Once you’ve gone through the wizard, Google Desktop starts its initial indexing sweep.

Searching Your Desktop

From here on out, whenever you look for something on your computer, rather than invoking Windows search and waiting impatiently while it grinds away (and you grind your teeth) and returns with nothing, double-click the swirly Google Desktop taskbar icon and Google for it. Don’t bother combing through an endless array of Inboxes, Outboxes, Sent Mail, and folders or wishing you could remember whether your AIM buddy suggested starving or feeding your cold. Click the swirl.

Figure 4-6 shows the results of a Google Desktop search for "google hacks". Notice that it found 35 email messages, 18 files, and 42 items matching my query in my web-browsing history. As you can probably guess from the icons to the left of each result, the first item is a text file, and the rest are from a web site, displaying the site’s icon and screenshot along with the result. These are sorted by date, but you can easily switch to relevance by clicking the “Sort by relevance” link at the top right of the results list.

Google Desktop search results
Figure 4-6. Google Desktop search results

Figures 4-7 and 4-8 show individual search results as I clicked through them. Note that each is displayed in a manner appropriate to the content.

Cached web pages are presented, as shown in Figure 4-7, in much the same manner as they are in the Google cache.

A cached web page
Figure 4-7. A cached web page

The various Reply, Reply to All, Forward, etc., links associated with an individual message result (Figure 4-8) work: click them, and the appropriate action is taken by your email program.

An email message
Figure 4-8. An email message

Google Desktop Search Syntax

It just wouldn’t be a Google search interface without special search syntax to go along with it.

A filetype: operator restricts searches to only a particular type of file: filetype:powerpoint or filetype:ppt (.ppt being the PowerPoint file extension) both find only Microsoft PowerPoint files, while filetype:word or filetype:doc (.doc being the Word file extension) both restrict results to Microsoft Word documents.

Searching the Web

Now you’d think I’d hardly need to cover Googling...and you’d be right. But there’s a little more to Googling via the Google Desktop than you might expect. Take a close look at the results of a Google search for "google hacks" shown in Figure 4-9.

Google Desktop Web Search results pack a little extra
Figure 4-9. Google Desktop Web Search results pack a little extra

Come on back when you’re through with that double take.

If you missed it, notice the new quick links [“Quick Links” in Chapter 1]: “157 results stored on your computer.”

Tip

When the Google Desktop is running, you can bring up a Google Quick Search box by pressing Ctrl twice. You can also disable this feature in your Google Desktop preference if you find yourself accidentally bringing up the form with a twitchy Ctrl finger.

Yes, those are the same results (and then some, given that my indexer was hard at work) returned in my earlier Google Desktop Search of my local machine. As an added reminder, they’re called out by that Google Desktop swirl. Click a local result and you end up in the same place as before: all 157 results, an HTML page, or Microsoft Word document. Click any other quick link or search result, and they’ll act in the manner you’d expect from any Google.com results.

Behind the Scenes

Now before you start worrying about the results of a local search—or indeed your local files—being sent off to Google, read on. What’s actually going on is that the local Google Desktop server intercepts any Google Web Searches, passes them to Google.com in your stead, and runs the same search against your computer’s local index. It then intercepts the Web Search results as they come back from Google, pastes in local finds, and presents it in your browser as a cohesive whole.

All work involving your local data is done on your computer. Neither your filenames nor your files themselves are ever sent to Google.com—as long as you don’t enable the “search across computers” option, which is disabled by default.

For more on Google Desktop and privacy, right-click the Google Desktop taskbar swirl, select About, and click the Privacy link.

Google Desktop Sidebar

Google Desktop includes a desktop sidebar that you can optionally install. The sidebar lives just to the right of your desktop, and you can view it by moving your mouse all the way to the right.

The sidebar keeps track of email, news, weather, and information on your computer from one location. Figure 4-10 shows some of the Google Desktop Sidebar modules, though they’re all stacked in a single column on your computer.

A slightly dissected view of the Google Desktop Sidebar
Figure 4-10. A slightly dissected view of the Google Desktop Sidebar

When you fire up the sidebar for the first time, the features are personalized for your preferences. Because Google Desktop knows which web pages you visit, you’ll find Web Clips based on your personal browsing history. The photos box shows you pictures on your hard drive, and the sidebar can make some good guesses about your location for displaying weather and maps.

Twiddling Knobs and Setting Preferences

There are various knobs to twiddle and preferences to set through the Google Desktop browser-based interface and taskbar swirl.

Set various preferences in the Google Desktop Preferences page. Click the Desktop Preferences link on the Google Desktop home page or any results page to bring up the settings shown in Figure 4-11.

Google Desktop Preferences
Figure 4-11. Google Desktop Preferences

Hide your local results when sharing Google Web Search results with a friend or colleague by clicking the Hide link next to any visible Google Desktop quick links. You can also turn Desktop quick link results on and off from the Google Desktop Preferences page.

Warning

Keep in mind that if you want to keep your search history private [Hack #11], the Google Desktop is another place that stores your browsing history—including Google searches you’ve performed. Uncheck the “Web history” option in Google Desktop preferences to keep your searches to yourself.

You can also include or exclude specific locations from your Google Desktop index. Just add a folder or web site to the form listed next to Don’t Search These Items, and Google’s indexer looks the other way. In addition, you can specifically add a folder to your search if the indexer seems to be missing an important folder.

From the preferences page, you can also enable/disable the Search Across Computers feature that makes your desktop searchable from other locations, and the Advanced Features feature that sends some nonidentifiable information about your Google Desktop usage back to Google’s servers. If you want to be “off the grid,” make sure both these features are disabled.

If you see something in your search results that you’d rather not see, click the “Remove results” link next to the Search Desktop button on the top-right of any results page, and you can go through and remove those items from Google Desktop index, as shown in Figure 4-12. Note that if you open or view any of these items again, they are once again indexed and will start showing up in search results.

Removing items from your Google Desktop index
Figure 4-12. Removing items from your Google Desktop index

Search, set preferences, check the status of your index, pause or resume indexing, quit Google Desktop, or browse the “About docs” by right-clicking the Google Desktop taskbar swirl and choosing an item from the menu, shown in Figure 4-13.

The Google Desktop taskbar menu
Figure 4-13. The Google Desktop taskbar menu

Extending Google Desktop

Google sees Google Desktop as more than an application that helps you organize and manage your information. By offering a software development kit (SDK) for Google Desktop, Google hopes that third-party developers will create their own applications that work with Google Desktop and the Sidebar to manage your information.

To take a look at the available extensions, go to the Google Desktop plug-ins page (http://desktop.google.com/plugins/) and browse through the directory. You’ll find new Sidebar modules, including one that tracks your AdSense revenue or provides real-time London subway information, and ways to extend Google Desktop, including a way to add Google Desktop to your Windows shell.

After evaluating the Google Desktop as an interface to find needles in my personal haystack, one thing still sticks in my mind: I stumbled across an old email message that I was sure I’d lost.

Google with Bookmarklets

Create interactive bookmarklets to perform Google functions from the comfort of your own browser.

You probably know what bookmarks are. But what are bookmarklets ? Bookmarklets are like bookmarks but with an extra bit of JavaScript magic added. This makes them more interactive than regular bookmarks; they can perform small functions such as opening a window, grabbing highlighted text from a web page, or submitting a query to a search engine. There are several bookmarklets that allow you to perform useful Google functions right from the comfort of your own browser.

Tip

If you’re using Internet Explorer for Windows, you’re in gravy: all these bookmarklets will most likely work as advertised. But if you’re using a less-appreciated browser (such as Opera) or operating system (such as Mac OS X), pay attention to the bookmarklet requirements and instructions; you might need special magic to get a particular bookmark working or, indeed, you might not be able to use the bookmarklet at all.

Google Bookmark (http://www.google.com/searchhistory/)

At the top of the Search History page at Google, you’ll find the option to add a Google Bookmark bookmarklet. Don’t let the mouthful of a title scare you away; this bookmarklet is a handy way to add starred items (a.k.a. Google Bookmarks) to your personalized Search History [Hack #12]. Click the bookmarklet, and a new windows pops up so you can adjust the bookmark title, notes, or labels before you click Save.

Google Translate! (http://www.microcontentnews.com/resources/translator.htm)

This puts Google’s translation tools into a bookmarklet, enabling one-button translation of the current web page.

Highlight Query Terms (http://www.nimbustier.net/publications/web/bookmarklet-google.html.en)

If you’ve ever performed a Google Search for a specific keyword, clicked on a result, and then wondered why that particular page was returned, this bookmarklet is for you. Click the bookmarklet after a Google Search, and all your query terms are highlighted in the page.

The Dooyoo Bookmarklets collection (http://dooyoo-uk.tripod.com/bookmarklets2.html)

This features several bookmarklets for use with different search engines—two for Google. Similar to Google’s Browser Buttons, one finds highlighted text and the other finds related pages.

Joe Maller’s Translation Bookmarklets (http://www.joemaller.com/translation_bookmarklets.shtml)

This translates the current page into the specified language via Google or AltaVista.

Bookmarklets for Opera (http://www.philburns.com/bookmarklets.html)

This includes a Google translation bookmarklet, a Google bookmarklet that restricts searches to the current domain, and a bookmarklet that searches Google Groups. As you might imagine, these bookmarklets were created for use with the Opera browser.

LuckyMarklets (http://www.researchbuzz.org/2004/01/happy_google_hacks_week_2004_3.shtml)

Tara’s bookmarklets take advantage of the I’m Feeling Lucky feature in Google Web Search, Google News, and Google Images.

Milly’s Bookmarklets (http://www.imilly.com/bm.htm)

This is an incredible collection of bookmarklets for all things Google: Web Search, Images, Directory, Definitions, Cache, the Google site itself, and many more, Google or otherwise.

If you find these bookmarks useful, you might want to try building your own bookmarklet for spotting blog commentary [Hack #43] or for adding feeds to Google Homepage or Google Reader [Hack #57].

Google from IRC

Performing Google searches from IRC is not only convenient, but also efficient. See how fast you can Google for something on IRC and click on the URL highlighted by your IRC client.

When someone pops into your IRC channel with a question, you can bet your life that 9 times out of 10, he could have easily found the answer on Google. If you think this is the case, you can tell him that, or you can do it slightly more subtly by suggesting a Google search term to an IRC bot, which then goes and looks for a result.

Most IRC clients can highlight URLs in channels. Clicking on a highlighted URL opens your default web browser and loads the page. For some people, this is a lot quicker than finding the icon to start their web browser and then typing or pasting the URL. More obviously, a single Google search will present its result to everybody in the channel.

The goal is to have an IRC bot, called GoogleBot, that responds to the !google command. It responds by showing the title and URL of the first Google search result. If the size of the page is known, this is also displayed.

The Code

First, unless you’ve already done so, you need to grab a copy of the Google Web APIs Developer’s Kit (http://www.google.com/apis/download.html), create a Google account, and obtain a license key [Chapter 8]. As I write this, the free-license key entitles you to 1,000 automated queries per day. This is more than enough for a single IRC channel.

The googleapi.jar file included in the kit contains the classes the bot uses to perform Google searches, so you need to make sure this is in your classpath when you compile and run the bot (the simplest way is to drop it into the same directory as the bot’s code itself).

GoogleBot is built on the PircBot Java IRC API (http://www.jibble.org/pircbot.php), a framework for writing IRC bots. You need to download a copy of the PircBot ZIP file, unzip it, and drop pircbot.jar into the current directory, along with the googleapi.jar.

Tip

For more on writing Java-based bots with the PircBot Java IRC API, be sure to check out “IRC with Java and PircBot” [Hack #35] in IRC Hacks by Paul Mutton (O’Reilly).

Create a file called GoogleBot.java:

import org.jibble.pircbot.*;
import com.google.soap.search.*;
     
public class GoogleBot extends PircBot {
    
    // Change this so it uses your license key!
    private static final String googleKey = "insert your api key";
    
    public GoogleBot(String name) {
        setName(name);
    }
    
    public void onMessage(String channel, String sender, String login,
            String hostname, String message) {
        
        message = message.toLowerCase().trim();
        if (message.startsWith("!google ")) {
            String searchTerms = message.substring(8);
            
            String result = null;
            try {
                GoogleSearch search = new GoogleSearch();
                search.setKey(googleKey);
                search.setQueryString(searchTerms);
                search.setMaxResults(1);
                GoogleSearchResult searchResult = search.doSearch();
                GoogleSearchResultElement[] elements =
                        searchResult.getResultElements();
                if (elements.length == 1) {
                    GoogleSearchResultElement element = elements[0];
                    // Remove all HTML tags from the title.
                    String title = element.getTitle().replaceAll("<.*?>", "");
                    result = element.getURL() + " (" + title + ")";
                    if (!element.getCachedSize().equals("0")) {
                        result = result + " - " + element.getCachedSize();
                    }
                }
            }
            catch (GoogleSearchFault e) {
                // Something went wrong. Say why.
                result = "Unable to perform your search: " + e;
            }
            
            if (result == null) {
                // No results were found for the search terms.
                result = "I could not find anything on Google.";
            }
            
            // Send the result to the channel.
            sendMessage(channel, sender + ": " + result);
        }
    }
    
}

Your license key is a simple string, so you can store it in the GoogleBot class as googleKey.

You now need to tell the bot which channels to join. If you want, you can tell the bot to join more than one channel, but remember, you are limited in the number of Google searches you can do per day.

Create the file GoogleBotMain.java:

public class GoogleBotMain {
    
    public static void main(String[] args) throws Exception {
        GoogleBot bot = new GoogleBot("GoogleBot");        
        bot.setVerbose(true);
        bot.connect("irc.freenode.net");
        bot.joinChannel("#irchacks");
    }
    
}

Running the Hack

When you compile the bot, remember to include both pircbot.jar and googleapi.jar in the classpath:

C:\\java\\GoogleBot> javac -classpath .;pircbot.jar;googleapi.jar *.java

You can then run the bot like so:

C:\\java\\GoogleBot> java -classpath .;pircbot.jar;googleapi.jar GoogleBotMain

The bot then starts up and connects to the IRC server.

Figure 4-14 shows GoogleBot running in an IRC channel and responding with the URL, title, and size of each of the results of a Google search.

GoogleBot performing IRC-related searches
Figure 4-14. GoogleBot performing IRC-related searches

Performing a Google search is a popular task for bots to do. Take this into account if you run your bot in a busy channel because there might already be a bot there that lets users search Google.

Paul Mutton

Google on the Go

Being on the go and away from your laptop or desktop doesn’t mean leaving Google behind.

As the saying goes, “You can’t take it with you.” Unless, that is, you’re talking about Google. Just because you’ve left your laptop at home or at the office, that doesn’t necessarily mean leaving the Web and Google behind. So long as you have your trusty cell phone or network-enabled PDA in your pocket, so too do you have Google.

Whether you have the top-of-the-line Treo 700, Blackberry, or Sidekick with integrated web browser; base-model cell phone that your carrier gave you for free; or anything in between, chances are you can Google on the go.

Google caters to the “on the go” crowd with its Google wireless interfaces: a simpler, lighter, gentler PDA- and smartphone-friendly version of Google, a WAP (read: wireless Web) flavor for cell phones with limited web access, and an SMS gateway for messaging your query to and receiving an almost instantaneous response from Google. You can also take the power of Google Maps and Google Local [Hack #63] with you so you won’t get lost again. And there’s even a mobile interface to Google’s Froogle (http://froogle.google.com) product search.

Google by PDA or Smartphone

Google PDA Search (http://www.google.com/pda) brings all the power of Google to the PDA in your palm, hiptop on your belt, or cell phone in your pocket.

Settle that “in like Flynn” versus “in like Flint” dinner-table argument without leaving your seat. Find quickie reviews and commentary on that Dustmeister 2000 vacuum before making the purchase. Figure out where you’ve seen that bit-part actor before without having to wait for the credits.

Your modern PDA and the smarter so-called smartphones sport a full-fledged web browser on which you can surf all that the Web has to offer in living color—albeit substantially smaller. You find the usual Address Bar, Back and Forward buttons, Bookmarks or Favorites, and point-and-click (or point-and-tap, as the case may be) hyperlinks. While the onboard browser might be able to handle the regular Google.com web pages, the Google PDA Search provides simpler, smaller, no-nonsense, plain HTML pages. And results pages pack in fewer results for faster loading.

Just point your mobile browser at http://google.com/pda, enter your search terms, click the Google Search button, and up come your results, as shown in Figure 4-15.

Google PDA search results on a Blackberry
Figure 4-15. Google PDA search results on a Blackberry

You have the full range of Google Search syntax [Chapter 1] and complete Web index available to you, although it might be more than a little challenging to enter those quotes, colons, parentheses, and minus signs.

Google by Cell Phone

If you have a garden-variety cell phone—the kind your mobile provider either gives away free with signup or charges on the order of $40 for—you may already have a built-in browser...of a sort. Don’t expect anything nearly as fast, colorful, or feature-filled as your computer’s web browser. This is a text-only world, limited in both display and interactivity.

That said, you have the wealth—if not the Technicolor—of the Web right in your pocket.

Step one, however, is to find the browser in the first place. It’s usually cleverly hidden behind some (possibly meaningless) moniker such as WAP, Web, Internet, Services, Downloads, or a brand name such as mMode or T-Zones. If nothing of the sort leaps out at you, look for an icon sporting your cell phone provider’s logo, take a stroll through the menus, dig out your manual, or give your provider a ring (usually 611 on your cell phone).

Browser in hand, point it at wap.google.com/wml, tap in a search (without tripping over your fingers), and click the Search button or link (as shown in Figure 4-16, left). A few moments later, your first set of results show up (as shown in Figure 4-16, right). Scroll to the bottom of the results and click the Next link to move to the next page of results.

Google wireless search home (left) and results (right)
Figure 4-16. Google wireless search home (left) and results (right)

Click any of the results to visit the page in question, just as you would in a normal browser. You’ll notice immediately that the pages you visit by clicking a result link are dumbed down—similar to Google’s wireless search itself—to suit the needs of your mobile’s display abilities.

Truth be told, you’re not directly visiting the resulting page at all. What you see on your screen and in Figure 4-17 is courtesy of the Google WAP proxy, a service that turns HTML pages into WAP/WML (think of it as HTML for wireless devices) on the fly. Click another link on the resulting page and you can continue browsing via the Google proxy. Google essentially turns the entire Web into a mobile Web.

A piece of the O’Reilly home page seen through the lens of the Google WAP proxy
Figure 4-17. A piece of the O’Reilly home page seen through the lens of the Google WAP proxy

In fact, you can actually surf rather than search the Web using the Google WAP proxy. Find your mobile browser’s Options menu and click the Go to URL link. In the resulting page, enter any web site URL into the Go to URL box and click the Go button to visit a mobile version of that page.

Tip

The Google WAP proxy is also a handy addition to your phone’s bookmarks. Add the following URL to access the proxy directly: http://google.com/gwt/n. In fact, you can visit this link in a standard web browser to preview what your favorite sites will look like once they’ve been stripped to their bare essentials for mobile browsing.

Google by SMS

As a New York Times article, “All Thumbs, Without the Stigma” at:

http://tech2.nytimes.com/mem/technology/techreview.html?res=9E00E6DE163FF931A2575BC0A9629C8B63

suggested recently, the thumb is the power digit. While the thumboard of choice for executives tends to be the Blackberry mobile email device (http://www.blackberry.com/), for the rest of the world (and for many of the kids in your neighborhood), it’s the cell phone and SMS.

SMS messages are quick-and-dirty text messages (think mobile instant messaging) tapped into a cell phone and sent over the airwaves to another cell phone for around $.05 to $.10 apiece.

But SMS isn’t just for person-to-person messaging. In the UK, BBC Radio provides so-called shortcodes (really just short telephone numbers) to which you can SMS your requests to the DJ’s automated request-tracking system. You can SMS bus and rail systems for travel schedules. Your airline can SMS you updates on the status of your flight. And now you can talk to Google via SMS as well.

Google SMS (http://www.google.com/sms/) provides an SMS gateway for querying the Google Web index, looking up phone numbers [Hack #5], seeking out definitions [Hack #6], and comparative shopping in the Froogle product catalog service (http://froogle.google.com).

Simply send an SMS message to U.S. shortcode 46645 (read: GOOGL) with one of the following forms of query:

Google Local Business Listing

Consult Google Local’s business listings by passing it a business name or type and city, state combination, or zip code:

vegetarian restaurant Jackson MS
southern cooking 95472
scooters.New York NY

Tip

The Google SMS documentation suggests using a period (.) between your query and city name or zip code to be sure that you’re triggering a Google Local Search.

Residential Phone Number

Find a residential phone number with some combination of first or last name, city, state, zip code, or area code. Or enter a full phone number without punctuation to do a reverse-lookup:

augustus gloop Chicago il
violet beauregard 95472
mike teevee ny
7078277000

Tip

As with any Google Phonebook [Hack #5] query, you’ll find only listed numbers in your results.

Froogle Prices

Check the current prices of items for sale online through Froogle (http://froogle.google.com/). To trigger a Froogle lookup, prefix your query with an F (upper- or lowercase), price, or prices (the latter two also work at the end of the query):

price bmw 2002
ugg boots prices
Definition

Rather than scratching your head trying to understand just what Ms. Austen means by disapprobation, ask Google for a definition [Hack #6]. Prefix the word or phrase of interest with a D (upper- or lowercase) or the word define:

D disapprobation
define osteichthyes
Calculation

Perform feats of calculation and conversion using the Google Calculator [Hack #32]:

(2*2)+3
12 ounces in grams
Zip Code

Pass Google SMS a U.S. zip code to find out where it is in the country:

95472

Tip

Google SMS is sure to sport more features by the time you read this. Be sure to consult the “Google SMS: How to Use” page at http://www.google.com/sms/howtouse.html for the latest or—for the real thumb jockeys among you—submit your email address to an announcement list from the Google SMS home page.

Sports Scores

Send the name of a college or pro team and get back the score of its most recent game:

sf giants
oregon state
Currency Conversion

Include the name of a currency and an amount in your message, and get back the current value:

300 usd in eur
500 yen in pounds
Facts and Figures

This one settles your bar bets. Send a question and get back an answer if Google has one. For example:

calories in milk
people in japan

You receive your results as one or more SMS messages labeled, appropriately enough, 1of3, 2of3, etc. if the answer doesn’t fit on a single screen. Notice that there are links to URLs in the responses, as shown in Figure 4-18.

A Google SMS query response
Figure 4-18. A Google SMS query response

In addition to your answer, you can often find the source of the answer in the message. As in Figure 4-18, the answer is straight from the CIA Factbook.

Warning

While the cost of sending an SMS messages is usually paid by the sender, automated messages such as those sent by Google SMS are usually charged to you, the receiver. Unless you have an unlimited SMS plan, all that Googling can add up. Be sure to check out what’s included in your mobile plan, check your phone bill, or call your mobile operator before you spend a lot of time (and money) on this service.

Froogle on the Go

If you wish you could compare prices at that “One Day Sale” on kitchen gadgets without leaving the store, Wireless Froogle (http://froogle.google.com) is as much a part of the shopping experience as that credit card.

Point your mobile browser at http://wml.froogle.com and tap in the name of the product you’re about to take to the checkout, and up pops a list of prices as advertised by online vendors, as shown in Figure 4-19.

Wireless Froogle Search results
Figure 4-19. Wireless Froogle Search results

You’ll find everything from cellular phones to yogurt makers, abacuses to faux yak fur coats on Froogle.

At the time of this writing, Wireless Froogle is nowhere near as complete as one might hope. You can’t constrain your results by price, group them by store, or sort them in any way. Results don’t link to anywhere. That said, it is a still a handy price-check tool as you’re standing in that checkout line.

$44 for a pashmina? Lemme at it! Sometimes instant gratification is worth it, and sometimes paying only $44 for silk is well worth the wait.

Maps on the Go

With a bit of prep beforehand, you can take the power of Google Maps [Hack #64] and Google Local with you as you travel. Local for mobile includes the clickable, dragable maps you find at Google Maps on your cell phone. Instead of a site that runs through your phone’s browser, Local for mobile is an application you can download and install on your phone. Browse to the Local for mobile site (http://www.google.com/glm/) and click Get Started to see if your phone is supported.

If your phone is on the list, the web site walks you through the installation process. Once installed, the application is available on your phone and the maps are always available to you. When you start the application, the familiar maps interface appears, and you can start zooming and dragging the map.

Imagine you’re out and about in Corvallis, Oregon, and you want to grab a cup of coffee. Key in the phrase coffee in corvallis for a map of coffee shops, as shown in Figure 4-20.

Local for mobile results for “coffee in corvallis”
Figure 4-20. Local for mobile results for “coffee in corvallis”

Key in the number of a result to see the name of the location, and again to see business details such as address and phone number. There’s even a quick link for dialing that particular business with one click if you need to call ahead.

Local for mobile isn’t a scaled-down version of Google Maps; it’s a fully functional version of Google Maps. You can even choose the satellite view from the menu to see satellite images of a particular area. Figure 4-21 shows a satellite image of the Pacific Northwest and California on a cell phone.

Satellite view in Local for mobile
Figure 4-21. Satellite view in Local for mobile

You might not need satellite photos to navigate your way to the nearest coffee shop, but you can travel comfortably knowing you can scope out the topography of your current location.

When you’re at a computer, be sure to stop by Google Mobile (http://www.google.com/mobile/) to stay on top of all of Google’s mobile offerings. Google is continually making its features available via mobile devices to make sure you can access your data and the Web wherever you need it.

Google over IM

Build a Google Talk bot that will have you talking directly with Google Search results.

Instant messaging is no longer solely the domain of teenagers with too much spare time on their hands. IM has morphed from a fun toy to a serious productivity tool. Google offers its own IM client called Google Talk (http://www.google.com/talk/).

Thanks to the Google Talk and the Google API, you can skip the web site and bring Google Search results directly into your IM client with a Google Talk bot. A bot is simply a program that looks like an IM chat buddy (someone who receives and sends messages). Behind the scenes, though, the bot simply does what it’s programmed to do. With a Google Talk bot up and running, you can find search results without leaving your IM client, which sounds very productive.

What You Need

Google Talk uses a standard messaging protocol called Extensible Messaging and Presence Protocol (XMPP). XMPP was developed as part of Jabber, an open IM protocol. Because Jabber has been around for a number of years, there are plenty of existing tools that speak Google Talk’s language.

This hack is written in Python and requires the jabber.py module (http://jabberpy.sourceforge.net) for communicating through Google Talk. The code for talking with the Google API is adapted from the simple Python example [Hack #95] and requires the pyGoogle module (http://pygoogle.sourceforge.net). And, of course, you’ll need a free Google API key, which you can pick up at Google Web APIs (http://www.google.com/apis/).

You’ll also need a spare Google Account for your bot. Log into Gmail with your Google account and send yourself an invitation. Be sure to log out of Google completely, follow the instructions in your Gmail invite, and sign up for Google using your alternate identity. Jot down the alternate account username and password. Remember that your bot will be logging into Google Talk, so whichever name you give your bot when you sign up will be your bot’s identity online.

The Code

This code provides a bare-bones bot that handles incoming messages and sends simple messages. In addition, the script queries Google and formats the response for instant messages. Be sure to include the login username and password for your bot. Include your Google API key as well, and then save the following code to a file called queryBot.py:

#!/usr/bin/python
# queryBot.py
# A Google Talk bot that returns Google Search
# results as messages for any incoming message.
# Usage: python queryBot.py 

import sys
import string
import re
import jabber
import xmlstream
import google

username = 'insert google account name' # do not include @gmail.com
password = 'insert google account password'
google.LICENSE_KEY = 'insert google API key'
botname = 'queryBot'

def sendMsg(toid,msg):
    r = jabber.Message(toid,msg)
    r.setType('chat')
    con.send(r)

def messageCB(con,msg):
    if msg.getBody():
        query = msg.getBody()
        fid = msg.getFrom()

        print '>>> query: %s' % query    
        print '>>> from: %s' % fid

        # Query Google.
        data = google.doGoogleSearch(query)
     
        # Output.
        for result in data.results:
             # set the results as variables
            title = result.title
            URL = result.URL
            snippet = result.snippet

            # Strip HTML
            regex = re.compile('<[^>]+>')
            title = regex.sub(r'',title)
            snippet = regex.sub(r'',snippet)
            regex2 = re.compile('&#39;')
            title = regex2.sub(r"'",title)
            snippet = regex2.sub(r"'",snippet)

            title = '\\n*%s*' % title # Bold title

            # Format result
            response = string.join( (title, snippet, URL), "\\n")

            # Send result
            r = jabber.Message(fid,response)
            r.setType('chat')
            con.send(r)

def connect():
    global con
    con = jabber.Client(host='gmail.com',debug=[],
                            log='xmpp.log',port=5223,
                            connection=xmlstream.TCP_SSL)
    con.connect()
    con.setMessageHandler(messageCB)
    con.auth(username, password, botname)
    con.requestRoster()
    con.sendInitPresence()
    print "[[ Bot is Online, ready for queries! ]]"

con = None
while 1:
    if not con:
        connect()
    con.process(1)

Note that when the Jabber client is initialized in the connect() function, a logfile (xmpp.log) is set. You’ll find a copy of all XMPP messages flying between your machine and the Google Talk server, and it’s extremely useful for finding problems with your bot.

Running the Hack

Before you can run your bot, you’ll need a bit of Google login shuffling. To combat spam, Google Talk requires every user to have explicit permission to talk to each other. Log into Google Talk as yourself and send a chat request to your bot’s identity. Then log in using your bot’s credentials and approve your real self for chatting. You’ll need to approve everyone your bot chats with.

Also, make sure both jabber.py and google.py are in the same directory as your script. If they aren’t, install them with the setup.py scripts that come with the modules.

Once everything is set, open a command prompt and start the script, like so:

python queryBot.py

The bot should start and give you the opening OK:

[[ Bot is Online, ready for queries! ]]

At this point, log into Google Talk as yourself and send a simple message to your bot. Back in the command window, you’ll see the incoming query and the user who sent it:

>>> query: ROTFL
>>> from: user@example.com/Talk.v9222832159

At this point, the script takes the incoming message and queries the Google API for search results. As the results come in, they’re sent back to the user, as shown in Figure 4-22.

QueryBot responding to the message “ROTFL”
Figure 4-22. QueryBot responding to the message “ROTFL”

The bot sends back the first 10 results for the query as individual messages.

When you’re ready to put your bot to sleep, type Ctrl-C in the command window to take your bot offline.

See Also

  • For a fully functioning Google Talk bot that can conference several users together and perform more complex commands, take a look at Google Talk: Conference Bot (http://coders.meta.net.nz/~perry/jabber/confbot.php) by Perry Lorier and Limodou—the inspiration for this hack. The code is freely available, and if you’re familiar with Python you can customize the bot for your own purposes.

  • “Google from IRC” [Hack #50].

Googlify Your Browser

The Google Toolbar and a handful of other extensions can make Google a part of your web browser.

If you already use the Quick Search box in Firefox [Hack #55], you know the value of having instant access to Google Searches whenever you browse the Web. The Google Toolbar (http://toolbar.google.com) gives you several options beyond web searching and provides one-click access to several Google features that interact with the current page you’re browsing, from translating the page to posting information from the page to a blog.

Unlike the Quick Search box, you need to take some time to install the Google Toolbar, but you’ll be up and running in just a few minutes. Once installed, the toolbar is a part of your browser, as shown in Figure 4-23.

The Google Toolbar in Firefox
Figure 4-23. The Google Toolbar in Firefox

Features

Google doesn’t provide access to all of its features via the toolbar, but there are some handy shortcuts that can give you more information about the page you’re currently reading. You can add or remove features from the Google Toolbar by clicking the Google logo on the far left of the toolbar and choosing Options.

Web search

The most prominent feature of the Google Toolbar is the blank search form. As you type a word or phrase into the form, Google tries to guess what word you’re after and offers a few suggestions along with estimated result counts in a drop-down box, as shown in Figure 4-24.

Google Toolbar autocompleting suggestions as you type
Figure 4-24. Google Toolbar autocompleting suggestions as you type

You can click a suggestion or continue typing to search for a particular phrase. Click Enter to leave the current page and view search results, or click the down arrow to choose from other Google searches such as Google Image, Google Groups, or the current site you’re reading.

Tip

Firefox users can add autocomplete suggestions to the Quick Search box with an official extension by Google called Google Suggest for Firefox (http://www.google.com/tools/firefox/suggest/index.html). This is a handy option if you don’t want to install the full Google Toolbar.

Another handy way to use the toolbar search is to highlight a word or phrase on the page, click and drag the text to the form, and then “drop” the text by releasing the mouse button. You’re instantly taken to a search results page for the highlighted phrase.

PageRank

Google assigns every site in its index a popularity value from 0 to 10 called a PageRank, and the Google Toolbar is one of the only ways to find the numeric score for any particular site. As you browse pages, the toolbar contacts Google with the URL and displays a green graph in the toolbar with the corresponding PageRank. You can place your cursor over the PageRank graph to see the numeric score, as shown in Figure 4-25.

Viewing the PageRank of the current page with Google Toolbar
Figure 4-25. Viewing the PageRank of the current page with Google Toolbar

A site with a higher PageRank score means that Google believes the site has a higher authority and displays the site higher in its search results. Using the Google Toolbar, you can also use the PageRank score to help judge the authority of a particular source.

Tip

If you want to see the PageRank value of every page you visit but don’t want to install the Google Toolbar, try the pagerankstatus extension for Firefox (http://pagerankstatus.mozdev.org). Once installed, you’ll see the green PageRank indicator in your browser’s lower status bar. Keep in mind that this extension isn’t supported by Google in any way, and you’ll be sending each site you visit to a third party.

Blog This!

If you publish a blog with Google’s free tool Blogger (http://www.blogger.com), the Google Toolbar offers a quick way to quote other web sites. Click the orange B button (also known as Blog This!) on the toolbar to bring up a new window with a form for composing a new blog post. The text area includes the HTML necessary to display the title of the page you’re viewing, linked to the page URL. If you highlight some text on the page before you click Blog This!, as shown in Figure 4-26, the text is automatically quoted in the new entry as well.

Quoting a web site with the Blog This! feature of the Google Toolbar
Figure 4-26. Quoting a web site with the Blog This! feature of the Google Toolbar

Blog This! takes the work out of linking to interesting bits of information you find on the Web. It’s a great way to start your own “web clippings file” to share with others.

If you want only the Blog This! feature and don’t need the rest of the Google Toolbar, you can install a bookmarklet that functions exactly the same way by visiting the Blogger help page for Blog This! (http://help.blogger.com/bin/answer.py?answer=152).

Page information

The blue i button on the toolbar provides quick shortcuts to extended information about the page you’re viewing at Google. Click the button to choose one of four options shown in Figure 4-27.

Finding extended information about the current page with Google Toolbar
Figure 4-27. Finding extended information about the current page with Google Toolbar

Cached Snapshot of Page shows you the latest version of the page in Google’s cache, if available; the Similar Pages link uses the related: syntax to show links to pages Google has determined are similar to the current page; Backward Links uses the link: syntax to find other pages that link to the current page; and, as you’d expect, the Translate Page into English link uses Google’s Language Tools (http://www.google.com/language_tools) to translate the current page. You can change your default translation language in the toolbar options.

Spellchecking

Another useful feature of the toolbar is the ability to check your spelling on any web form. If you contribute to a number of different web sites, you know that not all of them provide spellchecks, and you often have to bring up another program, such as a word processor or email program, to check your text before you send it off. Instead, you can click the ABC button on the Google Toolbar, which highlights any misspelled words, as shown in Figure 4-28.

Checking your spelling in any web form with the Google Toolbar
Figure 4-28. Checking your spelling in any web form with the Google Toolbar

Click a highlighted word to see a list of suggestions and click a suggestion to make the change. Click any empty space in the form to stop the spellchecker.

Tip

This list is only a sampling of the tools available with the Google Toolbar, and the best way to get to know the features is to play around with them.

Installation

To install the Google Toolbar, point your browser to http://toolbar.google.com and click the blue Download Google Toolbar button. From there, you need to read through the Terms and Conditions and click Agree and Install to start the installation.

At the time of this writing, the toolbar is available for Internet Explorer on Windows and Firefox on Windows, Mac OS X, and Linux. Because the program is a browser extension rather than a traditional application, the download and installation happen within the browser window. You need to approve some security requests along the way. Figure 4-29 shows the standard extension installation dialog for Firefox.

Installing the Google Toolbar in Firefox
Figure 4-29. Installing the Google Toolbar in Firefox

Click Install Now to download and install the toolbar, and then restart Firefox to start using the toolbar. The process for installing the toolbar in Internet Explorer is similar, but you’ll see something like Figure 4-30.

Internet Explorer security warning for the Google Toolbar
Figure 4-30. Internet Explorer security warning for the Google Toolbar

Click Run to start the Google Toolbar installation. During the Internet Explorer installation process, you can choose to enable or disable features that “phone home” to Google with your browsing activities. Figure 4-31 shows the privacy notice from the installation process.

Internet Explorer privacy warning from the Google Toolbar installation
Figure 4-31. Internet Explorer privacy warning from the Google Toolbar installation

Some features require that the toolbar contact Google to get extended information about pages you’re visiting. If you’re uncomfortable with the idea that Google has access to every page you’re visiting, you might want to disable the advanced features during installation. Keep in mind that you can always disable the advanced features at any point after installation.

Warning

To get a better sense of how the Google Toolbar affects your browsing privacy, read the answers to the privacy questions at the Google Toolbar site (http://www.google.com/support/toolbar/bin/topic.py?topic=938).

Once you’ve made it through the security gauntlet, restart your browser; the Google Toolbar is waiting for you just below your main browser controls.

Privacy

As mentioned earlier, many of the features of the Google Toolbar require sending information to Google’s servers to function. If you want to use to toolbar but aren’t comfortable with sending every page you visit to Google’s servers, you can disable the features that “phone home” to Google.

Click the Google logo on the far left of the toolbar and choose Options. From the Browse tab, uncheck PageRank Display, SpellCheck, WordTranslator, and AutoLink, and click OK to disable the features. Under the Search tab, uncheck “Suggest popular queries as you type” and click OK. Also, keep in mind that as Google adds features to the toolbar, you might need to disable features that contact Google’s servers.

Remember that the Google Toolbar keeps a list of every query you type so you can access them quickly later. To clear your query cache at any time, click the Google logo and choose Clear Search History. You can also set the toolbar to forget your saved queries at the end of a session by unchecking “Save the search history...” under the Search tab in the toolbar options.

Finally, if you want to remove the toolbar completely, you can quickly remove it by clicking the Google logo and choosing Help→Uninstall. A new window pops up, asking you to confirm the removal and provide some optional info about why you’re removing the toolbar. Click Uninstall the Google Toolbar, restart your browser, and the Google Toolbar will be history.

Though you might give up a bit of privacy if you use the toolbar, you get quick access to some useful Google features in exchange, so you’ll have to weigh the pros and cons of the toolbar for yourself.

Search with Google from Any Web Page

Searching the Web can be as simple as highlighting a term and clicking your mouse.

Imagine you’re reading your favorite blog and the author starts rambling on about retro video games. Before you know it, you’re knee-deep in gaming jargon as she compares her SNES Emulator to MAME or discusses her favorite ROMs and Mods. If such terms are Greek to you and you want to find out what they mean, wouldn’t it be great to just highlight the term, click a button on your mouse, and have the answer? This type of context-menu search is easy to set up, if it isn’t set up in your browser already.

If you use the Firefox browser (http://www.mozilla.com/firefox/), you’re in luck, because a context search is built right in. Simply highlight any term on a web page, right-click with your mouse (Ctrl-click on a Mac), and click the “Search Web for...” option shown in Figure 4-32.

Firefox context search
Figure 4-32. Firefox context search

Firefox opens a new tab with the Google Search results page for the word or phrase you highlighted.

Microsoft Internet Explorer (http://www.microsoft.com/windows/ie/) users don’t have things quite so easy because there’s no built-in context search. But you can get your own context search up and running in a few minutes with a bit of JavaScript and a new Registry entry.

The Code

This code handles the work of taking a highlighted term and opening a new browser window with a properly formatted Google URL. Open a text editor such as Notepad and create a new file called GoogleSearch.html with the following code:

<script language="JavaScript">
var searchURL = new String("http://www.google.com/search?q=");

var w = window.external.menuArguments;
var d = w.document;
var s = d.selection;
var r = s.createRange();
var term = new String(r.text);

window.open(searchURL + term);
</script>

Save this file on your computer in a memorable spot or create a new folder for it, such as c:\\scripts\\. Jot down the full path to this new file and open a blank file in Notepad again. This new file adds some information to your Windows Registry to let Internet Explorer know where to find GoogleSearch.html and when to execute it.

Add the following code and save the new file as GoogleContext.reg:

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\\Software\\Microsoft\\Internet Explorer\\MenuExt\\Search Google]
@="c:\\\\scripts\\\\GoogleSearch.html"
"contexts"=dwords:00000010

Now double-click the file and confirm that you want to add the Registry information. You’ve just added a right-click menu entry called Search Google that will appear whenever you right-click on highlighted text within Internet Explorer. When you right-click and select the Search Google option, the JavaScript file you created earlier executes.

Running the Hack

Close any open Internet Explorer windows and then restart the browser. You should be able to highlight any text on a page and see the new context-menu entry shown in Figure 4-33.

Search Google context-menu entry
Figure 4-33. Search Google context-menu entry

Choosing Search Google opens a new window such as the one in Figure 4-34, displaying search results for the selected term.

Google Search results window
Figure 4-34. Google Search results window

With just a few minutes’ coding, you’ll have streamlined access to Google, giving you the knowledge that MAME stands for Multiple Arcade Machine Emulator and a number of links to follow for more information.

Customize the Firefox Quick Search Box

Though Google Web Search is the default option in the Firefox search box, with some quick coding, you can add many other Google Search types.

If you use the Firefox web browser (available at http://www.mozilla.org/products/firefox/), you’re probably already aware of the useful search box in the upper-right corner. From any page, at any time, you can simply type a query into the box and press Enter, and the search page comes up in the browser. Though Google is the default search engine, you can click the arrow to choose from many other search engines, as shown in Figure 4-35.

The default Firefox search engine options
Figure 4-35. The default Firefox search engine options

The nice thing about this list of potential search engines is that you can add any search engine of your choice. In fact, Firefox offers an Add Engines... option that takes you to a page with more search choices you can install with a few clicks. The New Search Engines section of the Mozilla site (the technology behind Firefox) contains the page shown in Figure 4-36 (http://mycroft.mozdev.org/quick/google.html), full of over 300 different Google-related searches you can add to the Firefox search box.

List of Google-related Firefox Quick Search options at Mozilla.org
Figure 4-36. List of Google-related Firefox Quick Search options at Mozilla.org

These are searches that others have found useful and decided to share with the larger Mozilla community. The Google specialty searches include everything from searching specialty engines that Google offers, such as Google Scholar (http://scholar.google.com) or Google Blogsearch (http://blogsearch.google.com), to searching Google in different countries and languages. Some plug-ins simply use the site: operator to search a specific web site using Google, such as the Wikipedia with Google plug-in that searches the popular online encyclopedia using Google.

Warning

Keep in mind that all the plug-ins at Mozilla are contributed by users, and some might work better than others. Keep an eye out for a green checkmark in front of a specific engine, which indicates the plug-in was tested and was found to be working. A blue question mark indicates a plug-in that hasn’t been tested, a red X indicates a broken plug-in, and a green N indicates a new plug-in.

To add a search engine from this Mozilla page, simply click the name of the search engine you’d like to add. A pop-up box asks you to confirm your choice; click OK, and the new choice is available in the Firefox search box menu. Behind the scenes, Firefox has copied a small .src file and icon to the searchplugins directory of the Firefox installation. This text file defines how the search works.

If you don’t find the search of your dreams at the Mozilla page, it’s fairly easy to build your own specialty Google search and add it to your list of available search engines. You just need a simple text editor to create the search engine text file and an eye for spotting patterns in search URLs.

The Code

Imagine you find yourself frequently looking for academic papers on various subjects. You could use the Google Advanced Search to limit your results to printable PDF documents across .edu domains, giving you a higher chance of finding relevant papers. But tweaking the Advanced Search Form every time you want to run that particular search is time you could spend finding what you’re looking for. This is a perfect case for a custom Firefox Quick Search entry.

The first step in creating a custom entry is to perform a search and take a look at the URL. For this example, browse to the Advanced Search form at http://www.google.com/advanced_search, type aerodynamics into the top search field, change the file format to Adobe Acrobat PDF (.pdf), set the domain to .edu, and click Google Search. You should receive a page full of PDF documents across educational domains related to aerodynamics.

Now take a closer look at the URL. The relevant pieces of the URL include the google.com domain, the search file, and several querystring variables that make up the search query:

http://www.google.com/search?as_q=aerodynamics&num=10&hl=en&btnG=Google+Search&as_epq=&as_oq=&as_eq=&lr=&as_ft=i&as_filetype=pdf&as_qdr=all&as_occt=any&as_dt=i&as_sitesearch=.edu&as_rights=&safe=images

This is where your skills at assembling Advanced Search URLs [Hack #17] come in handy. Note the important variable/value pairs as_q= query, as_filetype=pdf and as_sitesearch=.edu in this example.

Knowing how Google Advanced Search URLs are constructed, you can write the file that tells Firefox where to send search requests. Create a file called google_pdf_edu.src in a plain-text editor such as Notepad and add the following code:

# Google PDF Search across .edu domains
#
# Created January 26, 2006

<SEARCH
    version="7.1"
    name="Google EDU Search"
    description="Search for PDFs across EDU domains"
    method="GET"
    action="http://www.google.com/search" >

<input name="as_filetype" value="pdf">
<input name="as_sitesearch" value=".edu">
<input name="as_q" user>

</search>

As you can see, this quick file begins with an opening <SEARCH> tag that holds the name of the search and a brief description. Everything before the question mark in the search results URL becomes the value of the action attribute. The input tags let Firefox know which variable/value pairs should be included in the query. The user designation in an input tag lets Firefox know that user input should be supplied for that particular querystring variable—in this case, as_q.

Running the Hack

Save the file and add it to the Firefox searchplugins directory, usually located at C:\\Program Files\\Mozilla Firefox\\searchplugins\\ on Windows and at /Applications/Mozilla.app/Contents/MacOS/searchplugins on Mac OS X. You’ll also need an icon for the search, and because Firefox comes with a Google Search option, you can simply copy the existing google.gif file in the searchplugins directory and name it the same thing as your new Google Search text file—google_pdf_edu.gif, in this example.

Once you restart Firefox, you’ll find a new option in the search list called Google EDU Search. Choose this option and type the original aerodynamics query in the search box. If all goes well, you should see a matching page of Google search results such as the one shown in Figure 4-37.

Google Advanced Search results via the Firefox search box
Figure 4-37. Google Advanced Search results via the Firefox search box

If you find yourself using a particular Google search time and again, you might be able to speed up your access to the search with an eye for search URLs and some quick text editing.

Build a Google Screensaver

With a bit of Perl and the built-in screensavers available in Mac OS X or Windows XP, you can create your own screensaver that shows pictures from Google Images.

Along with desktop backgrounds, screensavers have always been a feature of personal computers that people feel comfortable changing, tweaking, fiddling with, and hacking for fun. And by scripting Google Images, you can create a screensaver based on images from across the Web.

This hack relies on the screensavers that ship with Windows XP and Mac OS X. Each screensaver lets you specify a directory on your computer that contains images, and displays those images on your screen during your computer’s idle moments. A Perl script downloads images from a Google Images search that you specify.

The Code

This code works on both Windows XP and Mac OS X systems, but you’ll need a Perl component that isn’t installed by default. The WWW::Google::Images module (http://search.cpan.org/dist/WWW-Google-Images/lib/WWW/Google/Images.pm) handles all of the hard work of gathering images from a Google Images search and saving a copy on your computer.

Copy the code to a file called goosaver.pl and put the file in a local folder path where the images will be stored. On Windows XP, you should specify a drive and folder, such as C:\\goosaver. On Mac OS X, you should specify a full path, such as /Users/pb/Photos/goosaver.

The following code contacts Google Images with your query and downloads matching photos:

#!/usr/bin/perl
# goosaver.pl
# Downloads images from a Google Image
# search for a screensaver.

use strict;    
use WWW::Google::Images;

# Take the query from the command line.
my $query = join(' ',@ARGV) or die "Usage: perl spell.pl <query>\\n";

# Create a new WWW::Google::Images instance.
my $agent = WWW::Google::Images->new(
            server => 'images.google.com');

# Query Google Images.
my $result = $agent->search($query . " inurl:wallpaper", 
                            limit => 25,
                            iregex => 'jpg'
);

# Save each image in the result locally, with
# the format [query][count].[extension].
my $count;
while (my $image = $result->next()) {
    $count++;
    print $image->content_url() . "\\n";
    print $image->save_content(base => $query . $count) . "\\n\\n";
}

Note that although the query is set on the command line, this script adds the inurl:wallpaper keyword to the query. This means Google Images will return only images that have the word wallpaper in the URL, taking advantage of how people naturally organize their files online. If you don’t get good results with this addition, simply remove this bit from the script or try other options that people might use to organize images, such as inurl:large or inurl:desktop.

Also note that the iregex => 'jpg' option limits saved results to files that are JPEGs. If you want more varied file types to be returned, remove this line, but keep in mind that the system screensavers typically prefer JPEGs.

Running the Hack

How to run the script depends on which operating system you’re using.

On Mac OS X

To set up your screensaver on Mac OS X, first create your Google screensaver photo folder. It can be anywhere, but your Pictures directory is a memorable place. Call the new folder goosaver.

To get the ball rolling, open a Terminal window (Applications→Utilities→Terminal), change to the goosaver directory (using the cd command), and run the script from the command line, like this:

% perl goosaver.pl insert query

For example, if you want a screensaver with those mathematical visualizations called fractals, call the script like so:

% perl goosaver.pl fractal

This downloads and adds several fractal-related photos to your goosaver folder.

You can now set up your screensaver. Select System Preferences from the Apple menu and choose Desktop & Screen Saver. Click the Screen Saver button, and then click the Choose Folder... option. Select your goosaver folder in your Pictures directory, and you should see the pictures you’ve just downloaded in the preview window, as shown in Figure 4-38.

Setting a Mac screensaver folder
Figure 4-38. Setting a Mac screensaver folder

Your screensaver is now set to display the photos you downloaded from Google Images when the screensaver is activated.

On Windows XP

The process for setting up the screensaver on Windows is almost identical to the Mac OS X version. Unlike the Mac, however, Windows XP does not come with Perl installed, so you might need to do a bit more work to get started. If you want to run Perl on Windows, you can download the free ActivePerl for Windows by ActiveState from http://www.activestate.com/Products/ActivePerl/.

Warning

Don’t forget that you’ll also need to install the WWW::Google::Images module before you can use the script.

Once Perl is installed, create your new screensaver folder somewhere in your filesystem; C:\\goosaver is a good place. Run goosaver.pl from the command line (Start→Programs→Accessories→Command Prompt) to download some photos:

% perl goosaver.pl insert query

For something specific, such as landscape photos, the command would look like this:

% perl goosaver.pl landscape

Now that some photos exist in the folder, set your screensaver by right-clicking any empty space on your desktop and choosing Properties. Choose the Screen Saver tab and select My Picture Slideshow from the list of screensavers. Click the Settings button, and you should see the options shown in Figure 4-39.

Windows XP screensaver options
Figure 4-39. Windows XP screensaver options

Under the “Use pictures in this folder” heading, click Browse and choose the Google screensaver folder you created. Click OK, and your screensaver now shows the photos you just downloaded from Google Images.

It takes a bit of work on both systems to set up a custom Google Images screensaver, but you’re rewarded with unexpected images from across the Web.

Add a Feed to Google Quickly

Speed up the time it takes to add RSS feeds to your Google Homepage or Google Reader.

Adding a news feed to either Google Homepage (http://www.google.com/ig) or Google Reader (http://reader.google.com) isn’t a complex process, but it does involve some copying, pasting, clicking, and generally breaking out of the flow of reading a site to make it happen.

With a bit of browser hacking, you can reduce the friction of adding sites to Google. Because Internet Explorer and Firefox are quite different, adding feeds quickly requires different approaches in each browser.

Internet Explorer

One way to add shortcuts to Internet Explorer is through a custom context menu. A context menu is the menu that pops up when you right-click (or Ctrl-click on a Mac) an element on a web page. The context part of its name refers to the fact that different choices appear in different situations. For example, when you right-click a link, you’re presented with the options Open Link in New Window, Copy Link Location, Bookmark This Link, and others. In another context, such as when clicking an image or clicking highlighted text, you have different choices in the menu.

If you’ve been reading personal blogs for a while, you’ve probably seen many variations of the white-on-orange buttons that indicate a link to an RSS feed, and if you haven’t, you can find some examples [Hack #39] in this book.

Wouldn’t it be great if you could right-click one of these buttons and have the option to add to Google Homepage or Google Reader? This would save quite a few steps, and you wouldn’t have to break from the site you’re currently reading to add the feed. Similar to quick searching in Internet Explorer [Hack #54], this hack shows how to add a custom context menu item for adding feeds to Google.

The code

Much like a bookmarklet [Hack #43], any JavaScript that runs via a context-menu entry has access to the page currently loaded in the browser. This means that when you click the context-menu entry you’ve added, the browser executes a script that performs a particular function using information from the current page.

In this case, it grabs the URL linked from the currently clicked image, constructs a special Google URL that includes the feed URL, and opens the new URL in a new browser window.

Add the following code to a file called AddToGoogle.html:

<script language="JavaScript">
var addURL = new String("http://fusion.google.com/add?feedurl=");

var w = window.external.menuArguments
var url = w.event.srcElement.parentElement.href;

window.open(addURL + url,null,
     "height=455,width=788,status=yes,scrollbars=yes,resizable=yes");
</script> 

The external.menuArguments object holds information about the current document, and the event.srcElement is the document item the user clicked. Grabbing the href attribute of the element’s parent gives you the link URL around the image tag. Save the file in a spot you’ll remember. For simplicity in this hack, save it to a directory called c:\\scripts\\.

Now that the script is ready to go, you just need to add the context-menu entry to Internet Explorer and tell it to run this particular script when you click the entry. You can do this through the Windows Registry. The Registry is a system database that holds information about applications, including Internet Explorer. You can safely make additions to the Registry via .reg files. Create a new text file called AddGoogleContext.reg and add the following code:

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\\Software\\Microsoft\\Internet Explorer\\MenuExt\\Add to Google]

@="c:\\\\scripts\\\\AddToGoogle.html"
"contexts"=dword:00000002

Note that the contexts entry ends with 2, which means the entry will appear only when the user clicks an image. Other values you can use here include 1 (for anywhere), 20 (for text links), or 10 (for text selections).

Save the file, double-click, and confirm that you want to add the new Registry information. You now have a right-click menu entry called Add to Google that appears whenever you right-click an image.

Running the hack

Once the code and Registry settings are in place, restart Internet Explorer. Browse to a site with a feed URL link and take the new context-menu entry for a spin. When you right-click an image, you should see Add to Google, as shown in Figure 4-40.

Add to Google context-menu entry
Figure 4-40. Add to Google context-menu entry

When you click the Add to Google context-menu entry, the Add to Google page appears in a window, such as the one shown in Figure 4-41, where you can choose your preferred reader.

Add to Google page in a new window
Figure 4-41. Add to Google page in a new window

Keep in mind that the Add to Google context-menu entry is available for every image on a web page, regardless of whether it links to an RSS feed. So, you must use your best judgment about when to use the feature. If the image turns out not to be linked to an RSS feed, Add to Google won’t be able to show the feed title and description in the upper-right corner of the page, and you’ll know you clicked a bad feed link.

Choose your preferred application, and then you can close the pop-up window and go back to reading the site.

Firefox

If you use Firefox, you’re probably well aware of the orange Live Bookmark icon that appears at the right end of the address bar when the site you’re visiting has an available XML feed. This icon indicates that the site author has embedded a bit of code into the page to let applications know where her XML feed is located [Hack #39].

Normally, you can click the icon to add a Live Bookmark that tracks recent entries to the site in your browser’s bookmarks. Michael Koziarski has built an extension for Firefox called Feed Your Reader that changes the Live Bookmark feature to use the orange icon for adding feeds to your favorite reader—including Google’s offerings—instead of to your browser’s bookmarks.

Browse to the extension page (http://projects.koziarski.net/fyr/), install the extension directly in the page, and restart Firefox. Choose Tools→Extensions from the top menu, highlight Feed Your Reader, and click Options. Choose Google Reader from the list of options in the drop-down menu, as shown in Figure 4-42.

The newsreader options in Feed Your Reader
Figure 4-42. The newsreader options in Feed Your Reader

Click OK, and the extension is set to go. Browse to any site with an embedded XML feed (such as http://weblogs.oreillynet.com) and click the orange Live Bookmark icon in the address bar. Instead of adding a Live Bookmark, the Add to Google page opens in a new tab in your browser, where you can choose your preferred reader.

Though the two browsers require slightly different approaches, both can be extended to help you add feeds to Google more quickly, saving you the hassle of opening new windows and cutting and pasting URLs.

Tame Long Google URLs

With an eye for URLs and the right tools, you can shorten long Google URLs when you need to send them via email, instant message, or on paper.

Most of the time, we’re all surfing the Web in virtual isolation. It’s just you and the computer, and the last thing on your mind is the length of a URL at a page you’re visiting. But as soon as you want to share the piece of the Web you’re viewing with someone else, the length of a URL becomes important.

Because email programs wrap text at 72 characters for easy reading, any URL that’s longer could be broken. A broken URL means the person on the other end of the message won’t be able to see the page you’ve sent—or he’ll have to spend a minute or two pasting the URL together in Notepad. And imagine trying to hand-write a note to someone that includes some of the URLs you stumble across!

Trimming Google URLs

Google has a lot of great content to share with others, but some of the URLs are definitely too long to send via email. For example, using the Quick Search box in the Firefox browser [Hack #55] to search for the term brevity on Google yields this URL:

http://www.google.com/search?q=brevity&start=0&ie=utf-8&oe=utf-8&client=firefox-a&rls=org.mozilla:en-US:official 

Those 112 characters are definitely past the 72-character safe zone. If you look at the URL, you can see some variable/value pairs that contain the relevant information. The characters ?q=brevity look important, but the rest of the URL looks like gibberish.

Tip

It’s important to note that what looks like gibberish is actually useful information to Google, but it’s not useful to you when you’re trying to share links, so you can cut it out.

Cutting out the garbage characters of the URL gives you something more manageable:

http://www.google.com/search?q=brevity

The 38 characters in this URL are well within the safe zone, and the URL points to exactly the same page. And once you know that a Web Search URL without the www prefix automatically redirects to the same page, you can trim four more characters from the URL:

http://google.com/search?q=brevity

This q= pattern is repeated throughout Google’s services, and you can often use this method to trim URLs from places beyond the Web Search. Here are a few examples:

ServiceURL pattern
Google Images
http://images.google.com/images?q=insert query
                           
Google Groups
http://groups.google.com/groups?q=insert query
                           
Google News
http://news.google.com/news?q=insert query
                           
Froogle
http://froogle.google.com/froogle?q=insert query
                           

When you’re ready to share a URL, keep an eye out for ways to trim the URL down to size. But there will be times when the only option you have is a URL-trimming service.

URL-Trimming Services

The scourge of long URLs is so rampant on the Web that several free services have appeared to help you share even the most insanely long URLs with others. To see how these services can help, here’s an example of a Google Maps URL that points to a page with driving directions from San Francisco, Calif., to the O’Reilly offices in Sebastopol, Calif.:

http://maps.google.com/maps?f=d&hl=en&saddr=San+Francisco,+CA&daddr=1005+Gravenstein+Hwy+N,+Sebastopol,+CA

As you can see, this 106-character URL is dense with information. There’s nothing extraneous we can strip out to get the same information at Google. This is where TinyURL.com can help. Copy any long URL you want to abbreviate and paste it into the form on the front page at http://tinyurl.com. Click the Make TinyURL! button, and the next page gives you an abbreviated URL, like this:

http://tinyurl.com/oorj6

These 24 characters are well within the safe zone and definitely won’t break in an email. Another service, available at http://shorl.com, produces the following URL:

http://shorl.com/dikafrekikuru

Each of these services stores the long URL on their servers, assigns the URL a random character string, and redirects to that long URL when someone visits the short address on their servers. Shorl.com even provides some usage statistics, so you can see how many people have used the shortened URL.

There are some drawbacks to using these third-party services. The person you’re sharing the link with won’t know what site he’s actually going to visit. This might make for some fun practical jokes, but it’s always better to be as direct as possible when sharing URLs with people. Also, the longevity of the link isn’t guaranteed. If TinyURL or Shorl.com goes out of business tomorrow, your link will fail. Using a redirection service such as these isn’t the best choice if you’re going to print a URL in a book, for example. But for casual use, these services are a good way to share long URLs without annoying the person on the other end.

Autocomplete Search Terms as You Type

Google can suggest your search terms before you even finish typing them.

It’s true: Google is clairvoyant. It can guess what you’re going to search for even before you’ve typed it. Well, maybe that overstates it. But it can certainly take an educated guess, based on the popularity and number of results of certain keywords.

Tip

This hack relies on the Greasemonkey Plugin (http://greasemonkey.mozdev.org/) for the Firefox web browser (http://www.mozilla.com/firefox/).

Don’t believe me? Visit http://www.google.com/webhp?complete=1 and start typing, and Google will autocomplete your query after you’ve typed just a few characters. This is insanely cool, and virtually nobody knows about it. And even people “in the know” need to visit a special page to use it. This hack makes this functionality work everywhere—even on the Google home page (http://www.google.com).

The Code

This user script runs on all Google pages, but it works only on pages with a search form. Of course, being Google, this is most pages, including the home page and web search result pages.

This hack doesn’t do any of the autocompletion work itself. It relies entirely on Google’s own functionality for suggesting completions for partial search terms, defined entirely in http://www.google.com/ac.js. All we need to do is create a <script> element pointing to Google’s own code and insert it into the page. Then, we tell Google to activate it by adding another <script> element that calls Google’s own InstallAC function.

Save the following user script as google-autocomplete.user.js:

// ==UserScript==
// @name            Google Autocomplete
// @namespace       http://diveintomark.org/projects/greasemonkey/
// @description     Autocomplete search keywords as you type
// @include         http://*.google.tld/*
// @exclude         http://*/*complete=1*
// ==/UserScript==

function getSearchBox(sFormName) {
    return document.forms.namedItem(sFormName);
}

function injectAC(sFormName) {
    var elmScript = document.createElement('script');
    elmScript.src = 'http://www.google.com/ac.js';
    document.body.appendChild(elmScript);
    var elmDriver = document.createElement('script');
    elmDriver.innerHTML = 'var elmForm = document.forms.namedItem("' +
    sFormName + '");\\n' +
    'InstallAC(elmForm, elmForm.elements.namedItem("q"),' +
    'elmForm.elements.namedItem("btnG"), "search", "en");';
    document.body.appendChild(elmDriver);
}

var sFormName = 'f';
var elmForm = getSearchBox(sFormName);
if (!elmForm) {
    sFormName = 'gs';
    elmForm = getSearchBox(sFormName);
}
if (!elmForm) { return; }
window.setTimeout(function() { injectAC(sFormName); }, 100);

Running the Hack

After installing the user script (Tools→Install This User Script), go to http://www.google.com and start typing the word greasemonkey. After typing the first three letters, gre, you will see a drop-down menu with possible completions, as shown in Figure 4-43.

Autocompletion of “gre” search
Figure 4-43. Autocompletion of “gre” search

If you continue typing greasemonkey and then type a space, Google will suggest possible multiword searches, as shown in Figure 4-44.

Suggestions for multiword “greasemonkey” search
Figure 4-44. Suggestions for multiword “greasemonkey” search

Mark Pilgrim

Refine Your Google Search

Google might already know what keywords you should add to your search to find exactly what you’re looking for.

As described in “Autocomplete Search Terms as You Type” [Hack #59], you can visit http://www.google.com/webhp?complete=1 and start typing, and Google Suggest will autocomplete your query as you type. By itself, this is wickedly cool. Now, let’s make it even cooler by integrating it into the main Google web search. Along with the usual search results, you’ll see a list of related queries made up of additional keywords, so you can refine your search.

The Code

Google Suggest works by requesting a specially constructed URL with the characters you’ve typed so far. The request returns JavaScript code, and Google Suggest evaluates this code and adds the results to its autocomplete menu. If you type a complete keyword, followed by a space, Google Suggest will return a list of popular searches that include your keyword plus one or two other words.

For example, if you type firefox, Google Suggest constructs this URL:

http://www.google.com/complete/search?js=true&qu=firefox

Enter that URL in your location bar and you’ll see Google’s response:

sendRPCDone(frameElement, "firefox", new Array("firefox", "firefox download",
"firefox browser", "firefox extensions", "firefox plugins", "firefox mozilla",
"firefox themes", "firefox.com", "firefox web browser", "firefox 1.0"),
new Array("25,900,000 results", "8,000,000 results", "6,990,000 results",
"1,270,000 results", "1,250,000 results", "8,160,000 results",
"1,950,000 results", "1 result", "5,460,000 results", "6,540,000 results"),
new Array(""));
[end example]

In other words, Google is already doing the hard part: tracking billions of queries and ranking them by popularity. Compared to that, constructing the request and parsing the response is easy. You can mimic Google’s autocomplete algorithm by constructing the URL yourself, calling GM_xmlhttpRequest and parsing the response.

Save the following user script as refinesearch.user.js:

// ==UserScript==
// @name          Refine Your Search
// @namespace     http://diveintomark.org/projects/greasemonkey/
// @description   adds a "refine your search" list on Google search results
// @include       http://www.google.tld/search*
// ==/UserScript==

function getCurrentSearchText() {
    var elmForm = document.forms.namedItem('gs');
    if (!elmForm) { return; }
    var elmSearchBox = elmForm.elements.namedItem('q');
    if (!elmSearchBox) { return; }
    var usQuery = elmSearchBox.value;
    if (!usQuery) { return; }
    return usQuery;
}

function getFirstSearchResult() {
    var results = document.evaluate("//p[@class='g']", document, null,
        XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
    return results.snapshotLength ? results.snapshotItem(0) : null;
}

function parseRefineYourSearchResults(oResponse) {
    if (oResponse.responseText.indexOf('new Array(') == -1) return;
    var arResults = oResponse.responseText.split(
        'new Array("')[1].split('")')[0].split('", "');
    var usQuery = getCurrentSearchText();
    var htmlArResults = new Array();
    for (var i = 0; i < arResults.length; i++) {
        if (!arResults[i] || (arResults[i] == usQuery)) continue;
        htmlArResults.push('<a href="http://www.google.com/search?q=' +
                           escape(arResults[i]) + '">' +
                           arResults[i] + '</a>');
    }
    if (!htmlArResults.length) return;
    var elmRefine = document.createElement('div');
    elmRefine.id = 'refineyoursearch';
    elmRefine.style.fontSize = 'small';
    elmRefine.style.paddingTop = elmRefine.style.paddingBottom = '1em';
    var html = 'Refine your search: ' + htmlArResults.join(' &middot; ');
    elmRefine.innerHTML = html;
    var elmFirstResult = getFirstSearchResult();
    elmFirstResult.parentNode.insertBefore(elmRefine, elmFirstResult);
}

var usQuery = getCurrentSearchText();
if (!usQuery) return;
if (!getFirstSearchResult()) return;
GM_xmlhttpRequest({
    method: "GET",
    url:    "http://www.google.com/complete/search?hl=en&js=true&qu=" +
            escape(usQuery + ' '),
    onload: parseRefineYourSearchResults
});

Running the Hack

After installing the user script from Tools→Install This User Script, go to http://www.google.com and search for firefox. Before the first search result, you’ll see a list of related queries, as shown in Figure 4-45.

Google search for “firefox” with suggested refined searches
Figure 4-45. Google search for “firefox” with suggested refined searches

If you click on one of the suggested refined searches, such as firefox plugins, Google displays those search results, which include suggestions for even further refinements, as shown in Figure 4-46. Depending on your keywords, you might be able to drill down several levels, until Google finally runs out of suggestions.

Google search for “firefox plugins” with suggestions
Figure 4-46. Google search for “firefox plugins” with suggestions

Google Suggest works only on web searches, and only in English, so this hack inherits those limitations. You can read more about Google Suggest in Google’s FAQ (http://labs.google.com/suggestfaq.html).

Mark Pilgrim

Make Google More Accessible for Low-Vision Users

Change Google’s layout to make it easier for low-vision users to read.

As a class of disabilities, low-vision users are often ignored by accessibility experts. However, accessibility expert Joe Clark has recently published his research into the needs of web users with limited vision. He pioneered a technique known as the zoom layout: a special alternate style applied to a web page that specifically caters to low-vision users.

Tip

This hack relies on the Greasemonkey Plugin (http://greasemonkey.mozdev.org/) for the Firefox web browser (http://www.mozilla.com/firefox/).

As I was learning about zoom layouts, it occurred to me that this would be a perfect application of Greasemonkey. (Actually, that thought occurs to me a lot these days.) This hack is my first attempt at transforming a site into a zoom layout.

Tip

If you want, you can read more about zoom layouts at http://www.alistapart.com/articles/lowvision/ and http://joeclark.org/atmedia/atmedia-NOTES-2.html.

The Code

This user script runs on several specific Google pages:

Tip

This hack is written to be cross-browser compatible. It works in Firefox with Greasemonkey, in Internet Explorer 6 for Windows with Turnabout, and in Opera 8 with its built-in support for User JavaScript. You can download Turnabout at http://reifysoft.com/turnabout.php, and Opera at http://www.opera.com.

Save the following user script as zoom-google.user.js:

// ==UserScript==
// @name          Zoom Google
// @namespace     http://diveintomark.org/projects/greasemonkey/
// @description   make Google more accessible to low-vision users
// @include       http://www.google.tld/
// @include       http://www.google.tld/?*
// @include       http://www.google.tld/webhp*
// @include       http://www.google.tld/imghp*
// @include       http://www.google.tld/search*
// @include       http://images.google.tld/
// @include       http://images.google.tld/?*
// @include       http://images.google.tld/images*
// ==/UserScript==

function addGlobalStyle(css) {
    var elmHead, elmStyle;
    elmHead = document.getElementsByTagName('head')[0];
    elmStyle = document.createElement('style');
    elmStyle.type = 'text/css';
    elmHead.appendChild(elmStyle);
    elmStyle.innerHTML = css;
}

function getElementsByClassName(sTag, sClassName) {
    sClassName = sClassName.toLowerCase() + ' ';
    var arElements = document.getElementsByTagName(sTag);
    var iMax = arElements.length;
    var arResults = new Array();
    for (var i = 0; i < iMax; i++) {
        var elm = arElements[i];
        var sThisClassName = elm.className;
        if (!sThisClassName) { continue; }
        sThisClassName = sThisClassName.toLowerCase() + ' ';
        if (sThisClassName.indexOf(sClassName) != -1) {
            arResults.push(elm);
        }
    }
    return arResults;
}

function removeFontTags() {
    // remove font tags
    var arFonts = document.getElementsByTagName('font');
    for (var i = arFonts.length - 1; i >= 0; i--) {
        var elmFont = arFonts[i];
        var elmSpan = document.createElement('span');
        elmSpan.innerHTML = elmFont.innerHTML;
        elmFont.parentNode.replaceChild(elmSpan, elmFont);
    }
}

function zoomStyle() {
    addGlobalStyle('body { margin: 30px; } \\n' +
'body, td { font-size: large ! important; } \\n' +
'html>body, html>body td { font-size: x-large ! important; } \\n' +
'body, div, td { background: navy ! important; ' +
    'color: white ! important; } \\n' +
'a:link { background: transparent ! important; ' +
    'color: yellow ! important; } \\n' +
'a:visited { background: transparent ! important; ' +
    'color: lime ! important; } \\n' +
'a.fl { background: transparent ! important; ' +
    'color: white ! important; } \\n' +
'input { font-size: large ! important; } \\n' +
'html>body input { font-size: x-large ! important; } \\n' +
'.g { width: auto ! important; } \\n' +
'.n a, .n .i { font-size: large ! important; } \\n' +
'html>body .n a, html.body .n .i { font-size: x-large ! important; } \\n' +
'.j { width: auto ! important; }');
}

function accHomePage() {
    // remove personalized header, if any
    var arTable = document.getElementsByTagName('table');
    for (var i = arTable.length - 1; i >= 0; i--) {
        var elmTable = arTable[i];
        var html = elmTable.innerHTML;
        if (/\\/accounts\\/Logout/.test(html)) {
            elmTable.parentNode.removeChild(elmTable);
        }
    }

    // simplify logo
    var arImages = document.getElementsByTagName('img');
    for (var i = arImages.length - 1; i >= 0; i--) {
        var elmLogo = arImages[i];
        if (elmLogo.alt) {
            var elmTextLogo = document.createElement('h1');
            elmTextLogo.style.fontSize = '400%';
            var sAlt = /Firefox/.test(elmLogo.alt) ? '' : elmLogo.alt;
            elmTextLogo.appendChild(document.createTextNode(sAlt));
            elmLogo.parentNode.replaceChild(elmTextLogo, elmLogo);
            var elmLink = elmTextLogo.parentNode;
            while (elmLink.nodeName != 'BODY' && 
                   elmLink.nodeName != 'HTML' && 
                   elmLink.nodeName != 'A') {
                elmLink = elmLink.parentNode;
            }
            elmLink.style.textDecoration = 'none';
        } else {
            elmLogo.parentNode.removeChild(elmLogo);
        }
    }

    // simplify search form
    if (document.forms.length) {
        var arTD = document.getElementsByTagName('td');
        for (var i = arTD.length - 1; i >= 0; i--) {
            var elmTD = arTD[i];
            if (/Advanced/.test(elmTD.innerHTML)) {
                elmTD.innerHTML = '';
            }
        }
    }
}

function accSearchResults() {
    // simplify logo
    var elmLogo = document.getElementsByTagName('img')[0];
    var elmTextLogo = document.createElement('h1');
    elmTextLogo.appendChild(document.createTextNode('Google'));
    elmTextLogo.style.marginTop = '0.2em';
    elmTextLogo.style.marginRight = '0.3em';
    elmLogo.parentNode.replaceChild(elmTextLogo, elmLogo);
    elmTextLogo.parentNode.style.textDecoration = 'none';

    // simplify top form
    var elmAdvancedWrapper = document.getElementsByTagName('table')[3];
    var elmAdvanced = elmAdvancedWrapper.getElementsByTagName('td')[1];
    elmAdvanced.parentNode.removeChild(elmAdvanced);

    // remove "tip" if present
    var elmTip = document.getElementsByTagName('table')[7];
    if (/Tip/.test(elmTip.innerHTML)) {
        elmTip.parentNode.removeChild(elmTip);
    }

    // remove ads, if any
    var aw1 = document.getElementById('aw1');
    while (aw1) {
        var table = aw1.parentNode;
        while (table.nodeName != 'TABLE') {
            table = table.parentNode;
        }
        table.parentNode.removeChild(table);
        aw1 = document.getElementById('aw1');
    }
    var tpa1 = document.getElementById('tpa1');
    if (tpa1) {
        while (tpa1.nodeName != 'DIV' && tpa1.nodeName != 'P') {
            tpa1 = tpa1.parentNode;
        }
        tpa1.parentNode.removeChild(tpa1);
    }
    var tpa2 = document.getElementById('tpa2');
    if (tpa2) {
        while (tpa2.nodeName != 'DIV' && tpa2.nodeName != 'P') {
            tpa2 = tpa2.parentNode;
        }
        tpa2.parentNode.removeChild(tpa2);
    }
    addGlobalStyle('iframe[name="google_ads_frame"] { ' +
        'display: none ! important }');

    // simplify results count
    var elmDivider = document.getElementsByTagName('table')[5];
    elmDivider.parentNode.removeChild(elmDivider);
    var elmResultsContainer = document.getElementsByTagName('table')[5];
    var arTD = elmResultsContainer.getElementsByTagName('td');
    if (arTD.length > 1) {
        var sResults = arTD[1].textContent;
        var iParen = sResults.indexOf('(');
        if (iParen != -1) {
            sResults = sResults.substring(0, iParen);
        }
        var iDef = sResults.indexOf('[');
        if (iDef != -1) {
            sResults = sResults.substring(0, iDef);
        }
        var elmResults = document.createElement('h2');
        elmResults.appendChild(document.createTextNode(sResults));
        elmResultsContainer.parentNode.replaceChild(elmResults, 
            elmResultsContainer);
    } else {
        elmResultsContainer.parentNode.removeChild(elmResultsContainer);
    }

    // make search results use real headers
    var arResults = getElementsByClassName('p', 'g');
    for (var i = arResults.length - 1; i >= 0; i--) {
        var elmResult = arResults[i];
        var arLink = elmResult.getElementsByTagName('a');
        if (!arLink.length) { continue; }
        var elmLink = arLink[0];
        var elmWrapper = document.createElement('div');
        var elmHeader = document.createElement('h3');
        elmHeader.style.margin = elmHeader.style.padding = 0;
        elmHeader.innerHTML = '<a href="' + elmLink.href + '">' + 
            elmLink.innerHTML + '</a>';
        var elmContent = elmResult.cloneNode(true);
        elmContent.innerHTML = elmContent.innerHTML.replace(/<nobr>/g, '');
        arLink = elmContent.getElementsByTagName('a');
        if (!arLink.length) { continue; }
        elmLink = arLink[0];
        elmContent.removeChild(elmLink);
        elmContent.style.marginTop = 0;
        elmWrapper.appendChild(elmHeader);
        elmWrapper.appendChild(elmContent);
        elmResult.parentNode.replaceChild(elmWrapper, elmResult);
    }

    // simplify next page link
    var arFont = document.getElementsByTagName('font');
    for (var i = arFont.length - 1; i >= 0; i--) {
        var elmFont = arFont[i];
        var html = elmFont.innerHTML;
        if (/Result\\&nbsp\\;Page\\:/.test(html)) {
            var elmTable = elmFont.parentNode;
            while (elmTable.nodeName != 'TABLE') {
                elmTable = elmTable.parentNode;
            }
            var arTD = elmTable.getElementsByTagName('td');
            if (arTD.length) {
                var elmTD = arTD[arTD.length - 1];
                var arNext = elmTD.getElementsByTagName('a');
                if (arNext.length) {
                    var elmNext = arNext[0];
                    var elmTextNext = document.createElement('center');
                    elmTextNext.innerHTML = '<p style="font-size: ' +
                        'xx-large; margin-bottom: 4em;"><b><a href="' +
                        elmNext.href + '">More Results&nbsp;&nbsp;' +
                        '&rarr;</a></b></p>';
                    elmTable.parentNode.replaceChild(elmTextNext, 
                        elmTable);
                }
            }
            break;
        }
    }

    // remove bottom ads
    var arCenter = document.getElementsByTagName('center');
    if (arCenter.length > 1) {
        var elmCenter = arCenter[1];
        elmCenter.parentNode.removeChild(elmCenter);
        elmCenter = arCenter[0];
        for (var i = 0; i < 4; i++) {
            elmCenter.innerHTML = elmCenter.innerHTML.replace(/<br>/, '');
        }
    }

}

document.forms.namedItem('f') && accHomePage();
document.forms.namedItem('gs') && accSearchResults();
removeFontTags();
zoomStyle();

Running the Hack

After installing the user script (Tools→Install This User Script), go to http://www.google.com. The normally spartan search form has been magnified and simplified even further, as shown in Figure 4-47.

Google home page, zoomed
Figure 4-47. Google home page, zoomed

Accessibility studies have shown that low-vision users have an easier time reading light text on a dark background, so therefore the page is displayed as white-on-navy. Unvisited links are displayed in yellow; visited links are displayed in light green. The hack removes several elements from the page, including the Advanced Search link, plus any advertisements for Google services or other messages that occasionally appear below the search box.

When you execute a search, the search results are displayed differently, as shown in Figures 4-48 and 4-49, with the following notable differences:

  • The entire page uses the same white-on-navy color scheme we used on the home page.

  • The Google logo in the top-left corner is displayed as plain text instead of as an image.

  • The top search form no longer includes the Advanced Search option.

  • The sponsored links along the top and right are gone.

  • The number of results is displayed much larger than before, and in the same white-on-navy color scheme.

  • Links to search results pages are displayed in yellow (or green, if you’ve already visited that page). Other links within each search result, such as the “Cached” and “Similar pages” links, are displayed in white.

  • The “Goooooooogle” navigation bar to see more results is replaced by a simple link titled “More results.”

  • The search box at the bottom of the page is gone.

Google search results, zoomed
Figure 4-48. Google search results, zoomed
Bottom of Google search results, zoomed
Figure 4-49. Bottom of Google search results, zoomed

If you click the Images link at the top of the page to search for the same keywords in Google Image Search, you will see that the image search results have been similarly hacked, as shown in Figure 4-50.

Google image results, zoomed
Figure 4-50. Google image results, zoomed

As with the web search results, the top navigation has been simplified, the number of results is more prominent, and the “Goooooooogle” navigation bar has been replaced by a single “More results” link that moves to the next page of images. The image thumbnails themselves cannot be magnified, since Google provides them only in a specific size.

Mark Pilgrim

Search for Lyrics on Google

Use info from the current or selected track to search for lyrics pages via Google in Safari.

There are tons of lyrics sites on the Web that are ready to provide you with the words to your favorite songs. This hack Googles for the lyrics of the currently playing or selected song in iTunes. You’ll be singing along by the second verse.

Tip

This hack uses the AppleScript scripting language for Mac OS X.

The Code

This script gets the name and artist of the currently playing track (or, if no track is playing, the selected track), tidies up the text, and incorporates it into a URL that is then used by Safari to search Google for sites containing the lyrics of that particular track:

-- base of the URL string, includes the term "lyrics"
property baseURL : "http://www.google.com/search?q=lyrics+"

tell application "iTunes"

  -- get a reference to playing or selected track
  if player state is not stopped then
    set theTrack to current track
  else if selection is not {} then
    set theTrack to (item 1 of selection)
  else
    display dialog "Nothing is playing or selected." buttons {"Cancel"} \xc2 
      default button 1 with icon 0
  end if
  
  -- get the name and artist and replace "bad" characters
  tell theTrack
    set nom to my fixChars(name)
    set art to my fixChars(artist)
  end tell
  
  -- assemble URL string, replace spaces with "+"
  set theURL to (baseURL & \xc2 
    (my replace_chars((art & "+" & nom), " ", "+"))) as text

  my open_location(theURL)
  
end tell

on fixChars(a)
  set myDelims to {"!", "@", "#", "$", "%", "^", "&", "*",\xc2 
    "(", ")", "-", "-", "+", "=", ":", ";", "'", ",", ".", "/", \xc2 
    "<", ">", "?", "{", "}", "[", "]"}
  repeat with curDelim in myDelims
    set AppleScript's text item delimiters to curDelim
    set s to every text item of a
    set AppleScript's text item delimiters to {""}
    set a to s as string
  end repeat
  return a
end fixChars

on replace_chars(txt, srch, repl)
  set AppleScript's text item delimiters to the srch
  set the item_list to every text item of txt
  set AppleScript's text item delimiters to the repl
  set txt to the item_list as string
  set AppleScript's text item delimiters to ""
  return txt
end replace_chars

to open_location(theURL)
  tell application "Safari"
    activate
    -- un-comment if you want to keep from opening windows:
    if name of window 1 does not start with "Google Search:" then
      make new document at end of documents
    end if
    set URL of document 1 to theURL
  end tell
end open_location

I’ve defined a script property (BaseURL) at the beginning of the script, setting it to the main portion of the Google search URL, along with the start of the query, the word lyrics. The script will add more to the base URL later.

First things first: the script determines whether there is a current track or a selected track and sets the variable sel to a reference to one or the other.

Next, the script gets the artist and name properties of the track referenced by sel. It will use these text strings in building the search URL. Additionally, it removes any undesirable characters that might lead to an incorrect search. The handler fixChars() removes a variety of bad characters from the text string sent to it and returns the cleaned-up text.

Now, if the selected/playing song is “Third Uncle” by Brian Eno, the assembled theURL variable will contain a string that looks like this:

http://www.google.com/search?q=lyrics+Brian+Eno+Third+Uncle

All that’s left to do is to send the theURL string to Safari, using the handler open_location(). If the current front window in Safari doesn’t already have a Google search result in it, a new window will be opened; otherwise, the same window is used to load the Google result.

Tip

The same effect is used in “AMG EZ Search” [Hack #79 in iPod and iTunes Hacks].

The Google search results page will (hopefully) contain a whole bunch of links to lyric pages for your song, as shown in Figure 4-51.

Wow, over 3,000 results!
Figure 4-51. Wow, over 3,000 results!

Running the Hack

Enter the code into Script Editor. Save this script in your iTunes Scripts folder as a compiled script (set the File Format in the Save As... dialog to Script) and name it Google Lyric Search.

The script will target the currently playing track; if no track is playing, it will target a selected track. Using the Artist and the Song Name, a Google search URL will be constructed and sent to Safari.

If you want to emulate Google’s I’m Feeling Lucky button, use the following as your baseURL at the start of the script:

http://www.google.com/search?btnI=I'm+Feeling+Lucky&q=lyrics+

Remember: results are not always fruitful...especially on instrumental tracks. (Da-dum!)

Doug Adams

Get Google Hacks, 3rd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.