// ==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=$('').attr({ type: "button", id: "field", value: 'LOAD CSV' }); $("body").append(r); }); $('