|
// ==UserScript== |
|
// @name Aliexpress_orders_ohuf |
|
// @namespace http://tampermonkey.net/ |
|
// @version 2 |
|
// @description extract Aliexpress orders in a sensible way |
|
// @author ohuf |
|
// @match https://trade.aliexpress.com/orderList.htm* |
|
// @grant unsafeWindow |
|
// @grant GM_xmlhttpRequest |
|
// @grant GM_setClipboard |
|
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js |
|
// ==/UserScript== |
|
|
|
|
|
// Declare an array called data to hold each row of data to store to the clipboard |
|
var data = []; |
|
|
|
// Loop through each of the order div wrappers |
|
$(".order-item-wraper").each(async (ind, el)=>{ |
|
// Declaring a variable to hold the GST rate of 10% set on all Aliexpress invoices |
|
let GSTrate = 0.1; |
|
|
|
// Store information about the order in a orderInfo object |
|
let orderInfo = { |
|
id: $(el).find(".order-info .first-row .info-body ").text().trim(), |
|
status: $(el).find(".order-status .f-left").text().trim(), |
|
orderCurPrice: $(el).find(".amount-num").text().replace(',', '.').trim(), |
|
orderDate: $(el).find(".order-info .second-row .info-body").text().trim(), |
|
sellerName: $(el).find(".store-info .first-row .info-body").text().trim(), |
|
}; |
|
|
|
let currency = orderInfo.orderCurPrice.slice(0,1); |
|
switch(currency){ |
|
case "€": |
|
currency = "EUR"; |
|
break; |
|
case "$": |
|
currency = "USD"; |
|
break; |
|
default: |
|
currency = currency; // yes, I know.... |
|
} |
|
|
|
let order_Amount = orderInfo.orderCurPrice.slice(2,orderInfo.orderCurPrice.length); |
|
let orderEURAmount = parseFloat(order_Amount); //.text().replace(',', '.') |
|
//console.log("PRE " + orderInfo.id + " -> orderEURAmount= " + orderEURAmount); |
|
let order_date = new Date(orderInfo.orderDate); |
|
let order_ISODate = order_date.toISOString(); |
|
|
|
let USDtoEUR = 1; |
|
let start_date = new Date(orderInfo.orderDate); |
|
// now we step back through dates, until we can get a valid forex rate. Maximum 10 days to avoid bank holidys, exchange closings, etc... |
|
if(currency != "EUR"){ |
|
let i=0; |
|
do{ |
|
// Convert the date to iso format |
|
let isoDate2 = start_date.toISOString().slice(0,10); |
|
// Set the previous date for the forex api request (because worst case, there's no forex value for 'today') |
|
let prev = new Date(isoDate2); |
|
prev.setDate(prev.getDate()-1); |
|
let isoDate1 = prev.toISOString().slice(0,10); |
|
|
|
// Setup the parameters and end point for the api request |
|
let params = { |
|
date1: "start_at="+isoDate1, |
|
date2: "end_at="+isoDate1, |
|
base: "base=USD", |
|
currency: "symbols=EUR" |
|
} |
|
// Make the API request |
|
|
|
let endPoint = "https://api.exchangeratesapi.io/history?"+params.date1+"&"+params.date2+"&"+params.base+"&"+params.currency; |
|
|
|
let response = await fetch(endPoint); |
|
let json = await response.json(); |
|
|
|
//Debug: |
|
//console.log("DEBUG order: " + orderInfo.id + " APIdate=" + isoDate1 + "("+ start_date.getDay() +")" + " orderDate=" + orderInfo.orderDate); |
|
//console.log("i=" + i); |
|
//console.log(endPoint); |
|
//console.log(json); |
|
|
|
// Pluck the EUR exchange rate off of the json response |
|
try { |
|
USDtoEUR = json.rates[isoDate1].EUR; |
|
i = 10; |
|
} |
|
catch(err) { |
|
console.log("Exception!"); |
|
i++; |
|
} |
|
// --- End of API request --- |
|
|
|
start_date.setDate(start_date.getDate()-1); |
|
} while(i < 10); |
|
}else{ |
|
//console.log("price is already in target currency: no API call necessary."); |
|
} |
|
|
|
orderInfo["USDtoEURForex"] = USDtoEUR; |
|
orderEURAmount = (order_Amount * USDtoEUR).toFixed(2); |
|
//console.log(USDtoEUR); |
|
//console.log("POST orderEURAmount= " + orderEURAmount); |
|
|
|
// Loop through the order body div wrapper |
|
$(el).find(".order-body").each((i,e)=>{ |
|
|
|
// Loop through each of the products in the order |
|
$(e).find(".product-sets").each((i,e)=>{ |
|
// Clone the orderInfo object into an object called row |
|
let row = JSON.parse(JSON.stringify(orderInfo)); |
|
// Add in the product title, price and quantity ordered to the row |
|
row["rowType"] = "product"; |
|
row["productTitle"] = $(e).find(".product-title").text().trim(); |
|
row["productPrice"] = parseFloat($(e).find(".product-amount span:first()").text().replace(',', '.').trim().slice(1).trim()); |
|
if(currency == "USD"){ |
|
row["productEURAmount"] = (row.productPrice * USDtoEUR).toFixed(2); |
|
} else { |
|
row["productEURAmount"] = row.productPrice.toFixed(2); |
|
} |
|
row["productQuantity"] = $(e).find(".product-amount span:eq(1)").text().trim().slice(1); |
|
row["productProperty"] = $(e).find(".product-property span:eq(1)").text().trim().slice(1).trim(); |
|
|
|
row["orderStatus"] = orderInfo.status; |
|
row["orderDate"] = orderInfo.orderDate; |
|
row["ISODate"] = start_date.toISOString().slice(0,10);; |
|
row["orderCurPrice"] = orderInfo.orderCurPrice; |
|
row["orderCurrency"] = currency; |
|
row["orderAmount"] = order_Amount; |
|
row["orderEURAmount"] = orderEURAmount; |
|
// Push row in the dataTable |
|
data.push(row); |
|
}); |
|
}); |
|
|
|
}); |
|
|
|
|
|
// Create a button to click at the top of the order list page which will load the product details to the clip board |
|
$('#mybutton').one('click', function(){ |
|
var r=$('<input/>').attr({ |
|
type: "button", |
|
id: "field", |
|
value: 'LOAD CSV' |
|
}); |
|
$("body").append(r); |
|
}); |
|
$('<button/>', { |
|
text: "LOAD", //set text 1 to 10 |
|
id: 'csvBtn', |
|
click: function () { |
|
$("#csvBtn").text("Loading..."); |
|
var s = "";// "rowType\t id\t ISODate\t orderStatus\t orderCurrency\t orderAmount\t "EUR"\t orderEURAmount\t productTitle\t productProperty\t productQuantity\t orderCurrency\t productPrice\t "EUR"\t productEURAmount\t sellerName\t USDtoEURForex\t orderDate\t orderURL" |
|
Promise.all(data).then(() => { |
|
data.forEach(e=> { |
|
if (e.rowType === "product") { |
|
s += e.rowType + "\t"; |
|
s += e.id + "\t"; |
|
s += e.ISODate + "\t"; |
|
s += e.orderStatus + "\t"; |
|
s += e.orderCurrency + "\t"; |
|
s += e.orderAmount + "\t"; |
|
s += "EUR" + "\t"; |
|
s += e.orderEURAmount + "\t"; |
|
s += e.productTitle + "\t"; |
|
s += e.productProperty + "\t"; |
|
s += e.productQuantity + "\t"; |
|
s += e.orderCurrency + "\t"; |
|
s += e.productPrice + "\t"; |
|
s += "EUR" + "\t"; |
|
s += e.productEURAmount + "\t"; |
|
s += e.sellerName + "\t"; |
|
s += e.USDtoEURForex + "\t"; |
|
s += e.orderDate + "\t"; |
|
s += "https://trade.aliexpress.com/order_detail.htm?orderId=" + e.id + "\t"; |
|
s += "\n"; |
|
} |
|
}); |
|
GM_setClipboard (s); |
|
$("#csvBtn").text("Loaded to clipboard"); |
|
}); |
|
} |
|
|
|
}).appendTo("#appeal-alert"); |
|
|
|
|
|
function test(data){ return data;} |
|
|
Nice one Oliver
Do you mind to give me a hint where to put some localizations in like Date format currency because excel interpretes US dates wringly on a european machine same with dot instead if comma