MediaWiki:Common.js: Difference between revisions

Jump to navigation Jump to search
1,888 bytes removed ,  Yesterday at 08:18
no edit summary
No edit summary
No edit summary
Line 9: Line 9:
         window.location.href = '/Main_Page'; // Redirects to the home page
         window.location.href = '/Main_Page'; // Redirects to the home page
     });
     });
    function getApiUrl() {
        // With wgScriptPath = "", api.php is at root
        return '/api.php';
    }
    // Resolve a title to a pageid (or null if missing)
    function resolveTitleToPageId(title, cb) {
        $.getJSON(getApiUrl(), {
            action: 'query',
            prop: 'info',
            titles: title,
            format: 'json',
            formatversion: 2
        }).done(function (res) {
            if (!res || !res.query || !res.query.pages || !res.query.pages.length) {
            console.warn('[print] query returned no pages for title:', title, res);
            cb(null);
            return;
            }
            var page = res.query.pages[0];
            if (page.missing) {
            console.warn('[print] page is missing for title:', title);
            cb(null);
            return;
            }
            cb(page.pageid);
        }).fail(function (xhr) {
            console.warn('[print] query fail:', xhr && xhr.status, xhr && xhr.statusText);
            cb(null);
        });
    }


     // Loop through each card to format related articles
     // Loop through each card to format related articles
Line 1,080: Line 1,048:
     // Print current entry only
     // Print current entry only
     $('#print-button').on('click', function () {
     $('#print-button').on('click', function () {
         var title = window.currentEntryTitle;
         var title = window.currentEntryTitle; // e.g. "090"
         if (!title) { console.warn('[print] no currentEntryTitle'); window.print(); return; }
         if (!title) { console.warn('[print] no currentEntryTitle'); window.print(); return; }


         console.log('[print] resolving title:', title);
        var pageUrl = mw.util.getUrl(title); // respects /log/$1
         console.log('[print] fetching page HTML:', pageUrl);


         resolveTitleToPageId(title, function (pageid) {
         $.get(pageUrl).done(function (html) {
            if (!pageid) {
            console.warn('[print] could not resolve title to pageid; fallback print.');
            window.print();
            return;
            }
 
            var apiUrl = getApiUrl();
            console.log('[print] parsing pageid:', pageid, 'via', apiUrl);
 
            $.getJSON(apiUrl, {
            action: 'parse',
            pageid: pageid,          // use pageid, not 'page'
            prop: 'text',
            format: 'json',
            formatversion: 2
            }).done(function (res) {
            if (!res || !res.parse || !res.parse.text) {
                console.warn('[print] parse returned no text for pageid:', pageid, res);
                window.print();
                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) { console.warn('[print] no .print-only in parsed HTML'); window.print(); return; }
             if (!$print.length) { console.warn('[print] no .print-only found; fallback print'); 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,125: Line 1,071:


             var doc = iframe.contentDocument || iframe.contentWindow.document;
             var doc = iframe.contentDocument || iframe.contentWindow.document;
            // Raw Print.css (works with wgScriptPath = "")
             var printCssUrl = '/index.php?title=MediaWiki:Print.css&action=raw&ctype=text/css';
             var printCssUrl = '/index.php?title=MediaWiki:Print.css&action=raw&ctype=text/css';


             doc.open();
             doc.open();
             doc.write(
             doc.write(
                '<!doctype html><html><head><meta charset="utf-8">' +
            '<!doctype html><html><head><meta charset="utf-8">' +
                '<title>Print</title>' +
            '<title>Print</title>' +
                '<link rel="stylesheet" href="' + printCssUrl + '">' +
            '<link rel="stylesheet" href="' + printCssUrl + '">' +
                '</head><body>' + $print.prop('outerHTML') + '</body></html>'
            '</head><body>' + $print.prop('outerHTML') + '</body></html>'
             );
             );
             doc.close();
             doc.close();


             iframe.onload = function () {
             iframe.onload = function () {
                try { iframe.contentWindow.focus(); iframe.contentWindow.print(); }
            try { iframe.contentWindow.focus(); iframe.contentWindow.print(); }
                finally { setTimeout(function () { document.body.removeChild(iframe); }, 500); }
            finally { setTimeout(function () { document.body.removeChild(iframe); }, 500); }
             };
             };
            }).fail(function (xhr) {
        }).fail(function (xhr) {
             console.warn('[print] parse fail:', xhr && xhr.status, xhr && xhr.statusText);
             console.warn('[print] fetch failed:', xhr && xhr.status, xhr && xhr.statusText);
             window.print();
             window.print();
            });
         });
         });
     });
     });


     // Close modal with Close button
     // Close modal with Close button

Navigation menu