Created
February 27, 2022 04:52
-
-
Save nestedfunction/c81039af4e2c317972cb9cf6450f9800 to your computer and use it in GitHub Desktop.
AliExpress Orders to CSV
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
// ==UserScript== | |
// @name AliExpress Orders to CSV "DragonCity" V2 | |
// @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== | |
// Thanks for your scripts : https://gist.github.com/naixx/9929f308bfc0aac649d7c48c4a510737 | |
function parseDate(txt) | |
{ | |
// 012345678901234567 | |
// 10:31 May. 21 2019 | |
// var d = new Date(year, month (0-11), day, hours, minutes, seconds, milliseconds); | |
return new Date(txt.slice(14,18),("Jan.Feb.Mar.Apr.May.Jun.Jul.Aug.Sep.Oct.Nov.Dec.".indexOf(txt.slice(6,10)))/4, txt.slice(11,13), txt.slice(0,2), txt.slice(3,5),0,0); | |
} | |
var listTrack = new Array(); | |
var totalTrack = 0; | |
var dt = 0; | |
var orders = []; | |
var items = []; | |
var reqs = []; | |
$(".order-item-wraper").each((ind, el)=>{ | |
var products = []; | |
$(el).find(".order-body").each((i,e)=>{ | |
$(e).find(".product-sets").each((i,e)=>{ | |
let obj = { | |
title: $(e).find(".product-title").text().trim(), | |
price: parseFloat($(e).find(".product-amount span:first()").text().trim().slice(1).trim()), | |
amount: $(e).find(".product-amount span:eq(1)").text().trim().slice(1) | |
}; | |
products.push(obj); | |
// console.log(obj); | |
}); | |
// console.log(products); | |
}); | |
}); | |
getTrackingNumber = function(data){ | |
//console.log(data); | |
console.log(listTrack.length+'/'+totalTrack); | |
var orderId = data.cainiaoUrl.split('=')[1]; | |
var trackId = data.tracking[0].mailNo; | |
var oneUrl = 'https://www.17track.net/fr/track?nums='+trackId; | |
var status = data.tracking[0].keyDesc; | |
listTrack.push(trackId); | |
if(data.tracking[0].traceList) { | |
dt = new Date(data.tracking[0].traceList[0].eventTime); | |
status += '<br/>'+dt.toLocaleString()+'<br/>'+data.tracking[0].traceList[0].desc; | |
} | |
jQuery('.button-logisticsTracking[orderid='+orderId+']') | |
.before('<td class="tracking_number" style="background-color: red;color: white;font-weight: bold;padding:0px">'+data.tracking[0].mailNo+'</td>') | |
.before('<span class="tracking_status" style="color: black;font-weight: bold;font-size: 13px;">'+status+'</span>'); | |
}; | |
(function() { | |
'use strict'; | |
function getAllTrackingNumber() { | |
console.log("Search orders"); | |
listTrack.length=0; | |
totalTrack = jQuery('.button-logisticsTracking').length; | |
if(totalTrack > 0) { | |
console.log("Total "+totalTrack); | |
jQuery('.button-logisticsTracking').each(function() { | |
var orderId = jQuery(this).attr('orderid'); | |
jQuery.ajax({ | |
url:document.location.protocol+'//ilogisticsaddress.aliexpress.com/ajax_logistics_track.htm?orderId='+orderId+'&callback=getTrackingNumber', | |
dataType:'jsonp' | |
}); | |
}); | |
} | |
} | |
getAllTrackingNumber(); | |
})(); | |
var data = []; | |
var options = { weekday: undefined, year: 'numeric', | |
month: '2-digit', day: '2-digit', | |
hour: '2-digit', minute: '2-digit'}; | |
$(".order-item-wraper").each(async (ind, el)=>{ | |
var hasTracking = $(el).find(".button-logisticsTracking ").length > 0; | |
let order = { | |
id: $(el).find(".order-info .first-row .info-body ").text().trim(), | |
orderPriceUS: $(el).find(".amount-num").text().trim(), | |
orderDate: parseDate($(el).find(".order-info .second-row .info-body").text().trim()).toLocaleDateString('nl-NL', options), | |
sellerName: $(el).find(".store-info .first-row .info-body").text().trim(), | |
hasTracking: hasTracking, | |
}; | |
if (hasTracking){ | |
var req = new Promise((resolve, reject) => { | |
GM_xmlhttpRequest({ | |
method: "GET", | |
url: "https://ilogisticsaddress.aliexpress.com/ajax_logistics_track.htm?orderId=" + order.id + "&callback=test", | |
onload:(data)=>{ | |
order.tracking = eval(data.responseText).tracking; | |
order.trackingNumber = order.tracking.map(it=>it.mailNo).join(", "); | |
resolve(order); | |
orders.push(order); | |
}, | |
onerror: () => reject(400) | |
}); | |
}); | |
reqs.push(req); | |
} else{ | |
orders.push(order); | |
} | |
$(el).find(".order-body").each((i,e)=>{ | |
$(e).find(".product-sets").each((i,e)=>{ | |
let row = JSON.parse(JSON.stringify(order)); | |
row["rowType"] = "product"; | |
row["productTitle"] = $(e).find(".product-title").text().trim(); | |
row["product_pic_url"] = $(e).find(".product-left img").attr('src'); | |
row["product_count"] = $(e).find(".product-amount span:nth-child(2)").text().trim().slice(1); | |
row["product_url"] = $(e).find(".product-title .baobei-name").attr('href'); | |
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: "CHARGER", //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.orderDate + "\t"; | |
// s += (e.trackingNumber || ' ') + "\t"; | |
s += e.productTitle + "\t"; | |
s += e.product_count + "\t"; | |
s += "\"=HYPERLINK(" + "\"\"" + e.product_url + "\"\"" + ";" + "IMAGE(" + "\"\"" + e.product_pic_url + "\"\"" + ";3" + "\"))\t"; | |
s += e.orderPriceUS + "\t"; | |
s += e.sellerName + "\t"; | |
s += "\"=HYPERLINK(" + "\"\"" + "https://trade.aliexpress.com/order_detail.htm?orderId=" + e.id + "\"\"" + ";" + "\"\"" + e.id + "\"\"" + "\")\t"; | |
s += "\n"; | |
} | |
}); | |
GM_setClipboard (s); | |
$("#csvBtn").text("COPIÉ"); | |
}); | |
} | |
}).appendTo("#appeal-alert"); | |
function test(data){ return data;} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment