Forked from naixx/Export aliexpress orders to clipboard as csv
Last active
May 8, 2024 11:22
-
-
Save wlei6277/dfe13af8bf77c4cc7dee23fd981abe96 to your computer and use it in GitHub Desktop.
This file contains 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
// ==UserScript== | |
// @name Aliexpress_Billy_edit | |
// @namespace http://tampermonkey.net/ | |
// @version 0.1 | |
// @description try to take over the world! | |
// @author You | |
// @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(), | |
orderPriceUS: $(el).find(".amount-num").text().trim(), | |
orderDate: $(el).find(".order-info .second-row .info-body").text().trim(), | |
sellerName: $(el).find(".store-info .first-row .info-body").text().trim(), | |
}; | |
// Trim the $ and leading white space on orderPrice attribute | |
orderInfo.orderPriceUS = orderInfo.orderPriceUS.slice(2,orderInfo.orderPriceUS.length); | |
// Declare a variable sumProduct to calculate the sum product of the items in the order (needed to calculate the delivery charge) | |
let sumProduct = 0; | |
// Make an API call to get the US & AUD foreign exchange rates | |
// First convert the date to iso format to use a query parameter in the api request | |
// Set the later date for the api request to get the AUD forex | |
let start_date = new Date(orderInfo.orderDate); | |
// The API service does not provide forex data for the weekend, therefore if the start date is a Saturday, Sunday or a Monday set it back 3 days | |
if (start_date.getDay() === 6 || start_date.getDay() === 1 || start_date.getDay() === 2) { | |
start_date.setDate(start_date.getDate()-3); | |
}; | |
// Convert the date to iso format | |
let isoDate2 = start_date.toISOString().slice(0,10); | |
// Set the previous date for the forex api request | |
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="+isoDate2, | |
base: "base=USD", | |
currency: "symbols=AUD" | |
} | |
// 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(); | |
// console.log(endPoint); | |
// console.log(json); | |
// console.log(json.rates[isoDate1].AUD); | |
// Pluck the AUD exchange rate off of the json response | |
let audToUSD = json.rates[isoDate1].AUD; | |
orderInfo["AUDtoUSForex"] = audToUSD; | |
orderInfo["orderPriceAUD"] = orderInfo.orderPriceUS*audToUSD; | |
// 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["productPriceUS"] = parseFloat($(e).find(".product-amount span:first()").text().trim().slice(1).trim()); | |
row["productQuantity"] = $(e).find(".product-amount span:eq(1)").text().trim().slice(1); | |
// Add in the GST rate and amount of GST per order | |
row["GSTrate"] = GSTrate; | |
row["GSTPerProductUS"] = row.GSTrate*row.productPriceUS; | |
// Calculate and add in the AUD product price and GST | |
row["productPriceAUD"] = row.productPriceUS*row.AUDtoUSForex; | |
row["GSTPerProductAUD"] = row.GSTPerProductUS*row.AUDtoUSForex; | |
// Push row in the dataTable | |
data.push(row); | |
// Increment sumProduct by the current row's total | |
sumProduct += row.productPriceUS*row.productQuantity; | |
}); | |
}); | |
// Calculate the delivery charge, combine it with the order information and add it as a row of data | |
let deliveryRow = JSON.parse(JSON.stringify(orderInfo)); | |
deliveryRow["rowType"] = "delivery"; | |
deliveryRow["deliveryChargeUS"] = deliveryRow.orderPriceUS/(1+GSTrate) - sumProduct; | |
deliveryRow["deliveryChargeGSTUS"] = deliveryRow.deliveryChargeUS * (1+GSTrate); | |
deliveryRow["deliveryChargeAUD"] = deliveryRow.deliveryChargeUS * deliveryRow.AUDtoUSForex; | |
deliveryRow["deliveryChargeGSTAUD"] = deliveryRow.deliveryChargeGSTUS * deliveryRow.AUDtoUSForex; | |
console.log(`total order is ${deliveryRow.orderPriceUS}. Delivery divided by order pricce is ${deliveryRow.orderPriceUS/(1+GSTrate)} Sum product is ${sumProduct}. Delivery charge is ${deliveryRow.deliveryChargeUS}`); | |
data.push(deliveryRow); | |
// Calculate the total tax paid, combine it with the order information and add it as a row of data | |
let taxRow = JSON.parse(JSON.stringify(orderInfo)); | |
taxRow["rowType"] = "tax"; | |
taxRow["taxTotalUS"] = taxRow.orderPriceUS - taxRow.orderPriceUS/(1+GSTrate); | |
taxRow["taxTotalAUD"] = taxRow.taxTotalUS * taxRow.AUDtoUSForex; | |
data.push(taxRow); | |
}); | |
// 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 productTitle\t productPriceUS\t productQuantity\t AUDtoUSForex\t productPriceAUD\t GSTrate\t GSTPerProductUS\t GSTPerProductAUD\t orderDate\t orderPriceUS\t orderPriceAUD\t sellerName\t url \n"; | |
Promise.all(data).then(() => { | |
data.forEach(e=> { | |
if (e.rowType === "product") { | |
s += e.rowType + "\t"; | |
s += e.id + "\t"; | |
s += e.productTitle + "\t"; | |
s += e.productPriceUS + "\t"; | |
s += e.productQuantity + "\t"; | |
s += e.AUDtoUSForex + "\t"; | |
s += e.productPriceAUD + "\t"; | |
s += e.GSTrate + "\t"; | |
s += e.GSTPerProductUS + "\t"; | |
s += e.GSTPerProductAUD + "\t"; | |
s += e.orderDate + "\t"; | |
s += e.orderPriceUS + "\t"; | |
s += e.orderPriceAUD + "\t"; | |
s += e.sellerName + "\t"; | |
s += "https://trade.aliexpress.com/order_detail.htm?orderId=" + e.id + "\t"; | |
s += "\n"; | |
} else if (e.rowType === "delivery") { | |
s += e.rowType + "\t"; | |
s += e.id + "\t"; | |
s += "Delivery charge on order (USD): " + "\t"; | |
s += e.deliveryChargeUS + "\t"; | |
s += "Delivery charge on order (AUD): " + "\t"; | |
s += e.deliveryChargeAUD + "\t"; | |
s += "GST on delivery charge (@ 10% rate, USD): " + "\t"; | |
s += e.deliveryChargeGSTUS + "\t"; | |
s += "GST on delivery charge (@ 10% rate, AUD): " + "\t"; | |
s += e.deliveryChargeGSTAUD + "\t"; | |
s += "\n"; | |
} else { | |
s += e.rowType + "\t"; | |
s += e.id + "\t"; | |
s += "Total GST paid on order (@ rate 10%, USD): " + "\t"; | |
s += e.taxTotalUS + "\t"; | |
s += "Total GST paid on order (@ rate 10%, AUD): " + "\t"; | |
s += e.taxTotalAUD + "\t"; | |
s += "\n"; | |
} | |
}); | |
GM_setClipboard (s); | |
$("#csvBtn").text("Loaded to clipboard"); | |
}); | |
} | |
}).appendTo("#appeal-alert"); | |
function test(data){ return data;} | |
Aloha!
I forked your script and simplified it down to the product lines.
I removed the Australian tax and shipping calculations and changed the target currency to Euro.
I also changed the logic for the forex api so it will only be called when the product currency is different from the target currency (e.g. USD->EUR):
You can find it here
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Where is the function in your script for sorted by date ? :)
Thank you in advance.