Web DevCenter
oreilly.comSafari Books Online.Conferences.
MySQL Conference and Expo April 14-17, 2008, Santa Clara, CA

Sponsored Developer Resources

Web Columns
Adobe GoLive
Essential JavaScript

Web Topics
All Articles
Scripting Languages

Atom 1.0 Feed RSS 1.0 Feed RSS 2.0 Feed

Learning Lab

O'Reilly Book Excerpts: Amazon Hacks

Hacking Amazon

Related Reading

Amazon Hacks
100 Industrial-Strength Tips & Tools
By Paul Bausch

by Paul Bausch

Editor's note: O'Reilly's Amazon Hacks recently released, so we asked author Paul Bausch to select some hacks from the book to highlight here. Find out how to search Amazon from any web page in IE, and how to create Amazon Associate links on your Movable Type weblog. And check back to this space next week for hacks on listing your Amazon items for sale on your own web site, and on creating a wireless wish list.

Hack #11: Search Amazon from Any Web Page in IE

Searching Amazon from any web page can be as simple as highlighting and clicking with an Internet Explorer context menu.

If searching Amazon from your address bar [Hack #10 in Amazon Hacks] isn't your style, you may find it helps to have context. Imagine this: you're reading an article on your favorite news web site about the latest tech trend: overclocking. It sounds like something you'd like to try, and you wonder if there are any books on the subject. You highlight the word, right-click, choose "Search Amazon" from the menu, and a new window opens with your answer. You can make this happen with a little JavaScript and a new registry entry.

The Code

First, the JavaScript. This bit of code will open a new browser window and plug the highlighted text into a URL. This code needs to be in a file somewhere on your computer where it can be accessed from the browser, and won't be deleted in a fit of spring cleaning. You could even create a new folder for it, c:\scripts\. If you have another location in mind, be sure to change any references to c:\scripts\ in the following code.

Create a file called AmazonSearch.html and add this code:

<script language="JavaScript">
var searchURL = new String("http://www.amazon.com/exec/obidos/[RETURN]

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);

Save it in the c:\scripts\ directory.

Next you need to add some information to your Windows registry to let Internet Explorer know where to find AmazonSearch.html, and when exactly it should be executed. You could open up a registry editor and add values manually, but it's just as easy to put the information into a .reg file and add it by double-clicking. Create a new file called AmazonContext.reg and add the following code:

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\MenuExt\[RETURN]
Search Amazon]


TIP: Backslashes, like those in filesystem paths, need to be escaped as double-slashes (\\) in registry entry files.

Save the file on your desktop, double-click it, and confirm that you want to add the new registry information. This setting adds a right-click menu entry called "Search Amazon" that will appear whenever text is highlighted. It also points to the JavaScript file and will execute it when the highlighted entry is clicked.

Running the Hack

Be sure to shut down all running copies of Internet Explorer. Once restarted, try browsing to any web page with text. Highlight any word and click your right mouse button. You should see the new entry as shown in Figure 1-18.

Figure 1-18
Figure 1-18. Search Amazon context entry

Click "Search Amazon" to open a new window with your search results for that term as shown in Figure 1-19.

Figure 1-19
Figure 1-19. Amazon Search Results window

Pages: 1, 2

Next Pagearrow