Skip to content

Instantly share code, notes, and snippets.

@Snack-X
Created October 14, 2019 15:30
Show Gist options
  • Save Snack-X/87a814f519c04d744311cee478e5b4c3 to your computer and use it in GitHub Desktop.
Save Snack-X/87a814f519c04d744311cee478e5b4c3 to your computer and use it in GitHub Desktop.
Amazon Total Order Calculator
//
// F12 - Paste - Enter
//
// Last Update: 2019-10-15
// Works for: Amazon JP, Amazon UK
//
// Reference
// - https://gist.github.com/koyopro/a480a45712ccf1bf239c
(async function () {
const $text1 = document.createElement('span');
const $text2 = document.createElement('div');
$text2.style.display = 'none';
$text2.style.padding = '32px';
$text2.style.lineHeight = '1.25';
$text2.style.color = '#000';
$text2.style.fontSize = '18pt';
$text2.style.background = '#fff';
const $overlay = document.createElement('div');
$overlay.style.position = 'fixed';
$overlay.style.left = 0;
$overlay.style.top = 0;
$overlay.style.width = '100%';
$overlay.style.height = '100%';
$overlay.style.zIndex = 1000;
$overlay.style.display = 'flex';
$overlay.style.alignItems = 'center';
$overlay.style.justifyContent = 'center';
$overlay.style.backgroundColor = 'rgba(0, 0, 0, .7)';
$overlay.style.color = '#fff';
$overlay.style.fontSize = '32pt';
$overlay.appendChild($text1);
$overlay.appendChild($text2)
document.body.appendChild($overlay);
let yearFrom = document.querySelector('#timePeriodForm select option:last-child').value;
yearFrom = parseInt(yearFrom.match(/^year-(\d+)$/)[1]);
const yearTo = new Date().getFullYear();
async function get(year, page = 1) {
const url = `https://${location.host}/gp/css/order-history?digitalOrders=1&unifiedOrders=1&orderFilter=year-${year}&startIndex=${(page - 1) * 10}`;
const response = await fetch(url);
const html = await response.text();
return html;
}
function parse(html) {
const parser = new DOMParser();
const page = parser.parseFromString(html, 'text/html');
const $orders = page.querySelectorAll('#ordersContainer .order');
const orders = [];
$orders.forEach($order => {
const $id = $order.querySelector('.order-info .actions .value');
const orderNo = $id.innerText.trim();
const $value = $order.querySelector('.order-info .a-column:nth-child(2) .value');
const value = parseFloat($value.innerText.replace(/[^\d.]/g, ''));
orders.push([orderNo, value]);
});
return orders;
}
const totalOrders = [];
for (let year = yearFrom; year <= yearTo; year++) {
let yearOrders = [];
let page = 1;
while (true) {
$text1.innerText = `${year} / Page ${page}`;
const html = await get(year, page);
const pageOrders = parse(html);
if (pageOrders.length === 0) break;
yearOrders = yearOrders.concat(pageOrders);
page += 1;
}
totalOrders.push([year, yearOrders]);
}
$text1.innerText = '';
let currency = v => `${v}`;
if (location.host.endsWith('.co.jp')) currency = v => `¥${v}`;
else if (location.host.endsWith('.com')) currency = v => `\$${v}`;
else if (location.host.endsWith('.co.uk')) currency = v => `£${v}`;
let grandTotal = 0;
$text2.innerHTML = totalOrders.map(([ year, orders ]) => {
const total = orders.map(([ no, value ]) => value).reduce((p, c) => p + c, 0);
grandTotal += total;
return `${year} = ${currency(total)}`;
}).join('<br>');
$text2.innerHTML += `<br>Total = ${currency(grandTotal)}`;
$text2.style.display = 'block';
})().then(() => {}).catch(console.error);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment