Last active
September 20, 2023 17:58
-
-
Save oliverswitzer/fc67cca27905461420c2ae7c2a057735 to your computer and use it in GitHub Desktop.
Extract Amazon orders to a csv file
This file contains hidden or 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
// Simply copy and paste this entire snippe into the browser console (Cmd + Option + J opens the console) from the Amazon orders page (https://www.amazon.com/gp/css/order-history?ref_=nav_AccountFlyout_orders) | |
// For now you'll have to navigate to the next page yourself and re-run this on each page. | |
// Once you've finished running this on all relevant order pages, run the following: | |
// | |
// | |
// const storedData = localStorage.getItem('amazonOrdersCSV'); | |
// downloadCSV(storedData); | |
// | |
function currentPageOrders() { | |
const currentOrderEls = [ | |
...document.querySelectorAll('[class*=order-card]'), | |
] | |
return currentOrderEls.map((el) => { | |
return { | |
title: el.querySelectorAll( | |
'.js-shipment-info-container + div .a-link-normal' | |
)[1].innerText, | |
productLink: el.querySelectorAll( | |
'.js-shipment-info-container + div .a-link-normal' | |
)[1].href, | |
total: el.querySelector('[class*=order-total] .value').innerText, | |
dateOrdered: el.querySelector( | |
'.order-info .a-column.a-span3 .value' | |
).innerText, | |
invoiceUrl: el.querySelector( | |
'[class*="order-level-connections"] a:last-child' | |
).href, | |
} | |
}) | |
} | |
function convertToCSV(data) { | |
if (data.length === 0) return '' | |
const header = Object.keys(data[0]).join(',') | |
const rows = data.map((obj) => { | |
return Object.values(obj) | |
.map((val) => { | |
if ( | |
typeof val === 'string' && | |
(val.includes(',') || val.includes('"')) | |
) { | |
// Escape double quotes and surround the value with double quotes if it has a comma or double quote | |
return `"${val.replace(/"/g, '""')}"` | |
} | |
return val | |
}) | |
.join(',') | |
}) | |
return [header, ...rows].join('\n') | |
} | |
function saveToLocalStorage(csvData) { | |
const existingData = localStorage.getItem('amazonOrdersCSV') || '' | |
if (!existingData) { | |
localStorage.setItem('amazonOrdersCSV', csvData) | |
} else { | |
const existingRows = existingData.split('\n') | |
const newRows = csvData.split('\n').slice(1) // Exclude the header from new data | |
const combinedData = [...existingRows, ...newRows].join('\n') | |
localStorage.setItem('amazonOrdersCSV', combinedData) | |
} | |
} | |
function downloadCSV(data) { | |
const blob = new Blob([data], { type: 'text/csv' }) | |
const url = URL.createObjectURL(blob) | |
const a = document.createElement('a') | |
a.href = url | |
a.download = 'orders.csv' | |
a.style.display = 'none' | |
document.body.appendChild(a) | |
a.click() | |
setTimeout(() => { | |
document.body.removeChild(a) | |
URL.revokeObjectURL(url) | |
}, 0) | |
} | |
const csvData = convertToCSV(currentPageOrders()) | |
console.log(csvData) | |
saveToLocalStorage(csvData) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment