Last active
September 12, 2024 14:36
-
-
Save Hegy/39c3a2bb9f649e15c920b7ec8c074ae9 to your computer and use it in GitHub Desktop.
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 Parse Orders Information to CSV | |
| // @namespace http://tampermonkey.net/ | |
| // @version 0.2 | |
| // @description Parse AliExpress order page into a CSV file suitable for spreadsheet use. | |
| // @author Hegy | |
| // @match https://www.aliexpress.com/p/order/index.html* | |
| // @icon https://www.google.com/s2/favicons?sz=64&domain=aliexpress.com | |
| // @grant none | |
| // ==/UserScript== | |
| // 2024-03-26 | |
| (function() { | |
| 'use strict'; | |
| // Function to convert data to CSV format | |
| function convertToCSV(data) { | |
| const header = ['1','2','3','4','Date', 'Order ID', 'Image Formula', 'Name', 'SKU', 'Order URL', 'Product URL', 'Price', 'Quantity', 'Total Price']; | |
| const csv = [header.join('\t')]; | |
| data.forEach(row => { | |
| csv.push(row.join('\t')); | |
| }); | |
| return csv.join('\n'); | |
| } | |
| // Function to extract data from each order item | |
| function extractOrderData(orderItem) { | |
| const orderHeader = orderItem.querySelector('.order-item-header-right-info'); | |
| const dateText = orderHeader.querySelector('div:first-child').innerText.trim(); | |
| const date = dateText.replace('Order date: ', ''); // Extracting date only | |
| const orderIdText = orderHeader.querySelector('div:last-child').innerText.trim(); | |
| const orderId = orderIdText.replace("Order ID: ", "'").replace('\nCopy', ''); // Extracting order ID only | |
| const orderUrl = 'https://www.aliexpress.com/p/order/detail.html?orderId=' + orderId.replace("'", ""); | |
| const contentBody = orderItem.querySelector('.order-item-content-body'); | |
| const url = contentBody.querySelector('a').getAttribute('href').replace('//', 'https://'); | |
| const imageStyle = orderItem.querySelector('.order-item-content-img').getAttribute('style'); | |
| const imageUrl = imageStyle.match(/url\("([^"]+)"/)[1]; | |
| const imageUrlFormula = '=IMAGE("'+ imageUrl + '")'; | |
| const name = orderItem.querySelector('.order-item-content-info-name span[title]').getAttribute('title'); | |
| const sku = orderItem.querySelector('.order-item-content-info-sku').innerText.trim(); | |
| const numberParent = orderItem.querySelector('.order-item-content-info-number'); | |
| const numberFull = numberParent.querySelector('div:first-child').innerText.trim(); | |
| const price = numberFull.replace('US $', '').replace('.', ','); | |
| const quantityFull = orderItem.querySelector('.order-item-content-info-number-quantity').innerText.trim(); | |
| const quantity = quantityFull.replace('x', ''); | |
| const priceSpans = orderItem.querySelectorAll('.order-item-content-opt-price-total span'); | |
| let totalPrice = ''; | |
| priceSpans.forEach(span => { | |
| totalPrice += span.innerText.trim(); | |
| }); | |
| totalPrice = totalPrice.replace('US $', '').replace('.', ','); | |
| return ['', '', '', '', date, orderId, imageUrlFormula, name, sku, orderUrl, url, price, quantity, totalPrice]; | |
| } | |
| // Function to run when button is clicked | |
| function runScript() { | |
| const orderItems = document.querySelectorAll('.order-item'); | |
| const dataStraight = []; | |
| orderItems.forEach(orderItem => { | |
| dataStraight.push(extractOrderData(orderItem)); | |
| }); | |
| const data = dataStraight.reverse(); | |
| // Convert data to CSV format | |
| const csvContent = convertToCSV(data); | |
| // Download CSV file | |
| const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' }); | |
| const link = document.createElement('a'); | |
| link.href = window.URL.createObjectURL(blob); | |
| link.download = 'order_data.csv'; | |
| link.style.display = 'none'; | |
| document.body.appendChild(link); | |
| link.click(); | |
| document.body.removeChild(link); | |
| } | |
| // Create and style the button | |
| const button = document.createElement('button'); | |
| button.textContent = 'Extract Order Data'; | |
| button.style.position = 'fixed'; | |
| button.style.top = '20px'; | |
| button.style.left = '50%'; | |
| button.style.transform = 'translateX(-50%)'; | |
| button.style.padding = '10px 20px'; | |
| button.style.border = 'none'; | |
| button.style.borderRadius = '5px'; | |
| button.style.background = '#007bff'; | |
| button.style.color = '#fff'; | |
| button.style.cursor = 'pointer'; | |
| button.addEventListener('click', runScript); | |
| // Add the button to the page | |
| document.body.appendChild(button); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment