Skip to content

Instantly share code, notes, and snippets.

@Bolukan
Last active July 28, 2023 14:17
Show Gist options
  • Save Bolukan/16146e6e9c3c7e05a8a23f8b2f8a1dc4 to your computer and use it in GitHub Desktop.
Save Bolukan/16146e6e9c3c7e05a8a23f8b2f8a1dc4 to your computer and use it in GitHub Desktop.
Aliexpress_items
// ==UserScript==
// @name Extract AliExpress Order Detail Pages
// @namespace http://tampermonkey.net/
// @version 0.2
// @description Extract the contents from AliExpress order detail pages, while also removing all svg tags
// @author You
// @match https://www.aliexpress.com/p/order/detail.html*
// @grant none
// ==/UserScript==
(function() {
'use strict';
function copyArrayToClipboard(arr) {
// Create a temporary textarea element
const textarea = document.createElement('textarea');
var orderStrings = arr.map((order) => Object.values(order).join('\t'));
var orderString = orderStrings.join('\n');
// Set the value of the textarea to the array string
textarea.value = orderString;
// Append the textarea to the document
document.body.appendChild(textarea);
// Select the contents of the textarea
textarea.select();
// Copy the selected text to the clipboard
document.execCommand('copy');
// Remove the temporary textarea from the document
document.body.removeChild(textarea);
}
function monthNameToNumber(monthName) {
const monthNames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
return monthNames.indexOf(monthName) + 1;
}
// Function to convert a date string from "Jul 25, 2023" to "dd-mm-yyyy" format
function formatDate(dateString) {
const parts = dateString.split(' ');
if (parts.length !== 3) {
return 'dd-mm-yyyy';
}
const month = monthNameToNumber(parts[0]);
const day = parseInt(parts[1].replace(',', ''), 10);
const year = parseInt(parts[2], 10);
// Pad day and month with leading zeros if needed
const formattedDay = day.toString().padStart(2, '0');
const formattedMonth = month.toString().padStart(2, '0');
return `${formattedDay}-${formattedMonth}-${year}`;
}
function extractOrderDetails() {
const orderDetailWraps = document.querySelectorAll('div.order-detail-item-content-wrap');
const orderDetails = [];
var counter = 1;
orderDetailWraps.forEach((orderWrap) => {
const orderDetail = {};
// counter
orderDetail.counter = counter;
counter++;
// product ID
const itemTitle = orderWrap.querySelector('div.item-title a');
// Check if the anchor tag exists and its href attribute contains ".html"
orderDetail.productId = '';
if (itemTitle && itemTitle.href.includes('.html')) {
// Extract the page name without the ".html" part
orderDetail.productId = itemTitle.href.split('/').pop().replace('.html', '');
};
// Get the item title
orderDetail.title = itemTitle?.textContent.trim() || '';
orderDetail.skuid = '';
// Get the item sku
const itemSKU = orderWrap.querySelector('div.item-sku-attr');
orderDetail.sku = itemSKU?.textContent.trim() || '';
// Get the item quantity
const itemQuantity = orderWrap.querySelector('span.item-price-quantity');
orderDetail.quantity = itemQuantity?.textContent.trim().substring(1) || '';
// Get the item price
const itemPrice = orderWrap.querySelector('div.item-price div.es--wrap--1CJdhmA');
orderDetail.price = itemPrice?.textContent.trim() || '';
orderDetail.productAction = '';
orderDetail.productURL = '';
// Get the image URL
const imgURL = orderWrap.querySelector('a.order-detail-item-content-img');
orderDetail.imageURL = imgURL?.style.backgroundImage.slice(5, -2) || '';
orderDetails.push(orderDetail);
});
return orderDetails;
}
function getOrderInfo(orderWrap) {
const order = {};
// *** Order Status Order Block
const orderStatusOrderBlock = orderWrap.querySelector('div.order-status.order-block[data-spm="order_detail_status"]');
// *** Order Detail Info - Order Detail Order Info
const orderDetailOrderInfo = orderWrap.querySelector('div.order-detail-info-item.order-detail-order-info');
// *** Order Detail Item
const orderDetailItem = orderWrap.querySelector('div.order-detail-item[data-spm="order_detail_item"]');
// *** Order Price
const orderPrice = orderWrap.querySelector('div.order-price');
// orderId
const orderDetailId = orderDetailOrderInfo.querySelector('span.order-detail-gray[data-pl="order_detail_gray_id"]');
order.orderId = orderDetailId?.nextSibling?.textContent.trim() || '';
// orderDate
const orderDetailDate = orderDetailOrderInfo.querySelector('span.order-detail-gray[data-pl="order_detail_gray_date"]');
order.orderDate = formatDate(orderDetailDate?.nextSibling?.textContent.trim() || '');
// totalAmount
const totalElement = orderPrice.querySelector('div.right-col.bold-font.right-gap.rightPriceClass');
const totalText = totalElement?.textContent.trim() || '';
const totalAmount = totalText.match(/€([\d.,]+)/);
order.totalAmount = totalAmount[0]; // ? parseFloat(totalAmount[1].replace(',', '.')) : null;
// orderStatus
const orderBlockTitleDiv = orderStatusOrderBlock?.querySelector('div.order-block-title[data-pl="order_block_title"]');
order.orderStatus = orderBlockTitleDiv?.textContent.trim() || '';
// storeName
const storeNameElement = orderDetailItem.querySelector('div.order-detail-item-store a span.store-name');
order.storeName = storeNameElement?.textContent.trim() || '';
// storeURL
const storeLinkElement = orderDetailItem.querySelector('div.order-detail-item-store a[href^="//www.aliexpress.com/store/"]');
order.storeURL = 'https:' + storeLinkElement?.getAttribute('href') || '';
order.hasTracking = true;
return order;
}
function scrapeAll() {
// Call the function and get the list of background-image URLs
const orderWrap = document.querySelector('div.order-wrap');
var orderInfo = getOrderInfo(orderWrap);
var orderDetails = extractOrderDetails();
var order = orderDetails.map((orderDetail) => ({...orderInfo, ...orderDetail}));
for (let i = 1; i < order.length; i++) {
order[i].totalAmount = '';
}
copyArrayToClipboard(order);
}
// Function to remove <svg> tags and their contents
function removeSVGTags() {
var svgElements = document.getElementsByTagName('svg');
for (var i = svgElements.length - 1; i >= 0; i--) {
var svgElement = svgElements[i];
svgElement.parentNode.removeChild(svgElement);
}
}
// Function to add a button to the page
function addButton() {
var button = document.createElement('button');
button.textContent = 'SCRAPE (c) Bolukan';
button.style.margin = '10px';
button.addEventListener('click', function() {
removeSVGTags();
scrapeAll();
});
// Find a suitable container on the page to add the button (you can customize
// this selector)
var container = document.querySelector('.top-lighthouse');
// Append the button to the container
if (container) {
container.appendChild(button);
}
}
// Call the function to add the button when the page loads
addButton();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment