MediaWiki:Common.js:修订间差异

HW
HW留言 | 贡献 (测试)
HW
HW留言 | 贡献
 
(未显示同一用户的22个中间版本)
第1行: 第1行:
/**
//用户名前显示头像
* 失传媒体维基 - 快捷分类按钮
mw.loader.using( 'mediawiki.util', function () {
* 在编辑页面顶部添加常用分类快捷按钮
    $( document ).ready( function () {
*/
        function addAvatarsToElements( $elements ) {
$( function() {
            $elements.each( function () {
    // 仅在编辑页面执行
                var $link = $( this );
    if ( mw.config.get( 'wgAction' ) !== 'edit' ) 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 quickCategories = [
                if ( !href ) return;
        // 状态分类
                var userPart = decodeURIComponent( href.split( '/wiki/' )[1] );
        { label: '丢失', category: '丢失', style: 'background:#ffcccc;' },
                if ( !userPart ) return;
        { label: '部分发现', category: '部分发现', style: 'background:#fff3cd;' },
                if ( userPart.indexOf( '/' ) !== -1 ) {
        { label: '发现', category: '发现', style: 'background:#d4edda;' },
                    return;
       
                }
        // 类型分类(分隔线后)
                var userName = userPart.replace( /^User:|^用户:/, '' ).replace(/_/g, ' ');
        { label: '视频', category: '视频' },
                var linkText = $link.text().trim().replace(/^User:|^用户:/, '');
        { label: '音频', category: '音频' },
                if ( linkText !== userName ) {
        { label: '图片', category: '图片' },
                    var $target = $link.find( 'span, bdi' ).first();
        { label: '游戏', category: '游戏' },
                    var targetText = $target.length ? $target.text().trim().replace(/^User:|^用户:/, '') : '';
        { label: '文本', category: '文本' }
                    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() ) {
    var $container = $( '<div>' )
                            return;
        .css( {
                        }
            'padding': '10px',
                        var currentUserName = mw.user.getName();
            'margin-bottom': '15px',
                        if ( userName !== currentUserName ) {
            'border': '1px solid #ccc',
                            return;
            'border-radius': '4px',
                        }
            'background': '#f9f9f9'
        } )
        .prepend( '<b>快捷分类:</b> ' );


    // 生成按钮
                        var avatarImg = $avatarWrapper.find( 'img' );
    quickCategories.forEach( function( item, index ) {
                        var avatarUrl = avatarImg.attr( 'src' );
        // 在第4个按钮后加分隔线
                        if ( avatarUrl && avatarUrl !== '/images/avatars/default_l.gif' ) {
        if ( index === 3 ) {
                            var $styleTag = $( '#dynamic-avatar-style' );
            $container.append( '<span style="margin:0 10px; border-left:1px solid #ccc;"></span>' );
                            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 } );
    } );
} );


        var $btn = $( '<button>' )
            .text( item.label )
            .attr( 'type', 'button' )
            .css( {
                'margin': '0 5px 5px 0',
                'padding': '4px 10px',
                'border': '1px solid #999',
                'border-radius': '3px',
                'cursor': 'pointer',
                'font-size': '0.9em'
            } )
            .css( item.style || {} ) // 应用自定义样式
            .on( 'click', function() {
                // 获取当前编辑框内容
                var textarea = $( '#wpTextbox1' )[0];
                var content = textarea.value;
                var categoryTag = '[[分类:' + item.category + ']]';


                // 检查分类是否已存在
                if ( content.indexOf( categoryTag ) === -1 ) {
                    // 在内容末尾添加分类
                    if ( !content.endsWith( '\n' ) ) {
                        content += '\n';
                    }
                    content += categoryTag + '\n';
                    textarea.value = content;
                    $( this ).css( 'opacity', '0.5' ).text( '已添加: ' + item.label );
                } else {
                    alert( '分类“' + item.category + '”已存在!' );
                }
            } );


        $container.append( $btn );
//重定向
     } );
(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) {
    $container.insertBefore( '#wpTextbox1' );
        const targetUrl = `/wiki/${encodeURIComponent(titleValue)}`;
} );
        window.location.replace(targetUrl);
    }
})();