Skip to content

Instantly share code, notes, and snippets.

@glowinthedark
Last active September 23, 2024 12:28
Show Gist options
  • Save glowinthedark/5120f7db18f1ba74969072206ed056eb to your computer and use it in GitHub Desktop.
Save glowinthedark/5120f7db18f1ba74969072206ed056eb to your computer and use it in GitHub Desktop.
AliExpress Order History export to CSV
function formatCSVValue(value) {
value = String(value);
// If the value contains a comma, newline, or quote, it needs to be quoted
if (/[,\"\n]/.test(value)) {
// Escape quotes by doubling them
value = value.replace(/"/g, '""');
// Wrap the value in quotes
value = `"${value}"`;
}
return value;
}
function downloadCSVFile(csv_data, fileName) {
CSVFile = new Blob([csv_data], {
type: "text/csv"
});
let a = document.createElement('a');
a.download = fileName;
let url = URL.createObjectURL(CSVFile);
a.href = url;
a.style.display = "none";
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(a.href);
}
function t(el) {
var text = el
&& el.innerText
&& el.innerText
.trim()
.replace(/\s*Copy?:?\s*/i, '')
.replace(',', '.')
.replace('Order ID: ', '')
.replace('Order date: ', '') || '';
return formatCSVValue(text);
}
var items = ["status, date, id, detail, price, quantity, orderLink"];
document.querySelectorAll('.order-item').forEach(el => {
var status = el.querySelector('.order-item-header-status-text');
var date = el.querySelector('.order-item-header-right-info div');
var orderId = el.querySelector('.order-item-header-right-info div:nth-of-type(2)');
var details = el.querySelector('.order-item-content-info-name');
var price = el.querySelector('.order-item-content-info-number div');
var quantity = el.querySelector('.order-item-content-info-number-quantity');
var link = el.querySelector('.order-item-header-right a');
items.push([t(status), t(date), t(orderId), t(details), t(price), t(quantity).replace('x', ''), link ? link.href : '']);
});
console.log(items);
downloadCSVFile(items.join('\n'), "aliexpress_order_history.csv");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment