はてなASINページにAmazonの評価ボタンを表示する
「Amazonの評価ボタン」って正式には何て言うんだろう? 「持っています」とか「好き」とか5段階で評価できるあれです。
スクリーンショット:
(上の方に出ているテキストボックスはHatena Asin Info.user.jsによるもの)
// ==UserScript== // @name Add amazon rating buttons to hatena // @namespace http://d.hatena.ne.jp/nozom/ // @include http://d.hatena.ne.jp/asin/* // ==/UserScript== (function() { function getElementsByXPath(xpath, doc, context) { if (doc == null) doc = document; if (context == null) context = doc; var it = doc.evaluate(xpath, context, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null); var nodes = new Array(); var node; while ((node = it.iterateNext()) != null) { nodes.push(node); } return nodes; } function getAmazonRatingDiv(doc) { var divs = doc.getElementsByTagName('div'); for (var i = 0; i < divs.length; i++) { if (divs[i].className == 'bucket') { var h2 = divs[i].getElementsByTagName('h2')[0]; if (h2 && h2.textContent.match(/この商品を評価/)) { return divs[i]; } } } return null; } function appendRating(node, asin) { var url = 'http://www.amazon.co.jp/exec/obidos/ASIN/' + asin; GM_xmlhttpRequest({ method: 'GET', url: url, onload: function(response) { var d = document.createElement('div'); d.innerHTML = response.responseText; var ratingDiv = getAmazonRatingDiv(d); if (ratingDiv) { var iframe = document.createElement('iframe'); iframe.width = 500; iframe.height = 60; iframe.style.border = '0px'; iframe.style.verticalAlign = 'top'; node.appendChild(iframe); var iframedoc = iframe.contentWindow.document; iframedoc.writeln('<head>'); iframedoc.writeln('<base href="' + url + '">'); iframedoc.writeln('<style>h2 {color:#CC6600;font-size:medium;margin:0px 0px 0.25em;}</style>'); iframedoc.writeln('</head>'); iframedoc.writeln('<body>'); iframedoc.writeln('<div class="bucket">' + ratingDiv.innerHTML + '</div>'); iframedoc.writeln('</body>'); iframedoc.close(); } }, }); } if (location.href.match(/asin\/([^/]*)/)) { var asin = RegExp.$1; var divAsinButton = getElementsByXPath('//div[@class="asin-button"]')[0]; if (divAsinButton) { appendRating(divAsinButton, asin); } } })()
XPathとDOMとwritelnが混在しているところが何ともイケてない。