MediaWiki:Common.js: Difference between revisions

Jump to navigation Jump to search
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
     });
     });
    // ---- Utility: robust API URL resolver ----
    function getApiUrl() {
        // Try wgScript first (often '/log/index.php')
        var script = mw.config.get('wgScript') || ''; // e.g. '/log/index.php'
        if (script) {
            return script.replace(/\/index\.php$/, '') + '/api.php';
        }
        // Fallback: derive from wgArticlePath (often '/log/$1')
        var articlePath = mw.config.get('wgArticlePath') || '';
        if (articlePath) {
            return articlePath.replace(/\/\$1.*/, '') + '/api.php';
        }
        // Last resort: infer from current URL path (handles being under /log/)
        var path = window.location.pathname;        // e.g. '/log/Main'
        var m = path.match(/^\/[^/]+/);              // '/log'
        var base = m ? m[0] : '';
        return base + '/api.php';
    }


     // Loop through each card to format related articles
     // Loop through each card to format related articles
Line 1,051: Line 1,073:
         if (!pageTitle) { console.warn('[print] no currentEntryTitle'); window.print(); return; }
         if (!pageTitle) { console.warn('[print] no currentEntryTitle'); window.print(); return; }


         var apiUrl = (mw.config.get('wgScriptPath') || '') + '/api.php';
         var apiUrl = getApiUrl();
         console.log('[print] fetching:', pageTitle, 'via', apiUrl);
         console.log('[print] fetching:', pageTitle, 'via', apiUrl);


         $.getJSON(apiUrl, {
         $.getJSON(apiUrl, {
             action: 'parse',
             action: 'parse',
             page: pageTitle,     // e.g. "091"
             page: pageTitle,       // e.g. "090"
             prop: 'text',
             prop: 'text',
             format: 'json',
             format: 'json',
             formatversion: 2
             formatversion: 2
         }).done(function (res) {
         }).done(function (res) {
            if (res && res.error) {
            console.warn('[print] API error:', res.error);
            }
            if (res && res.warnings) {
            console.warn('[print] API warnings:', res.warnings);
            }
             if (!res || !res.parse || !res.parse.text) {
             if (!res || !res.parse || !res.parse.text) {
             console.warn('[print] parse API returned no text for:', pageTitle, res);
             console.warn('[print] parse API returned no text for:', pageTitle);
             window.print();
             window.print();
             return;
             return;
Line 1,086: Line 1,114:
             var doc = iframe.contentDocument || iframe.contentWindow.document;
             var doc = iframe.contentDocument || iframe.contentWindow.document;


             // Print CSS URL (raw)
             // Raw Print.css URL (works regardless of script path)
             var printCssUrl = (mw.config.get('wgScriptPath') || '') +
             var printCssUrl = (mw.config.get('wgScriptPath') || getApiUrl().replace(/\/api\.php$/, '')) +
             '/index.php?title=MediaWiki:Print.css&action=raw&ctype=text/css';
             '/index.php?title=MediaWiki:Print.css&action=raw&ctype=text/css';


Line 1,100: Line 1,128:


             iframe.onload = function () {
             iframe.onload = function () {
             try {
             try { iframe.contentWindow.focus(); iframe.contentWindow.print(); }
                iframe.contentWindow.focus();
             finally { setTimeout(function(){ document.body.removeChild(iframe); }, 500); }
                iframe.contentWindow.print();
             } finally {
                setTimeout(function () { document.body.removeChild(iframe); }, 500);
            }
             };
             };
         }).fail(function (xhr) {
         }).fail(function (xhr) {
Line 1,112: Line 1,136:
         });
         });
     });
     });




Navigation menu