Created
October 24, 2009 12:53
-
-
Save yujiorama/217533 to your computer and use it in GitHub Desktop.
This file contains 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
// ==UserScript== | |
// @name Amazon add cart link to search result | |
// @namespace http://d.hatena.ne.jp/yujiorama | |
// @description Amazon add cart link to search result | |
// @include http://www.amazon.co.jp/s/* | |
// @include https://www.amazon.co.jp/s/* | |
// ==/UserScript== | |
var subscription_id = 'XXXXXXXXXXXXXXX'; | |
var associate_id = 'XXXXXXXX-22'; | |
function setAttribute(node, opt) { | |
for (var x in opt) { | |
node.setAttribute(x, opt[x]); | |
} | |
return node; | |
} | |
function createNode(tag, opt) { | |
var n = document.createElement(tag); | |
n = setAttribute(n, opt); | |
return n; | |
} | |
function createHiddenNode(name, value) { | |
return createNode('input', {'type': 'hidden', 'name': name, 'value': value}); | |
} | |
( | |
function() { | |
if (console) {console.log('start: addcartbutton');} | |
var items = document.evaluate("//div[@class='productTitle']/a[position()=1]", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null); | |
try { | |
for (var i = 0; i < items.snapshotLength; ++i) { | |
var node = items.snapshotItem(i); | |
var href = node.getAttribute('href'); | |
var asin = href.match(/\/([0-9A-Z]{10})\//); | |
if (asin) { | |
if (console) {console.log('asin: ' + asin[1]);} | |
var f = document.createElement("form"); | |
f = setAttribute(f, {'method': 'post', 'action': '/gp/aws/cart/add.html', 'target': '_blank'}); | |
var asinnode = createHiddenNode('ASIN.1', asin[1]); | |
var qnode = createHiddenNode('Quantity.1', '1'); | |
var atagnode = createHiddenNode('AssociateTag', associate_id); | |
var subscnode = createHiddenNode('SubscriptionId', subscription_id); | |
var button = createNode('input', {'type': 'image', 'name': 'submit.add-to-cart', 'src': 'http://rcm-images.amazon.com/images/G/09/extranet/associates/buttons/remote-buy-jp1.gif', 'alt': 'カートに追加'}); | |
node.parentNode.appendChild(f); | |
f.appendChild(asinnode); | |
f.appendChild(qnode); | |
f.appendChild(atagnode); | |
f.appendChild(subscnode); | |
f.appendChild(button); | |
} | |
} | |
} | |
catch (e) { | |
if (console) { console.log('error: ' + e);} | |
dump( 'Error: Document tree modified during iteraton ' + e ); | |
} | |
if (console) {console.log('end: addcartbutton');} | |
} | |
)(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment