Skip to content

Instantly share code, notes, and snippets.

@tdtgit
Last active August 3, 2023 02:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tdtgit/ca2fa57c0f0aca35235e77b31b53d35d to your computer and use it in GitHub Desktop.
Save tdtgit/ca2fa57c0f0aca35235e77b31b53d35d to your computer and use it in GitHub Desktop.
Total spent on Uniqlo.com - Paste this code to your browser console and wait for minutes
// Function to format a number as currency
function formatCurrency(amount, currencyCode) {
return new Intl.NumberFormat('vi-VN', {
style: 'currency',
currency: currencyCode,
}).format(amount);
}
// Function to fetch data from a URL and handle errors
async function fetchData(url) {
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Error fetching data from ${url}`);
}
const data = await response.json();
if (data.status !== 'ok') {
throw new Error(`Error in data response from ${url}`);
}
return data.result;
} catch (error) {
console.error('An error occurred:', error);
return null;
}
}
// Function to fetch order details using the order ID
async function fetchOrderDetails(orderID) {
const url = `https://www.uniqlo.com/vn/api/commerce/v3/vi/orders/${orderID}`;
const orderDetail = await fetchData(url);
return orderDetail ? orderDetail.orderDetail : null;
}
// Function to fetch order data from the API
async function fetchOrderData() {
const url = `https://www.uniqlo.com/vn/api/commerce/v3/vi/orders?searchPage=1&displayResults=50`;
const orderData = await fetchData(url);
return orderData ? orderData.orders : [];
}
// Function to delay execution
function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
// Function to loop through order data, fetch order details, and calculate the total spent
async function processOrderData() {
const orders = await fetchOrderData();
let totalSpent = 0;
for (const order of orders) {
const orderDetails = await fetchOrderDetails(order.no);
if (orderDetails) {
const orderDate = new Date(orderDetails.createdDatetime * 1000).toLocaleString();
const orderValue = orderDetails.totalAmountTaxIncluded.value;
console.log(`Processing order: ${orderDate} - Value: ${formatCurrency(orderValue, 'VND')}`);
totalSpent += orderValue;
}
await delay(500); // Add a delay of 0.5 seconds between requests
}
const formattedTotalSpent = formatCurrency(totalSpent, 'VND');
console.log(`%cTotal Spent on Uniqlo.com: %c${formattedTotalSpent}`, 'font-weight: bold; font-size: 24px;', 'font-weight: bold; color: green; font-size: 24px;');
}
// Call the function to start processing order data and calculating the total spent
processOrderData();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment