|
// ==UserScript== |
|
// @name Export Shopee Orders to JSON |
|
// @namespace https://yukai.dev |
|
// @version 0.1.3 |
|
// @description Export Shopee Orders to JSON |
|
// @author Yukai Huang |
|
// @match https://shopee.tw/* |
|
// @icon https://www.google.com/s2/favicons?sz=64&domain=shopee.tw |
|
// @downloadURL https://gist.githubusercontent.com/Yukaii/200056f0c517fbd51eb20c7c43edbffe/raw/export-shopee-order.user.js |
|
// @updateURL https://gist.githubusercontent.com/Yukaii/200056f0c517fbd51eb20c7c43edbffe/raw/export-shopee-order.user.js |
|
// @grant GM_openInTab |
|
// @run-at context-menu |
|
// ==/UserScript== |
|
|
|
(async function() { |
|
'use strict'; |
|
|
|
function getAllOrderAndCheckoutList(offset = 0, limit = 20) { |
|
return fetch( |
|
`https://shopee.tw/api/v4/order/get_all_order_and_checkout_list?limit=${limit}&offset=${offset}`, |
|
).then((res) => res.json()); |
|
} |
|
|
|
async function getOrders() { |
|
let orders = [] |
|
|
|
for (let offset = 0; ;) { |
|
const { error, data } = await getAllOrderAndCheckoutList(offset); |
|
|
|
if (error || !data) { |
|
break |
|
} |
|
|
|
const { next_offset, order_data } = data |
|
|
|
if (next_offset === -1) { |
|
break |
|
} |
|
|
|
orders.push(...order_data.details_list); |
|
|
|
offset = next_offset |
|
} |
|
|
|
return orders; |
|
} |
|
|
|
function saveOrders(orders) { |
|
const data = JSON.stringify(orders, null, 2); |
|
|
|
// prompt browser to download file |
|
const blob = new Blob([data], { type: 'application/json' }); |
|
const url = URL.createObjectURL(blob); |
|
|
|
const a = document.createElement('a'); |
|
a.href = url; |
|
const timestamp = new Date().toISOString().replace(/:/g, '-'); |
|
a.download = `orders-${timestamp}.json`; |
|
a.click(); |
|
} |
|
|
|
const orders = await getOrders(); |
|
saveOrders(orders) |
|
})(); |