Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

MediaWiki:Common.js: Difference between revisions

MediaWiki interface page
No edit summary
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 356: Line 356:
});
});


mw.loader.using(['jquery', 'mediawiki.util'], function () {
mw.loader.using(['jquery', 'mediawiki.api'], function () {
     $(function () {
     $(function () {
         $('.main-banner[data-files]').each(function () {
         $('.main-banner[data-files]').each(function () {
             var $box = $(this);
             var $box = $(this);
 
              
             // Parse list: pipe or comma separated
             var list = String($box.attr('data-files'))
             var list = String($box.attr('data-files'))
                 .split(/\s*\|\s*|\s*,\s*/)
                 .split(/\s*\|\s*|\s*,\s*/)
Line 368: Line 367:
             if (!list.length) return;
             if (!list.length) return;


            // Pick a random filename
             var pick = list[Math.floor(Math.random() * list.length)];
             var pick = list[Math.floor(Math.random() * list.length)];


             // Build a local URL to the raw file
             // Use MediaWiki API to resolve actual file path
             var src = mw.util.getUrl('Special:FilePath/' + pick);
             new mw.Api().get({
 
                action: 'query',
            // Create the <img>
                titles: 'File:' + pick,
            var $img = $('<img>', {
                prop: 'imageinfo',
                src: src,
                iiprop: 'url',
                width: 2450,
                format: 'json'
                height: 450,
            }).done(function (data) {
                alt: ''
                var pages = data.query.pages;
                for (var pageId in pages) {
                    if (pages.hasOwnProperty(pageId) && pages[pageId].imageinfo) {
                        var url = pages[pageId].imageinfo[0].url;
                        var $img = $('<img>', {
                            src: url,
                            width: 2450,
                            height: 450,
                            alt: ''
                        });
                        $box.empty().append($img);
                    }
                }
             });
             });
            // Optional link wrapper via data-link
            var href = $box.attr('data-link');
            if (href) {
                var $a = $('<a>', { href: href });
                $a.append($img);
                $box.empty().append($a);
            } else {
                $box.empty().append($img);
            }
         });
         });
     });
     });
});
});