4,476
edits
No edit summary |
No edit summary |
||
Line 1,372: | Line 1,372: | ||
// 1) Add a tiny print-only CSS override to keep anchors as one box. | // 1) Add a tiny print-only CSS override to keep anchors as one box. | ||
var linkCssFix = doc.createElement("style"); | var linkCssFix = doc.createElement("style"); | ||
linkCssFix.textContent = | linkCssFix.textContent = | ||
"@media print {\n" + | |||
/* Keep anchor boxes intact so Chrome preserves the PDF annotation */ | " /* Keep anchor boxes intact so Chrome preserves the PDF annotation */\n" + | ||
.article-external-reference a, | " .article-external-reference a,\n" + | ||
.link-pdf a { | " .link-pdf a {\n" + | ||
" white-space: nowrap !important;\n" + | |||
" word-break: normal !important;\n" + | |||
" overflow-wrap: normal !important;\n" + | |||
" text-decoration: underline;\n" + | |||
} | " }\n" + | ||
/* Allow wrapping outside the anchor instead */ | " /* Allow wrapping outside the anchor instead */\n" + | ||
.article-external-reference { | " .article-external-reference {\n" + | ||
" overflow-wrap: anywhere;\n" + | |||
" word-break: break-word;\n" + | |||
} | " }\n" + | ||
/* Defensive: make sure anchors have a box */ | " /* Defensive: make sure anchors have a box */\n" + | ||
a[href] { position: relative; } | " a[href] { position: relative; }\n" + | ||
"}\n"; | |||
doc.head.appendChild(linkCssFix); | doc.head.appendChild(linkCssFix); | ||
Line 1,395: | Line 1,395: | ||
(function () { | (function () { | ||
// Shorten long visible URLs in external references, keep href intact | // Shorten long visible URLs in external references, keep href intact | ||
var refs = doc.querySelectorAll(".article-external-reference a[href]"); | var refs = doc.querySelectorAll( | ||
".article-external-reference a[href]" | |||
); | |||
refs.forEach(function (a) { | refs.forEach(function (a) { | ||
var txt = (a.textContent || "").trim(); | var txt = (a.textContent || "").trim(); | ||
Line 1,404: | Line 1,406: | ||
try { | try { | ||
var u = new URL(href, doc.baseURI); | var u = new URL(href, doc.baseURI); | ||
var label = u.hostname + (u.pathname.replace(/\/$/, "") ? u.pathname : ""); | var label = | ||
u.hostname + | |||
(u.pathname.replace(/\/$/, "") ? u.pathname : ""); | |||
if (label.length > 40) label = label.slice(0, 37) + "…"; | if (label.length > 40) label = label.slice(0, 37) + "…"; | ||
a.textContent = label; | a.textContent = label; | ||
Line 1,428: | Line 1,432: | ||
// Wait helpers | // Wait helpers | ||
function waitImages() { | function waitImages() { | ||
var imgs = | var imgs = [].slice.call(doc.images || []); | ||
if (!imgs.length) return Promise.resolve(); | if (!imgs.length) return Promise.resolve(); | ||
return Promise.all( | return Promise.all( | ||
imgs.map(function (img) { | imgs.map(function (img) { | ||
if (img.decode) return img.decode().catch(function () {}); | if (img.decode) { | ||
try { | |||
return img.decode().catch(function () {}); | |||
} catch (e) {} | |||
} | |||
return new Promise(function (res) { | return new Promise(function (res) { | ||
if (img.complete) return res(); | if (img.complete) return res(); |