跳转到内容

MediaWiki:Common.js:修订间差异

来自失传媒体中文维基
HW
HW留言 | 贡献
HW
HW留言 | 贡献
 
(未显示同一用户的16个中间版本)
第1行: 第1行:
// 用户名前显示头像
//用户名前显示头像
mw.loader.using( 'mediawiki.util', function () {
mw.loader.using( 'mediawiki.util', function () {
     $( document ).ready( function () {
     $( document ).ready( function () {
         // 选择用户链接
         function addAvatarsToElements( $elements ) {
        $( 'a[href*="/wiki/User:"], a[href*="/wiki/%E7%94%A8%E6%88%B7:"]' ).each( function () {
            $elements.each( function () {
            var $link = $( this );
                var $link = $( this );
           
                if ( $link.prev( '.sp-avatar-wrapper' ).length ) {
            // 防止重复添加
                    return;
            if ( $link.prev( '.sp-avatar-wrapper' ).length ) {
                 }
                 return;
            }
const excludeContainers = [
'#p-views',
'#p-cactions',
'#p-tb',
'#right-navigation',
'#p-personal',
'.mw-portlet-personal'
];
// 检查是否匹配任意一个排除容器
const isExcluded = excludeContainers.some(selector => $link.closest(selector).length);
if (isExcluded) {
return;
}


            // 提取用户名
                var href = $link.attr( 'href' );
            var href = $link.attr( 'href' );
                if ( !href ) return;
            var userName = decodeURIComponent( href.split( '/wiki/' )[1] ).replace( /^User:|^用户:/, '' );
                var userPart = decodeURIComponent( href.split( '/wiki/' )[1] );
                if ( !userPart ) return;
                if ( userPart.indexOf( '/' ) !== -1 ) {
                    return;
                }
                var userName = userPart.replace( /^User:|^用户:/, '' ).replace(/_/g, ' ');
                var linkText = $link.text().trim().replace(/^User:|^用户:/, '');
                if ( linkText !== userName ) {
                    var $target = $link.find( 'span, bdi' ).first();
                    var targetText = $target.length ? $target.text().trim().replace(/^User:|^用户:/, '') : '';
                    if ( !$target.length || targetText !== userName ) {
                        return;
                    }
                }
                new mw.Api().get( {
                    action: 'parse',
                    text: '{{#avatar:' + userName + '|l}}',
                    contentmodel: 'wikitext',
                    disablelimitreport: true,
                    disableeditsection: true
                } ).done( function ( data ) {
                    if ( data.parse && data.parse.text ) {
                        var avatarHtml = data.parse.text[ '*' ];
                        var $avatarWrapper = $( '<span class="sp-avatar-wrapper" style="vertical-align: middle; margin-right: 4px; display: inline-block;"></span>' ).html( avatarHtml );
                        $avatarWrapper.find( 'img' ).css( {
    width: '2em',
    height: '2em',
    objectFit: 'cover',
    display: 'inline-block',
    verticalAlign: 'middle',
    border: 'var(--background-color-neutral, #eaecf0) 2px solid',
    boxSizing: 'border-box'
} );
                        $link.before( $avatarWrapper );


            // 调用 API 获取高清头像
                        if ( mw.user.isAnon() ) {
            new mw.Api().get( {
                            return;
                action: 'query',
                        }
                list: 'users',
                        var currentUserName = mw.user.getName();
                ususers: userName,
                        if ( userName !== currentUserName ) {
                usprop: 'avatar',
                            return;
                // 请求 128px 或 256px 的原图 (根据你的 wiki 配置调整)
                         }
                usavatarsize: 128
            } ).done( function ( data ) {
                if ( data.query && data.query.users && data.query.users[0] && data.query.users[0].avatar ) {
                    var avatarUrl = data.query.users[0].avatar;
                   
                    // 创建图片元素
                    var $avatarImg = $( '<img>' ).attr( {
                        src: avatarUrl,
                        alt: userName,
                         style: 'vertical-align: middle; width: 20px; height: 20px; border-radius: 2px; object-fit: cover;'
                    } );


                    // 包裹元素并插入到链接前
                        var avatarImg = $avatarWrapper.find( 'img' );
                    var $avatarWrapper = $( '<span class="sp-avatar-wrapper" style="vertical-align: middle; margin-right: 4px; display: inline-block;"></span>' ).html( $avatarImg );
                        var avatarUrl = avatarImg.attr( 'src' );
                     $link.before( $avatarWrapper );
                        if ( avatarUrl && avatarUrl !== '/images/avatars/default_l.gif' ) {
                 }
                            var $styleTag = $( '#dynamic-avatar-style' );
                            if ( !$styleTag.length ) {
                                $styleTag = $( '<style id="dynamic-avatar-style"></style>' ).appendTo( 'head' );
                            }
                            var css = '.minerva-icon--userAvatarOutline { background-image: url("' + avatarUrl + '"); background-repeat: no-repeat; background-size: contain; width: 30px; height: 30px; mask-image: unset; }';
                            $styleTag.text( css );
                        }
                    }
                } );
            } );
        }
        addAvatarsToElements( $( 'a[href*="/wiki/User:"], a[href*="/wiki/%E7%94%A8%E6%88%B7:"], a[href*="/wiki/用户:"]' ) );
        var observer = new MutationObserver( function ( mutations ) {
            mutations.forEach( function ( mutation ) {
                $( mutation.addedNodes ).find( 'a[href*="/wiki/User:"], a[href*="/wiki/%E7%94%A8%E6%88%B7:"], a[href*="/wiki/用户:"]' ).each( function() {
                     addAvatarsToElements( $( this ) );
                 } );
             } );
             } );
         } );
         } );
        observer.observe( document.body, { childList: true, subtree: true } );
     } );
     } );
} );
} );
//重定向
(function() {
    const url = new URL(window.location.href);
    const pathname = url.pathname;
    const searchParams = url.searchParams;
   
    const isIndexPhp = pathname === '/index.php';
    const hasOnlyTitleParam = searchParams.size === 1 && searchParams.has('title');
    const titleValue = searchParams.get('title') || '';
    const hasValidTitle = titleValue.trim() !== '';
    if (isIndexPhp && hasOnlyTitleParam && hasValidTitle) {
        const targetUrl = `/wiki/${encodeURIComponent(titleValue)}`;
        window.location.replace(targetUrl);
    }
})();

2026年2月28日 (六) 09:22的最新版本

//用户名前显示头像
mw.loader.using( 'mediawiki.util', function () {
    $( document ).ready( function () {
        function addAvatarsToElements( $elements ) {
            $elements.each( function () {
                var $link = $( this );
                if ( $link.prev( '.sp-avatar-wrapper' ).length ) {
                    return;
                }
				
				const excludeContainers = [
					'#p-views', 
					'#p-cactions', 
					'#p-tb', 
					'#right-navigation', 
					'#p-personal', 
					'.mw-portlet-personal'
				];
					// 检查是否匹配任意一个排除容器
				const isExcluded = excludeContainers.some(selector => $link.closest(selector).length);
				if (isExcluded) {
					return;
				}

                var href = $link.attr( 'href' );
                if ( !href ) return;
                var userPart = decodeURIComponent( href.split( '/wiki/' )[1] );
                if ( !userPart ) return;
                if ( userPart.indexOf( '/' ) !== -1 ) {
                    return;
                }
                var userName = userPart.replace( /^User:|^用户:/, '' ).replace(/_/g, ' ');
                var linkText = $link.text().trim().replace(/^User:|^用户:/, '');
                if ( linkText !== userName ) {
                    var $target = $link.find( 'span, bdi' ).first();
                    var targetText = $target.length ? $target.text().trim().replace(/^User:|^用户:/, '') : '';
                    if ( !$target.length || targetText !== userName ) {
                        return;
                    }
                }
                new mw.Api().get( {
                    action: 'parse',
                    text: '{{#avatar:' + userName + '|l}}',
                    contentmodel: 'wikitext',
                    disablelimitreport: true,
                    disableeditsection: true
                } ).done( function ( data ) {
                    if ( data.parse && data.parse.text ) {
                        var avatarHtml = data.parse.text[ '*' ];
                        var $avatarWrapper = $( '<span class="sp-avatar-wrapper" style="vertical-align: middle; margin-right: 4px; display: inline-block;"></span>' ).html( avatarHtml );
                        $avatarWrapper.find( 'img' ).css( {
						    width: '2em',
						    height: '2em',
						    objectFit: 'cover',
						    display: 'inline-block',
						    verticalAlign: 'middle',
						    border: 'var(--background-color-neutral, #eaecf0) 2px solid',
						    boxSizing: 'border-box'
						} );
                        $link.before( $avatarWrapper );

                        if ( mw.user.isAnon() ) {
                            return;
                        }
                        var currentUserName = mw.user.getName();
                        if ( userName !== currentUserName ) {
                            return;
                        }

                        var avatarImg = $avatarWrapper.find( 'img' );
                        var avatarUrl = avatarImg.attr( 'src' );
                        if ( avatarUrl && avatarUrl !== '/images/avatars/default_l.gif' ) {
                            var $styleTag = $( '#dynamic-avatar-style' );
                            if ( !$styleTag.length ) {
                                $styleTag = $( '<style id="dynamic-avatar-style"></style>' ).appendTo( 'head' );
                            }
                            var css = '.minerva-icon--userAvatarOutline { background-image: url("' + avatarUrl + '"); background-repeat: no-repeat; background-size: contain; width: 30px; height: 30px; mask-image: unset; }';
                            $styleTag.text( css );
                        }
                    }
                } );
            } );
        }
        addAvatarsToElements( $( 'a[href*="/wiki/User:"], a[href*="/wiki/%E7%94%A8%E6%88%B7:"], a[href*="/wiki/用户:"]' ) );
        var observer = new MutationObserver( function ( mutations ) {
            mutations.forEach( function ( mutation ) {
                $( mutation.addedNodes ).find( 'a[href*="/wiki/User:"], a[href*="/wiki/%E7%94%A8%E6%88%B7:"], a[href*="/wiki/用户:"]' ).each( function() {
                    addAvatarsToElements( $( this ) );
                } );
            } );
        } );
        observer.observe( document.body, { childList: true, subtree: true } );
    } );
} );



//重定向
(function() {
    const url = new URL(window.location.href);
    const pathname = url.pathname;
    const searchParams = url.searchParams;
    
    const isIndexPhp = pathname === '/index.php';
    const hasOnlyTitleParam = searchParams.size === 1 && searchParams.has('title');
    const titleValue = searchParams.get('title') || '';
    const hasValidTitle = titleValue.trim() !== '';

    if (isIndexPhp && hasOnlyTitleParam && hasValidTitle) {
        const targetUrl = `/wiki/${encodeURIComponent(titleValue)}`;
        window.location.replace(targetUrl);
    }
})();