let startYear = 2014;
let endYear = 2025;
let total = 0;
const today = new Date();
const todayYear = today.getFullYear();
const todayMonth = String(today.getMonth() + 1).padStart(2, '0');
const todayDate = String(today.getDate()).padStart(2, '0');
const endDateFormatted = `${todayYear}.${todayMonth}.${todayDate}`;
async function fetchAmountsForYear(year) {
let yearTotal = 0;
let page = 1;
let hasNextPage = true;
while (hasNextPage) {
const startDate = `${year}.01.01`;
const endDate = year === todayYear ? endDateFormatted : `${year}.12.31`;
const baseUrl = `?startDate=${startDate}&endDate=${endDate}&Page=${page}`;
const response = await fetch(baseUrl);
const text = await response.text();
const parser = new DOMParser();
const doc = parser.parseFromString(text, 'text/html');
var emptyMessageCell = doc.querySelector('td.empty');
if (emptyMessageCell && emptyMessageCell.textContent.trim() === '해당이력이 없습니다.') {
console.log(`년도 ${year}에 해당 이력이 없습니다. 다음 년도로 진행합니다.`);
return null;
}
const amounts = doc.querySelectorAll('td.amount');
amounts.forEach(amount => {
const value = parseInt(amount.textContent.replace(/,/g, ''), 10);
if (value > 0) {
yearTotal += value;
}
});
const nextButton = doc.querySelector('a.btn_arrow.next');
hasNextPage = nextButton !== null;
page++;
}
return yearTotal;
}
async function fetchAllAmounts() {
for (let year = startYear; year <= endYear; year++) {
const yearTotal = await fetchAmountsForYear(year);
if (yearTotal !== null) {
total += yearTotal;
const formattedYearTotal = yearTotal.toString().replace(/B(?=(d{3})+(?!d))/g, ",");
console.log(`${year}년의 총 금액: ${formattedYearTotal}원`);
}
}
const formattedTotal = total.toString().replace(/B(?=(d{3})+(?!d))/g, ",");
console.log("지금까지 사용한 총 금액 :", formattedTotal + "원");
}
fetchAllAmounts();