Skip to content

Instantly share code, notes, and snippets.

@ynifamily3
Last active February 17, 2025 15:42
Show Gist options
  • Save ynifamily3/3c4f55db2d3a547b559e5ad8126981d1 to your computer and use it in GitHub Desktop.
Save ynifamily3/3c4f55db2d3a547b559e5ad8126981d1 to your computer and use it in GitHub Desktop.
function getMeta__() {
return [...document.querySelectorAll('#section_menu > ul > li > a')].map(
(x) => {
const href = x.getAttribute('href');
return href.split("'").filter((_, i) => i % 2 !== 0);
}
);
}
async function getMK__() {
const paperInfoForm = document.getElementById('paper_info_form');
const formData = new FormData(paperInfoForm);
const meta = getMeta__();
const pdfs = [];
for (let i = 0; i < meta.length; i++) {
formData.set('section', meta[i][0]);
for (let j = 0; j < Number(meta[i][1]); j++) {
formData.set('page_no', String(j + 1));
try {
const response = await fetch(
'https://digital.mk.co.kr/new/downloadPaperPdf.php',
{
method: 'POST',
body: formData,
}
);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const blob = await response.blob();
const url = window.URL.createObjectURL(blob);
pdfs.push(url);
const a = document.createElement('a');
a.href = url;
// a.download = `${formData.get('year')}${formData.get('month')}${formData.get('day')}_${meta[i][0]}_${String(j + 1)}.pdf`;
// a.download = `${meta[i][0]}_${String(j + 1).padStart(2, '0')}.pdf`;
// document.body.appendChild(a);
// a.click();
// a.remove();
// 다운로드 완료 후 일정 시간 대기
console.log(
'resolve',
`${meta[i][0]}_${String(j + 1).padStart(2, '0')}.pdf`
);
await new Promise((resolve) => setTimeout(resolve, 150));
} catch (error) {
console.error('Error downloading PDF:', error);
}
}
}
// run agent (pdf merger)
var merger = new PDFMerger();
for (let i = 0; i < pdfs.length; i++) {
console.log('머지', i, pdfs[i]);
try {
await merger.add(pdfs[i]);
} catch (e) {
console.log('ignore', i, pdfs[i]);
}
}
const title = document
.querySelector('#calendar_paper_btn > span')
.innerText.trim();
console.log('제목:', title);
await merger.setMetadata({
title,
});
await merger.save(`(통합) 매경 ${title}`);
}
// 다운로드 버튼 생성
const downloadButton = document.createElement("button");
downloadButton.textContent = "다운로드";
// downloadButton.style.position = "absolute";
// downloadButton.style.top = "10px";
// downloadButton.style.left = "10px";
downloadButton.style.padding = "10px 20px";
downloadButton.style.fontSize = "16px";
downloadButton.style.cursor = "pointer";
downloadButton.style.zIndex = "1000"; // 다른 요소 위에 배치
// 클릭 시 실행할 함수 지정
downloadButton.onclick = function() {
if (typeof getMK__ === "function") {
getMK__();
} else {
console.error("getMK__ 함수가 정의되지 않았습니다.");
}
};
// body 최상단에 추가
document.body.prepend(downloadButton);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment