From e4641aee58de843df756a382672470e2ff8cf265 Mon Sep 17 00:00:00 2001 From: Leon Date: Mon, 13 Jul 2026 17:51:54 +0800 Subject: [PATCH] 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. --- docutranslate/static/index.html | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/docutranslate/static/index.html b/docutranslate/static/index.html index 05721ff..ad1ef00 100644 --- a/docutranslate/static/index.html +++ b/docutranslate/static/index.html @@ -2010,18 +2010,14 @@ const printTitle = match ? match[1].replace(/\.[^.]+$/, '') : ''; return r.text().then(h => ({ h, printTitle })); }).then(({ h, printTitle }) => { - if (printTitle) { - if (//i.test(h)) { - h = h.replace(/<title>[^<]*<\/title>/i, `<title>${printTitle}`); - } else { - h = h.replace(//i, `${printTitle}`); - } - } 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); }; });