feat: on-page Download PDF opens Letter-size printable Study Load; drop pdf-lib/background path

This commit is contained in:
adrianbonpin
2026-08-18 19:28:44 +08:00
parent d6bacef1ec
commit cb2beecc0e
12 changed files with 287 additions and 480 deletions
+76 -9
View File
@@ -123,22 +123,89 @@
header = {};
}
const totalUnits = extractTotalUnits(document.body.innerText);
return { header, courses, totalUnits };
return { header, courses };
}
browser.runtime.onMessage.addListener((msg, sender, sendResponse) => {
if (msg && msg.type === 'scrape-study-load') {
scrapeStudyLoad()
.then((data) => sendResponse({ ok: true, data }))
.catch((err) => sendResponse({ ok: false, error: String((err && err.message) || err) }));
return true; // async response
// ---- On-page Download PDF button ----
const BTN_ID = 'ismisHelperDownloadBtn';
function showToast(msg, isError) {
if (window.toastr) {
if (isError) window.toastr.error(msg);
else window.toastr.success(msg);
} else if (isError) {
alert(msg);
}
});
}
function findTotalUnitsP() {
const ps = document.querySelectorAll('#StudyLoad p');
for (const p of ps) {
if (/Total Units/i.test(p.textContent || '')) return p;
}
return null;
}
async function onDownloadPdf() {
const btn = document.getElementById(BTN_ID);
try {
btn.disabled = true;
btn.innerHTML = '<i class="fa fa-spinner fa-spin"></i> Opening...';
const { header, courses } = await scrapeStudyLoad();
const included = ISMIStools.filterStudyLoadCourses(courses);
if (!included.length) throw new Error('No enrolled courses found.');
const totalUnits = ISMIStools.sumUnits(included);
const html = ISMIStools.buildStudyLoadHtml({ header, courses: included, totalUnits });
const win = window.open('', '_blank');
if (!win) {
showToast('Popup blocked — allow popups for this site, then try again.', true);
return;
}
win.document.write(html);
win.document.close();
showToast('Opened printable Study Load.');
} catch (err) {
showToast('Error: ' + ((err && err.message) || err), true);
} finally {
btn.disabled = false;
btn.innerHTML = '<i class="fa fa-download"></i> Download PDF';
}
}
function injectDownloadButton() {
if (document.getElementById(BTN_ID)) return;
const unitsP = findTotalUnitsP();
if (!unitsP) return;
const btn = document.createElement('button');
btn.id = BTN_ID;
btn.type = 'button';
btn.className = 'btn green pull-right';
btn.style.marginTop = '10px';
btn.innerHTML = '<i class="fa fa-download"></i> Download PDF';
btn.addEventListener('click', onDownloadPdf);
unitsP.insertAdjacentElement('afterend', btn);
}
function tryInjectButton() {
if (document.getElementById(BTN_ID)) return;
if (document.querySelector('#StudyLoad table.table-forum')) injectDownloadButton();
}
function watchStudyLoad() {
tryInjectButton();
const observer = new MutationObserver((mutations) => {
const inStudyLoad = mutations.some(
(m) => m.target && m.target.closest && m.target.closest('#StudyLoad')
);
if (inStudyLoad) tryInjectButton();
});
observer.observe(document.body, { childList: true, subtree: true });
}
(async function init() {
if (!(await isEnabled())) return;
watchPrivacyModal();
if (/\/Account\/Login/i.test(location.href)) autoLogin();
if (document.getElementById('StudyLoad')) watchStudyLoad();
})();
})();