4,396
edits
No edit summary |
No edit summary |
||
Line 773: | Line 773: | ||
function openModal(cardElement, event) { | function openModal(cardElement, event) { | ||
event.stopPropagation(); | event.stopPropagation(); | ||
var pageTitle = $(cardElement).data('page') || null; | var pageTitle = $(cardElement).data('page') || null; | ||
window.currentEntryTitle = pageTitle; | window.currentEntryTitle = pageTitle; | ||
var isRelatedArticle = $(cardElement).hasClass('related-article'); | var isRelatedArticle = $(cardElement).hasClass('related-article'); | ||
Line 1,050: | Line 1,049: | ||
$('#print-button').on('click', function () { | $('#print-button').on('click', function () { | ||
var pageTitle = window.currentEntryTitle; | var pageTitle = window.currentEntryTitle; | ||
if (!pageTitle) { | if (!pageTitle) { console.warn('[print] no currentEntryTitle'); window.print(); return; } | ||
var | var apiUrl = (mw.config.get('wgScriptPath') || '') + '/api.php'; | ||
console.log('[print] | console.log('[print] fetching:', pageTitle, 'via', apiUrl); | ||
$.getJSON( | $.getJSON(apiUrl, { | ||
action: 'parse', | action: 'parse', | ||
page: pageTitle, // | page: pageTitle, // e.g. "090" | ||
prop: 'text', | prop: 'text', | ||
format: 'json', | format: 'json', | ||
formatversion: 2 // html at res.parse.text | |||
// origin not needed same-origin | |||
}).done(function (res) { | }).done(function (res) { | ||
if (!res || !res.parse || !res.parse.text) { | |||
console.warn('[print] parse API returned no text for:', pageTitle, res); | |||
console.warn('[print] | |||
window.print(); | window.print(); | ||
return; | return; | ||
} | } | ||
var html = res.parse.text; | |||
var $tmp = $('<div>').html(html); | var $tmp = $('<div>').html(html); | ||
var $print = $tmp.find('.print-only').first(); | var $print = $tmp.find('.print-only').first(); | ||
console.log('[print] .print-only found:', $print.length); | console.log('[print] .print-only found:', $print.length); | ||
if (!$print.length) { | if (!$print.length) { console.warn('[print] no .print-only in parsed HTML'); window.print(); return; } | ||
// | // Hidden iframe | ||
var iframe = document.createElement('iframe'); | var iframe = document.createElement('iframe'); | ||
iframe.style.position = 'fixed'; | iframe.style.position = 'fixed'; | ||
Line 1,096: | Line 1,088: | ||
var printCssUrl = mw.util.getUrl('MediaWiki:Print.css', { action: 'raw', ctype: 'text/css' }); | var printCssUrl = mw.util.getUrl('MediaWiki:Print.css', { action: 'raw', ctype: 'text/css' }); | ||
doc.open(); | doc.open(); | ||
doc.write( | doc.write( | ||
Line 1,108: | Line 1,097: | ||
doc.close(); | doc.close(); | ||
iframe.onload = function () { | iframe.onload = function () { | ||
try { | try { | ||
Line 1,118: | Line 1,106: | ||
}; | }; | ||
}).fail(function (xhr) { | }).fail(function (xhr) { | ||
console.warn('[print] API | console.warn('[print] API fail:', xhr && xhr.status, xhr && xhr.statusText); | ||
window.print(); | window.print(); | ||
}); | }); |