Created
April 15, 2009 13:49
-
-
Save satyr/95774 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const AmazonView = ''+<><![CDATA[ | |
<style> | |
ol, ul {list-style: none; padding: 0; margin: 0} | |
img {float: right; margin: 0 0 0.5em 0.5em} | |
kbd {text-decoration: underline; text-transform: uppercase} | |
.item {clear: right} | |
kbd, .price {font-weight: bolder} | |
.price, .author, .label, .feature {font-size: 88%} | |
.author:before {content: "by "} | |
.feature:before {content: "\2022 "} | |
</style> | |
Found <b>${numitems}</b> item{if numitems > 1}s{/if} on | |
<a href="http://www.amazon.com/">Amazon.com</a> matching <b>${query}</b>. | |
<ol> | |
{for item in items} | |
<li class="item"> | |
{if item.image} | |
<a href="${item.url}"><img src="${item.image.src}" border="0" | |
height="${item.image.height}" width="${item.image.width}"/></a> | |
{/if} | |
<kbd>${item.key}</kbd> | |
<a href="${item.url}" accesskey="${item.key}">${item.title}</a> | |
{if item.price} | |
<nobr class="price"> | |
${item.price.amount} (${item.price.currency}) | |
</nobr> | |
{/if} | |
{if item.author} | |
<div class="author">${item.author}</div> | |
{/if} | |
{if item.label} | |
<div class="label">${item.label}</div> | |
{/if} | |
<ul> | |
{for feature in item.features} | |
<li class="feature">${feature}</li> | |
{/for} | |
</ul> | |
</li> | |
{/for} | |
</ol> | |
]]></>, | |
noun_type_amazon_com_index = { | |
_name: "Amazon.com index", | |
list: [{text: s, data: d, summary: s} for ([s, d] in Iterator({ | |
All: 0, | |
Apparel: 15683091, | |
Automotive: 15690151, | |
Baby: 3760931, | |
Beauty: 11055981, | |
Books: 1000, | |
Classical: 301668, | |
DigitalMusic: 301668, | |
DVD: 130, | |
Electronics: 493964, | |
GourmetFood: 3580501, | |
Grocery: 10746371, | |
HealthPersonalCare: 3760931, | |
HomeGarden: 1063498, | |
Industrial: 228013, | |
Jewelry: 3880591, | |
Kitchen: 1063498, | |
Magazines: 44264011, | |
Merchants: 493964, | |
Miscellaneous: 349028011, | |
Music: 301668, | |
MusicalInstruments: 12923151, | |
OfficeProducts: 1084128, | |
OutdoorLiving: 1063498, | |
PCHardware: 493964, | |
PetSupplies: 13900811, | |
Photo: 13900861, | |
Shoes: 3375301, | |
Software: 409488, | |
SportingGoods: 1036682, | |
Tools: 228013, | |
Toys: 130, | |
VHS: 404272, | |
Video: 130, | |
VideoGames: 44263011, | |
Watches: 378516011, | |
Wireless: 408234, | |
WirelessAccessories: 408234, | |
}))], | |
default: function() this.list[0], | |
suggest: function(txt, htm, cb, sx) { | |
if(sx || !txt) return []; | |
try { var {list} = this, sorter = [], re = RegExp(txt, "i") } catch (e) {} | |
// prefering earlier matches | |
if (re) for (var i = 0, l = list.length; i < l; ++i) { | |
var sg = list[i], x = sg.text.search(re); | |
if(x >= 0) sorter[x * l + i] = sg; | |
} | |
if (sorter.length) { | |
var ss = sorter.reduce(function(a, s)(a.push(s), a), []), [s] = ss; | |
this.default = function() s; | |
return ss; | |
} | |
// random suggestions to help user recognize available options | |
var r = Math.random() * list.length | 0; | |
return list.slice(r).concat(list.slice(0, r)); | |
}, | |
}; | |
(function(){{} | |
const URL = "http://www.amazon.com/", Link = "Amazon.com".link(URL); | |
CmdUtils.CreateCommand({ | |
name: "amazon", | |
icon: URL + "favicon.ico", | |
description: "Searches " + Link + " for items matching your words.", | |
takes: {"search terms": noun_arb_text}, | |
modifiers: {in: noun_type_amazon_com_index}, | |
execute: function({text}, {in: {data}}) { | |
Utils.openUrlInBrowser(URL + "s/ref=nb_ss_gw" + | |
Utils.paramsToString({ | |
url: "node=" + data, | |
"field-keywords": text, | |
})); | |
}, | |
preview: function(previewBlock, {text, summary}, {in: {text: searchIndex}}) { | |
if (!text) { | |
previewBlock.innerHTML = this.description; | |
return; | |
} | |
previewBlock.innerHTML = | |
"Searching " + Link + " for books matching <b>" + summary + "</b>."; | |
CmdUtils.previewAjax(previewBlock, { | |
type: "GET", | |
url: "http://ecs.amazonaws.com/onca/xml", | |
data: { | |
Service: "AWSECommerceService", | |
AWSAccessKeyId: "08WX39XKK81ZEWHZ52R2", | |
Version: "2008-08-19", | |
Operation: "ItemSearch", | |
Condition: "All", | |
Merchant: "All", | |
ResponseGroup: "ItemAttributes,Images", | |
SearchIndex: searchIndex, | |
Keywords: text, | |
}, | |
dataType: "xml", | |
error: function(xhr, info) { | |
previewBlock.innerHTML = ( | |
"Error searching " + Link + ".<br/>" + | |
"<em>" + xhr.status + " " + xhr.statusText + " (" + info + ")</em>"); | |
}, | |
success: function(responseData) { | |
responseData = jQuery(responseData); | |
var errorMessage = responseData.find("Error > Message").text(); | |
if (errorMessage) { | |
previewBlock.innerHTML = <em>{errorMessage}</em>.toXMLString(); | |
return; | |
} | |
const MAX_RESULTS = 35; | |
function iter(itemIndex) { | |
var itemDetails = jQuery(this), | |
itemAttrs = itemDetails.find("ItemAttributes"), | |
listPrice = itemAttrs.find("ListPrice"), | |
smallImage = itemDetails.find("SmallImage:first"); | |
return { | |
title: itemAttrs.find("Title").text(), | |
url: itemDetails.find("DetailPageURL").text(), | |
key: (itemIndex + 1).toString(MAX_RESULTS + 1), | |
author: (itemAttrs.find("Author") | |
.map(function() this.textContent).get().join(', ')), | |
features: (itemAttrs.find("Feature") | |
.map(function() this.textContent).get()), | |
label: itemAttrs.find("Label").text(), | |
price: listPrice.length && { | |
amount: listPrice.find("FormattedPrice").text(), | |
currency: listPrice.find("CurrencyCode").text(), | |
}, | |
image: smallImage.length && { | |
src: smallImage.find("URL").text(), | |
height: smallImage.find("Height").text(), | |
width: smallImage.find("Width").text(), | |
} | |
}; | |
} | |
var items = (responseData.find("Items > Item").slice(0, MAX_RESULTS) | |
.map(iter).get()); | |
previewBlock.innerHTML = CmdUtils.renderTemplate( | |
//jQuery("#amazon-search", feed.dom).html(), | |
AmazonView, | |
{ query: summary, | |
numitems: responseData.find("Items > TotalResults").text(), | |
items: items}); | |
} | |
}); | |
} | |
}); | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment