MediaWiki:Common.js: Difference between revisions

Jump to navigation Jump to search
no edit summary
No edit summary
No edit summary
Line 775: Line 775:
          
          
         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,050:
     $('#print-button').on('click', function () {
     $('#print-button').on('click', function () {
         var pageTitle = window.currentEntryTitle;
         var pageTitle = window.currentEntryTitle;
         if (!pageTitle) { window.print(); return; }
         if (!pageTitle) {
            console.warn('[print] No currentEntryTitle; printing whole page fallback.');
            window.print();
            return;
        }


         var api = mw.util.wikiScript('api');
         var api = mw.util.wikiScript('api');
         // fetch rendered entry HTML
         console.log('[print] Fetching parsed HTML for:', pageTitle, 'via', api);
 
         $.getJSON(api, {
         $.getJSON(api, {
             action: 'parse',
             action: 'parse',
             page: pageTitle,
             page: pageTitle,         // exact page title, e.g. "040"
             prop: 'text',
             prop: 'text',
             format: 'json'
             format: 'json',
            origin: '*'              // safe for CORS if needed
         }).done(function (res) {
         }).done(function (res) {
             var html = res && res.parse && res.parse.text && res.parse.text['*'];
             var html = res && res.parse && res.parse.text && res.parse.text['*'];
             if (!html) { window.print(); return; }
             if (!html) {
            console.warn('[print] No HTML returned from parse API; fallback print.');
            window.print();
            return;
            }


             var $tmp = $('<div>').html(html);
             var $tmp = $('<div>').html(html);
             var $print = $tmp.find('.print-only').first();
             var $print = $tmp.find('.print-only').first();
             if (!$print.length) { window.print(); return; }
             console.log('[print] .print-only found:', $print.length);


             // create hidden iframe
            if (!$print.length) {
            console.warn('[print] No .print-only in parsed HTML; fallback print.');
            window.print();
            return;
            }
 
             // Build hidden iframe
             var iframe = document.createElement('iframe');
             var iframe = document.createElement('iframe');
             iframe.style.position = 'fixed';
             iframe.style.position = 'fixed';
Line 1,077: Line 1,093:
             document.body.appendChild(iframe);
             document.body.appendChild(iframe);


            // load print CSS and the print-only HTML
             var doc = iframe.contentDocument || iframe.contentWindow.document;
             var doc = iframe.contentDocument || iframe.contentWindow.document;
             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' });


            console.log('[print] Using print CSS:', printCssUrl);
            // Write minimal document with ONLY the print block + print CSS
             doc.open();
             doc.open();
             doc.write(
             doc.write(
Line 1,090: Line 1,108:
             doc.close();
             doc.close();


            // Give the iframe a moment, then print
             iframe.onload = function () {
             iframe.onload = function () {
             iframe.contentWindow.focus();
             try {
            iframe.contentWindow.print();
                iframe.contentWindow.focus();
             setTimeout(function () { document.body.removeChild(iframe); }, 500);
                iframe.contentWindow.print();
             } finally {
                setTimeout(function () { document.body.removeChild(iframe); }, 500);
            }
             };
             };
         }).fail(function () {
         }).fail(function (xhr) {
            console.warn('[print] API request failed:', xhr && xhr.status, xhr && xhr.statusText);
             window.print();
             window.print();
         });
         });

Navigation menu