Skip to content

Instantly share code, notes, and snippets.

@cold-logic
Created May 6, 2016 08:06
Show Gist options
  • Save cold-logic/e4c8a01db4e3b9fb58da7f65e21c9c0e to your computer and use it in GitHub Desktop.
Save cold-logic/e4c8a01db4e3b9fb58da7f65e21c9c0e to your computer and use it in GitHub Desktop.
/*
Parse and display links to items in your Amazon shopping history in a popup window
Useful for sharing stuff with people via email (copy/paste)
Pro user tip: SKUs scanned with Amazon's mobile app show up in this list
https://smile.amazon.com/gp/history?ie=UTF8&ref_=ya_browsing_history
*/
($ => {
// Specify the range of products your interested in
const start = 0; // First product index
const end = 15; // Last product index
// Product boxes
let $sections = $(".a-section.a-spacing-none.asin_container");
// Results array
let products = [];
// Loop over the range
for (let i = start; i < end; i++) {
// Get the product box
let $section = $sections.eq(i);
// Get the product image
let $prodImg = $section.find("img");
// Create the product object
let product = {
// Get the product title
title: $prodImg.attr("title"),
// Generate an absolute link
href: `https://www.amazon.com${$section.find("a").attr("href")}`,
// Get the image URL
image: $prodImg.attr("src")
};
// Get the product information (title, book author, star rating, price)
let $prodInfo = $section.find(".a-section.a-spacing-none");
// Remove title since we get that from the image
$prodInfo = $prodInfo.slice(1, $prodInfo.length);
// Define the product variables
let fields = ["author","rating","price"];
// If there are only two fields (rating and price), remove the author
if ($prodInfo.length == 2) fields.splice(0, 1);
// Loop over the fields
fields.forEach((field, i) => {
product[field] = $prodInfo.eq(i).text().trim();
});
// Push into the products array
products.push(product);
}
// Open a blank window for the output
let logWin = window.open(
"about:blank",
"Amazon Shopping History",
"resizable,scrollbars,status"
);
let ul = document.createElement("ul");
products.forEach(product => {
/* Available product attributes are:
title
author
rating
price
href
image
*/
// Check for an author (if it's a book)
if (product.author) product.title = `${product.title} ${product.author}`;
// Append a list item to the unordered list
ul.appendChild($(`<li><a href="${product.href}">${product.title}</a></li>`)[0]);
});
// Append to the output window's body node
logWin.document.body.appendChild(ul);
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment