跳转到内容

MediaWiki:Common.js:修订间差异

来自失传媒体中文维基
HW
HW留言 | 贡献
HW
HW留言 | 贡献
 
(未显示同一用户的29个中间版本)
第1行: 第1行:
/* 这里的任何JavaScript将为所有用户在每次页面加载时加载。 */
//用户名前显示头像
// 等待页面内容加载完成
mw.loader.using( 'mediawiki.util', function () {
mw.hook('wikipage.content').add(function() {
    $( document ).ready( function () {
    // 仅在 Minerva 皮肤下生效
        function addAvatarsToElements( $elements ) {
    if (mw.config.get('skin') !== 'minerva') return;
            $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;
}


    // 1. 获取当前页面信息
                var href = $link.attr( 'href' );
    const pageTitle = mw.config.get('wgPageName');
                if ( !href ) return;
    const isTalkPage = mw.config.get('wgNamespaceNumber') % 2 === 1; // 判断是否为讨论页
                var userPart = decodeURIComponent( href.split( '/wiki/' )[1] );
    const isExistingPage = mw.config.get('wgArticleId') > 0; // 判断页面是否存在
                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 );


    // 2. 构造核心链接
                        if ( mw.user.isAnon() ) {
    const editUrl = mw.util.getUrl(pageTitle, { action: 'edit' });
                            return;
    const historyUrl = mw.util.getUrl(pageTitle, { action: 'history' });
                        }
    const talkTitle = isTalkPage ? pageTitle.replace(/^Talk:/, '') : `Talk:${pageTitle}`;
                        var currentUserName = mw.user.getName();
    const talkUrl = mw.util.getUrl(talkTitle);
                        if ( userName !== currentUserName ) {
                            return;
                        }


    // 3. 创建底部按钮容器(样式模仿 Wikidot 的简洁风格)
                        var avatarImg = $avatarWrapper.find( 'img' );
    const bottomBar = document.createElement('div');
                        var avatarUrl = avatarImg.attr( 'src' );
    bottomBar.id = 'custom-wiki-bottom-bar';
                        if ( avatarUrl && avatarUrl !== '/images/avatars/default_l.gif' ) {
    bottomBar.style.cssText = `
                            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 } );
     } );
} );


    // 4. 定义按钮组
    const buttons = [
        {
            url: editUrl,
            text: isExistingPage ? '编辑页面' : '创建页面',
            color: '#3366cc' // 经典 MediaWiki 蓝
        },
        {
            url: historyUrl,
            text: '查看历史',
            color: '#72777d' // 中性灰
        },
        {
            url: talkUrl,
            text: isTalkPage ? '返回条目' : '讨论页',
            color: '#202122' // 深灰
        }
    ];


    // 5. 生成按钮并插入容器
    buttons.forEach(btn => {
        const link = document.createElement('a');
        link.href = btn.url;
        link.textContent = btn.text;
        link.style.cssText = `
            display: inline-block;
            margin: 0 0.75rem;
            padding: 0.5rem 1.25rem;
            background-color: ${btn.color};
            color: white;
            text-decoration: none;
            border-radius: 4px;
            font-size: 0.95rem;
            font-weight: 500;
            transition: opacity 0.2s;
        `;
        link.onmouseover = () => link.style.opacity = '0.85';
        link.onmouseout = () => link.style.opacity = '1';
        bottomBar.appendChild(link);
    });


        // 6. 将按钮栏插入到 main#content 内部的最底部
//重定向
     const content = document.querySelector('main#content, #content');
(function() {
    const url = new URL(window.location.href);
     const pathname = url.pathname;
    const searchParams = url.searchParams;
      
      
     if (content) {
     const isIndexPhp = pathname === '/index.php';
        bottomBar.style.marginTop = '2.5rem';
    const hasOnlyTitleParam = searchParams.size === 1 && searchParams.has('title');
         content.appendChild(bottomBar);
    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);
    }
})();