4,456
edits
No edit summary |
No edit summary |
||
Line 1,546: | Line 1,546: | ||
}); | }); | ||
} | } | ||
Promise.all([ | Promise.all([ | ||
Line 1,560: | Line 1,552: | ||
waitFonts(1200), | waitFonts(1200), | ||
waitSpecificFont(1200), | waitSpecificFont(1200), | ||
nextFrame(), | nextFrame(), | ||
]).then(function () { | ]).then(function () { | ||
try { | try { | ||
// build the desired PDF filename via document.title | |||
var entryNum = ""; | |||
var numEl = doc.querySelector(".article-entry-number"); | |||
if (numEl) { | |||
var m = (numEl.textContent || "").match(/\d+/); | |||
entryNum = m ? m[0] : ""; | |||
} | |||
var desiredTitle = | |||
(entryNum ? "[" + entryNum + "] " : "") + "softwear.directory"; | |||
// save originals (scoped here) | |||
var oldIframeTitle = doc.title; | |||
var oldParentTitle = document.title; | |||
// define onafterprint AFTER we have originals so it can close over them | |||
iframe.contentWindow.onafterprint = function () { | |||
try { | |||
doc.title = oldIframeTitle; // restore iframe doc title | |||
document.title = oldParentTitle; // restore parent title | |||
} catch (e) {} | |||
setTimeout(function () { | |||
if (iframe.parentNode) iframe.parentNode.removeChild(iframe); | |||
}, 100); | |||
$btn.data("busy", false); | |||
}; | |||
// set temporary titles used by Chrome for the default PDF name | |||
doc.title = desiredTitle; // iframe document | |||
// (next line is optional/redundant; doc === iframe.contentWindow.document) | |||
// iframe.contentWindow.document.title = desiredTitle; | |||
document.title = desiredTitle; // parent (helps on some setups) | |||
// print | |||
iframe.contentWindow.focus(); | iframe.contentWindow.focus(); | ||
iframe.contentWindow.print(); | iframe.contentWindow.print(); | ||
// fallback cleanup in case onafterprint | // fallback cleanup in case onafterprint doesn’t fire | ||
setTimeout(function () { | setTimeout(function () { | ||
try { | |||
doc.title = oldIframeTitle; | |||
document.title = oldParentTitle; | |||
} catch (e) {} | |||
if (iframe.parentNode) iframe.parentNode.removeChild(iframe); | if (iframe.parentNode) iframe.parentNode.removeChild(iframe); | ||
$btn.data("busy", false); | $btn.data("busy", false); | ||
}, 1000); | }, 1000); | ||
} catch (err) { | |||
console.warn("[print] failed before print:", err); | |||
$btn.data("busy", false); | |||
} | } | ||
}); | }); |