Web DevCenter    
 Published on Web DevCenter (http://www.oreillynet.com/javascript/)
 See this if you're having trouble printing code examples


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]
external-search/mode=blended&keyword=");

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

@="c:\\scripts\\AmazonSearch.html"
"contexts"=dword:00000010

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

Hack #69: Create Amazon Associate Links on Your Movable Type Weblog

Integrate Amazon and Movable Type, and you'll be posting about books to your weblog in no time.

Using external tools to integrate Amazon links into your web publishing system is helpful, but ease of use depends on features being built directly into the software you use to publish. That's the thinking behind Movable Type's plug-in system, and MTAmazon is a plug-in that can do much of the integration work for you.

Amazon.com product links, wish lists, and more can be automatically inserted into web sites managed by Movable Type thanks to MTAmazon, a Movable Type plug-in for Amazon Web Services.

Amazon Web Services (AWS) connect Amazon's databases with associate sites, showing search results, buttons to buy products, users' wish lists, and the contents of product categories without requiring the associate to make manual updates. For sites that use Movable Type, the MTAmazon plug-in provides a simple way to make use of AWS.

You can download MTAmazon and get instructions on installation and basic usage from the MTAmazon web site at http://mtamazon.sourceforge.net/.

Provide MTAmazon with a product ID, search string, or the list ID from a Wish List, Listmania! list, or product category, and it will retrieve a wealth of product information and provide Movable Type template tags with which to display it. If you know the ASIN for a product, you can insert a picture of it, the title, the price, and a button to add the item to the Amazon shopping cart.

<MTAmazon search="B00003CWT6" method="Asin" lastn="1">
<img src="<MTAmazonMediumImage>" align="left" border="0" hspace="5" />
<b><MTAmazonTitle></b><br />
<MTAmazonManufacturer>
<p>Amazon Price: <b><MTAmazonSalePrice></b></p>
<form method="POST" 
action="http://www.amazon.com/o/dt/assoc/handle-buy-box=<MTAmazonASIN>">
<input type="hidden" name="asin.<MTAmazonASIN>" value="1">
<input type="hidden" name="tag-value" value="<MTAmazonAssociateID>">
<input type="hidden" name="tag_value" value="<MTAmazonAssociateID>">
<input type="hidden" name="dev-tag-value" value="<MTAmazonDevToken>">
<input type="submit" name="submit.add-to-cart" value="Buy From [RETURN]
Amazon.com">
</form></MTAmazon>

That's great, but it requires you to code the product's ASIN into your templates. The plug-in wouldn't be very useful if you had to change your templates every time you wanted to link to a different product. Fortunately there's a way to use content from Movable Type in the MTAmazon code. For instance, someone writing a book review in Movable Type can store the ISBN of the book in the entry's keyword field and use that ISBN to automatically insert a picture, price, and link to Amazon.com. Simply use the <MTEntryKeywords> tag as your search attribute.

Movable Type typically won't let you use one MT tag as the attribute for another, but MTAmazon will let you fake it with a special tag format. If you use the tag name with square brackets around it, MTAmazon will evaluate the tag and use the output as the attribute value. For example, in the case of our book review:

<MTAmazon search="[MTEntryKeywords]" method="Asin" lastn="1">

MTAmazon can evaluate more complex expressions as well. This example will use the titles of the last five entries as the keywords for a book search:

<MTAmazon search="[MTEntries lastn="5"][MTEntryTitle] [/MTEntries]">

Movable Type tags are available only in templates, so to include links or images in your weblog entries, you'll need another plug-in. Process Tags (http://kalsey.com/2002/08/process_tags_plugin/) will let you use MTAmazon and other MT template tags in your entry bodies. With this plug-in, you can add associate links to books, movies, or games that you mention in your weblog.

<a <MTAmazon search="0425170349" mode="Asin"> href="<MTAmazonLink>"[RETURN]
title="<MTAmazonTitle>"</MTAmazon>>this book</a>

One of the disadvantages to using Web Services to automatically insert associate links is that you may earn a lower commission on sales made through those links. Sales from AWS links earn only a 5 percent commission, while other links can earn up to 15 percent.

To take advantage of MTAmazon but still earn the higher commission, you'll need to replace the product links that MTAmazon generates with your own links. An Amazon associate URL is always in the format:

http://www.amazon.com/exec/obidos/ASIN/ProductASIN/Associate_ID/

MTAmazon has tags for both the product's ASIN and your associate ID. You can use these to construct associate links that will earn you greater commissions:

http://www.amazon.com/exec/obidos/ASIN/<MTAmazonASIN>/<MTAmazonAssociateID>/

— Adam Kalsey


Return to the Web DevCenter.

Copyright © 2009 O'Reilly Media, Inc.