fix: set parent page title before print for correct PDF filename

Chrome/Edge uses parent page title (not iframe title) when printing
to PDF. Temporarily swap document.title to translated stem before
print(), restore it 1s later after dialog opens.
This commit is contained in:
2026-07-13 17:51:54 +08:00
parent 206ef091cc
commit e4641aee58

View File

@@ -2010,18 +2010,14 @@
const printTitle = match ? match[1].replace(/\.[^.]+$/, '') : '';
return r.text().then(h => ({ h, printTitle }));
}).then(({ h, printTitle }) => {
if (printTitle) {
if (/<title>/i.test(h)) {
h = h.replace(/<title>[^<]*<\/title>/i, `<title>${printTitle}</title>`);
} else {
h = h.replace(/<head>/i, `<head><title>${printTitle}</title>`);
}
}
ifr.srcdoc = h;
ifr.onload = () => {
setTimeout(() => {
ifr.contentWindow.focus();
const originalTitle = document.title;
if (printTitle) document.title = printTitle;
ifr.contentWindow.print();
setTimeout(() => { document.title = originalTitle; }, 1000);
}, 500);
};
});