Skip to content

Instantly share code, notes, and snippets.

@zackexplosion
Last active October 14, 2021 11:22
Show Gist options
  • Save zackexplosion/6bec4da8515eb8bc8f1c1b8a19f94354 to your computer and use it in GitHub Desktop.
Save zackexplosion/6bec4da8515eb8bc8f1c1b8a19f94354 to your computer and use it in GitHub Desktop.
藍新後台出貨單下載
// make sure you have been login to https://www.newebpay.com/sale/Sell_center/search_transaction
// copy and paste this snippit to web dev console
// exportToCsv credit
// https://stackoverflow.com/questions/14964035/how-to-export-javascript-array-info-to-csv-on-client-side
function exportToCsv(filename, rows) {
var processRow = function (row) {
var finalVal = '';
for (var j = 0; j < row.length; j++) {
var innerValue = row[j] === null ? '' : row[j].toString();
if (row[j] instanceof Date) {
innerValue = row[j].toLocaleString();
};
var result = innerValue.replace(/"/g, '""');
if (result.search(/("|,|\n)/g) >= 0)
result = '"' + result + '"';
if (j > 0)
finalVal += ',';
finalVal += result;
}
return finalVal + '\n';
};
var csvFile = '';
for (var i = 0; i < rows.length; i++) {
csvFile += processRow(rows[i]);
}
var blob = new Blob([csvFile], { type: 'text/csv;charset=big-5;' });
if (navigator.msSaveBlob) { // IE 10+
navigator.msSaveBlob(blob, filename);
} else {
var link = document.createElement("a");
if (link.download !== undefined) { // feature detection
// Browsers that support HTML5 download attribute
var url = URL.createObjectURL(blob);
link.setAttribute("href", url);
link.setAttribute("download", filename);
link.style.visibility = 'hidden';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
}
}
var elements = []
$('#table_area_trans tr').each((i, v) => {
let e = v.querySelector('#trans_detail')
if (e) elements.push(e)
})
var orders = []
var i = 0
function printOrders(){
let data = [['訂單號碼', '商品內容']]
orders.forEach((v,i ) => {
data.push([v.id, v.desc])
})
let filename = '機促會' +new Date().getTime() + '出貨單.csv'
console.log(data.join('\n'))
exportToCsv(filename,data)
}
function getData() {
if(i >= elements.length) {
printOrders()
return
}
elements[i].onclick()
setTimeout(function() {
var order = {
id: $('#trans_detail_table #mer_tranid').text(),
desc: $('#trans_detail_table #prodesc').text()
}
orders.push(order)
i++
getData()
}, 3000)
}
getData()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment