MediaWiki:Gadget-ShowAvatar.js

Redayano留言 | 贡献2026年7月16日 (四) 21:21的版本
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)

注意:这类代码页面在保存之后,您可能需要清除浏览器缓存才能看到所作出的变更的影响。

$(function() {
    var username = mw.config.get('wgUserName');
    var pageName = mw.config.get("wgPageName").replace(/^user:/i, '');
    
    // The new BlueSpice Avatar URL format
    var avatarUrlBase = '/rest.php/mws/v1/dynamic-file-dispatcher/userprofileimage?username=';

    // 1. Top right personal menu avatar
    if ( username ) {
        var img = $('<img>')
            .attr('src', avatarUrlBase + encodeURIComponent(username))
            .on('error', function() { img.attr('src', '/index.php/Special:FilePath/Moehime.jpg') })
            .css({
                width: '1.2em',
                height: '1.2em',
                'border-radius': '10px',
                'vertical-align': 'middle',
                'margin-right': '4px'
            });
            
        var link = $('<a>')
            // BlueSpice Avatars uses the UserProfile page for uploads, not Special:上传头像
            .attr('href', '/Special:UserProfile') 
            .append(img);
            
        $('#pt-userpage').before($('<li id="pt-avatar"></li>').append(link));
    }

    // 2. Avatar next to the title on User pages
    if (mw.config.get("wgNamespaceNumber") === 2 && !mw.config.get("wgPageName").includes("/")) {
        $('.ns-2 #firstHeading').prepend(
            $('<a>').attr({
                href: '/Special:UserProfile/' + encodeURIComponent(pageName),
                title: '查看头像'
            }).prepend(
                $('<img>')
                .attr('src', avatarUrlBase + encodeURIComponent(pageName))
                .css({
                    width: '1.2em',
                    height: '1.2em',
                    'border-radius': '10px',
                    padding: '4px'
                })
            )
        );
    }
});