Last active
May 21, 2024 03:45
-
-
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
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
// 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() { | |
let orders = []; | |
let searchPage = 1; | |
let hasMoreData = true; | |
while (hasMoreData) { | |
const url = `https://www.uniqlo.com/vn/api/commerce/v3/vi/orders?searchPage=${searchPage}&displayResults=10`; | |
const orderData = await fetchData(url); | |
if (orderData && orderData.orders.length > 0) { | |
orders = [...orders, ...orderData.orders]; | |
searchPage++; | |
} else { | |
hasMoreData = false; | |
} | |
} | |
return 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