Skip to content

Instantly share code, notes, and snippets.

@luisfavila
Created July 2, 2022 00:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luisfavila/5cf3b8f81968be1355c1b98a9dfb97c7 to your computer and use it in GitHub Desktop.
Save luisfavila/5cf3b8f81968be1355c1b98a9dfb97c7 to your computer and use it in GitHub Desktop.
Calculate money spent on amazon

This script calculates your spent on amazon. Follow the instructions and run on the console of your browser's inspector tools. Currently supports € and £, calculates in POUNDS. Make sure to update the exchange rate.

  1. Navigate to https://www.amazon.co.uk/cpe/yourpayments/transactions
  2. Open developer tools, go to console and run:
function sumCurrentPage() {
  const table = $('.a-box-inner.a-padding-none'); 
  return Array
    .from(table.querySelectorAll('.a-column:nth-of-type(2) > span'))
    .map(v => {
      if (v.innerHTML.indexOf('£')) {
        return Number.parseFloat(v.innerHTML.replace('£', '')) || 0;
      }
      else if (v.innerHTML.indexOf('EUR')) {
        return (Number.parseFloat(v.innerHTML.replace('EUR ', '')) || 0) * 0.863816;
      }
      return 0;
    })
    .filter(v => !!v)
    .reduce((total, v) => total - v, 0);
}

(async () => {
  const sleep = (ms) => new Promise((resolve) => window.setTimeout(resolve, ms));
  
  let previous = Array.from(document.querySelectorAll('span')).find(el => el.textContent.toLowerCase().includes('previous page')).querySelector('input');
  while(previous) {
    previous.click();
    await sleep(2000);
    previous = Array.from(document.querySelectorAll('span')).find(el => el.textContent.toLowerCase().includes('previous page')).querySelector('input');
  }
  
  let total = sumCurrentPage();
  let next = Array.from(document.querySelectorAll('span')).find(el => el.textContent.toLowerCase().includes('next page')).querySelector('input');
  while(next) {
    next.click();
    await sleep(2000); 
    next = Array.from(document.querySelectorAll('span')).find(el => el.textContent.toLowerCase().includes('next page')).querySelector('input');
    total += sumCurrentPage();
  }
  console.log(`Total spent: £${Math.ceil(total)}`);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment