4,554
edits
No edit summary |
No edit summary |
||
Line 1,260: | Line 1,260: | ||
}); | }); | ||
(function ($) { | |||
var DEBUG = true; // set false to silence console logs | var DEBUG = true; // set false to silence console logs | ||
Line 1,304: | Line 1,301: | ||
} | } | ||
// 5 | // 5) Build a hidden iframe, inject CSS+HTML, and print | ||
function buildIframeAndPrint(printHtml, borderPref, $btn) { | function buildIframeAndPrint(printHtml, borderPref, $btn) { | ||
if (DEBUG) console.log("[print] buildIframeAndPrint: starting"); | if (DEBUG) console.log("[print] buildIframeAndPrint: starting"); | ||
Line 1,446: | Line 1,364: | ||
var htmlEl = doc.documentElement; | var htmlEl = doc.documentElement; | ||
if (borderPref === "without") { | if (borderPref === "without") { | ||
htmlEl.classList | if (htmlEl.classList) { | ||
htmlEl.classList.add("print-no-border"); | |||
} else if ( | |||
(" " + htmlEl.className + " ").indexOf(" print-no-border ") === -1 | |||
) { | |||
htmlEl.className += " print-no-border"; | |||
} | |||
} else { | } else { | ||
htmlEl.classList | if (htmlEl.classList) { | ||
htmlEl.classList.remove("print-no-border"); | |||
} else { | |||
htmlEl.className = htmlEl.className.replace( | |||
/\bprint-no-border\b/g, | |||
"" | |||
); | |||
} | |||
} | } | ||
if (DEBUG) console.log("[print] borderPref applied:", borderPref); | if (DEBUG) console.log("[print] borderPref applied:", borderPref); | ||
Line 1,474: | Line 1,398: | ||
onlyBr | onlyBr | ||
) { | ) { | ||
p.parentNode | if (p.parentNode) p.parentNode.removeChild(p); | ||
} | } | ||
}); | }); | ||
Line 1,530: | Line 1,454: | ||
var refs = doc.querySelectorAll(".article-external-reference a[href]"); | var refs = doc.querySelectorAll(".article-external-reference a[href]"); | ||
Array.prototype.forEach.call(refs, function (a) { | |||
var txt = (a.textContent || "").trim(); | var txt = (a.textContent || "").trim(); | ||
var href = a.getAttribute("href") || ""; | var href = a.getAttribute("href") || ""; | ||
Line 1,658: | Line 1,582: | ||
} | } | ||
// | // 6b) Choice click handler (decides source and calls buildIframeAndPrint) | ||
// | function handlePrintChoice(id, $btn) { | ||
if ($btn.data("busy")) { | |||
if (DEBUG) console.log("[print] busy, ignoring click"); | |||
return; | |||
} | |||
$btn.data("busy", true); | |||
var borderPref = id === "print-no-border" ? "without" : "with"; | |||
if (DEBUG) console.log("[print] option chosen:", borderPref); | |||
preloadFontForPrint(); | |||
// Prefer local .print-only (Entry page) | |||
var localPrintOnly = $(".print-only").first(); | |||
if (DEBUG) | |||
console.log( | |||
"[print] local .print-only present:", | |||
localPrintOnly.length | |||
); | |||
if (localPrintOnly.length) { | |||
if (DEBUG) console.log("[print] using local .print-only on this page"); | |||
hidePrintUI(); | |||
buildIframeAndPrint(localPrintOnly.prop("outerHTML"), borderPref, $btn); | |||
return; | |||
} | |||
// Otherwise fetch by title (modal/home list flow) | |||
var title = | |||
window.currentEntryTitle || | |||
(window.mw && | |||
mw.config && | |||
mw.config.get && | |||
mw.config.get("wgPageName")); | |||
if (DEBUG) console.log("[print] currentEntryTitle / wgPageName:", title); | |||
if (!title) { | |||
console.warn("[print] no title; falling back to window.print()"); | |||
window.print(); | |||
$btn.data("busy", false); | |||
return; | |||
} | |||
var pageUrl = | |||
window.mw && mw.util && mw.util.getUrl | |||
? mw.util.getUrl(title) | |||
: "/wiki/" + String(title); | |||
var pageUrlFresh = cacheBust(pageUrl); | |||
if (DEBUG) console.log("[print] fetching page HTML:", pageUrlFresh); | |||
$.get(pageUrlFresh) | |||
.done(function (html) { | |||
var $tmp = $("<div>").html(html); | |||
var $print = $tmp.find(".print-only").first(); | |||
if (DEBUG) | |||
console.log("[print] .print-only in fetched page:", $print.length); | |||
if (!$print.length) { | |||
console.warn( | |||
"[print] no .print-only in fetched page; window.print()" | |||
); | |||
window.print(); | |||
$btn.data("busy", false); | |||
return; | |||
} | |||
hidePrintUI(); | |||
buildIframeAndPrint($print.prop("outerHTML"), borderPref, $btn); | |||
}) | |||
.fail(function (xhr) { | |||
console.warn( | |||
"[print] fetch failed:", | |||
xhr && xhr.status, | |||
xhr && xhr.statusText | |||
); | |||
window.print(); | |||
$("#print-button").data("busy", false); | |||
}); | |||
} | |||
// 7) Event wiring — one tolerant handler | |||
$(document).off("click.print"); | $(document).off("click.print"); | ||
// | // Container handler: | ||
// - #print-button | // - #print-button toggles chooser | ||
// - #print-chooser | // - #print-chooser / #print-options capture clicks on the two anchors | ||
$(document).on( | $(document).on( | ||
"click.print", | "click.print", | ||
Line 1,675: | Line 1,676: | ||
var $chooser = ensurePrintChooser(); | var $chooser = ensurePrintChooser(); | ||
$chooser.toggle(); | $chooser.toggle(); | ||
$("#show-article").toggleClass( | $("#show-article").toggleClass( | ||
"print-opts-open", | "print-opts-open", | ||
$chooser.is(":visible") | $chooser.is(":visible") | ||
); | ); | ||
if (DEBUG) | if (DEBUG) | ||
console.log( | console.log( | ||
Line 1,690: | Line 1,689: | ||
// Otherwise, check if one of the two choice anchors was clicked | // Otherwise, check if one of the two choice anchors was clicked | ||
var | var $choice = $(e.target).closest( | ||
if (! | "a#print-with-border, a#print-no-border" | ||
); | |||
if (!$choice.length) return; // click inside container but not on a choice | |||
e.preventDefault(); | e.preventDefault(); | ||
var id = | var id = $choice.attr("id"); | ||
if (DEBUG) console.log("[print] option anchor click:", | if (DEBUG) console.log("[print] option anchor click:", id); | ||
handlePrintChoice(id, $ | handlePrintChoice(id, $choice); | ||
} | } | ||
); | ); | ||
Line 1,703: | Line 1,704: | ||
$(document).on("click.print", "#close-button", hidePrintUI); | $(document).on("click.print", "#close-button", hidePrintUI); | ||
$(document).on("keydown.print", function (e) { | $(document).on("keydown.print", function (e) { | ||
if (e. | // No ES6 keys here; use keyCode for ES5 compatibility | ||
if (e.keyCode === 27) hidePrintUI(); // ESC | |||
}); | }); | ||
}); | }); |