Skip to content

Instantly share code, notes, and snippets.

@destroytoday
Last active March 22, 2021 18:39
Show Gist options
  • Save destroytoday/6706265 to your computer and use it in GitHub Desktop.
Save destroytoday/6706265 to your computer and use it in GitHub Desktop.
Bookmarklet to scrape Amazon book pages
var imgRegexp = new RegExp("(http://ecx\.images-amazon\.com\/images\/[A-Z]\/[A-Za-z0-9%]+)\..+(\.jpg)");
var doc = document
, obj = { meta: [], taxonomy: [] };
// ------------------
// cover asset
// ------------------
var img = doc.querySelector('#main-image, #imgBlkFront');
if (img) obj.assets = [img.src.replace(imgRegexp, "$1$2")];
// ------------------
// title
// ------------------
var title = doc.querySelector('#btAsinTitle, h1#title');
if (title) obj.title = title.innerText;
// ------------------
// author
// ------------------
var author = doc.querySelector('.contributorNameTrigger, .contributorNameID, .author');
if (author) obj.taxonomy.push({ key: 'Author', values: [author.innerText.replace('(Author)', '').replace(/^\s+|\s+$/g, '')] });
// ------------------
// body
// ------------------
var body = doc.getElementById('bookDesc_iframe') || doc.getElementById('postBodyPS');
if (body && body.contentDocument) {
obj.body = body.contentDocument.getElementById('iframeContent').innerText;
} else if (body) {
obj.body = body.innerText;
};
// ------------------
// link meta
// ------------------
obj.meta.push({ key: 'link', value: window.location.origin + window.location.pathname });
// ------------------
// tags
// ------------------
var raw_tags = doc.querySelectorAll('#SalesRank li b');
var tags = [];
[].forEach.call(raw_tags, function (tag) {
tags.push(tag.innerText.replace(/^\s+|\s+$/g, ''));
});
obj.taxonomy.push({ key: 'Tags', values: tags });
// ---------------------
// serialize URL params
// ---------------------
serialize = function(obj) {
var str = [];
var valueName, value;
for(var prop in obj) {
if (prop == 'meta' || prop == 'taxonomy') {
[].forEach.call(obj[prop], function (item) {
if (prop == 'taxonomy') {
valueName = 'values';
value = item.values.join(",");
} else {
valueName = 'value';
value = item.value;
}
str.push(prop + "[][key]=" + encodeURIComponent(item.key));
str.push(prop + "[][" + valueName + "]=" + encodeURIComponent(value));
});
} else if (prop == 'assets') {
str.push(prop + "[]=" + encodeURIComponent(obj[prop][0]));
} else {
str.push(prop + "=" + encodeURIComponent(obj[prop]));
}
}
return str.join("&");
}
window.open("http://manage.siteleaf.dev:1337/pages/5245dbf3ef75ac50e900000c/posts/new?" + serialize(obj));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment