Created
June 29, 2016 23:01
-
-
Save akirattii/1e9849b99b332166995dcd9698aa60ec to your computer and use it in GitHub Desktop.
Snippet for amazon recommend items
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
// manifest.json | |
/* | |
... | |
"permissions": [ | |
"*://*.amazon.co.jp/*", | |
"*://*.amazon.com/*" | |
or | |
"<all_urls>" | |
], | |
... | |
*/ | |
document.querySelector("#btn").onclick = function() { | |
let kwd = document.querySelector("#txt").value; | |
// get a recommend item | |
getAmazonRecommend(kwd, 3, function(err, items) { | |
if (err) { | |
console.error(err); | |
return; | |
} | |
console.log("items:", items); | |
let pane = document.querySelector("#pane"); | |
let len = items.length; | |
for (let i = 0; i < len; i++) { | |
loadImage(items[i].imgsrc, pane); | |
} | |
}); | |
} | |
document.querySelector("#btn2").onclick = function() { | |
let url = document.querySelector("#txt").value; | |
let rootEl = document.getElementById("pane"); | |
loadImage(url, rootEl, function(imgEl) { | |
console.log("imgEl:", imgEl); | |
}); | |
} | |
/** | |
* get amazon recommend items. | |
* @param {String} keyword | |
* @param {Integer} max item's count to get. default is 3. | |
* @param {Function} callback. 'function(err, items){...})' | |
*/ | |
function getAmazonRecommend(kwd, max, cb) { | |
const URL_BASE = "https://www.amazon.co.jp/s/ref=nb_sb_noss_2?__mk_ja_JP=%E3%82%AB%E3%82%BF%E3%82%AB%E3%83%8A&url=search-alias%3Daps&field-keywords="; | |
const TIMEOUT = 10000; | |
let url = URL_BASE + encodeURIComponent(kwd); | |
let xhr = new XMLHttpRequest(); | |
xhr.onload = function() { | |
if (this.readyState === XMLHttpRequest.DONE) { | |
if (this.status === 200) { | |
let els = this.responseXML.querySelectorAll(".s-result-item"); | |
let items = makeItems(els); | |
if (items.length <= 0) { | |
return cb("No items recommended."); | |
} | |
return cb(null, items); | |
} else { | |
return cb(this.statusText); | |
} | |
} | |
}; | |
xhr.ontimeout = function() { | |
return cb("The request for ${url} timed out."); | |
}; | |
xhr.responseType = "document"; | |
xhr.timeout = TIMEOUT; | |
xhr.open("GET", url); | |
xhr.send(null); | |
function makeItems(els) { | |
if (!els || els.length <= 0) return []; | |
let arr = []; | |
let el; | |
let p, img; | |
let title, href, imgsrc; | |
let len = (max >= els.length) ? els.length : max; | |
for (let i = 0; i < len; i++) { | |
el = els[i]; | |
// image url | |
img = el.querySelector("img"); | |
if (img) { | |
imgsrc = img.src; | |
} | |
// title | |
p = el.querySelector(".a-link-normal[title]"); | |
if (!p) continue; | |
title = p.title; | |
// link | |
href = p.href; | |
if (!title || !href) { | |
continue; | |
} | |
// | |
arr.push({ | |
title, | |
href, | |
imgsrc, | |
}); | |
} | |
return arr; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment