打开/关闭菜单
打开/关闭外观设置菜单
打开/关闭个人菜单
未登录
未登录用户的IP地址会在进行任意编辑后公开展示。

MediaWiki:Common.js:修订间差异

MediaWiki界面页面
HW
HW留言 | 贡献
HW
HW留言 | 贡献
 
(未显示同一用户的3个中间版本)
第97行: 第97行:


//重定向
//重定向
/*
(function() {
(function() {
     const url = new URL(window.location.href);
     const url = new URL(window.location.href);
第112行: 第113行:
     }
     }
})();
})();
*/
//折叠
document.addEventListener('DOMContentLoaded', () => {
  // 找到页面上所有 .lm-collapsible
  document.querySelectorAll('.lm-collapsible').forEach(collapsible => {
    const toggle = collapsible.querySelector('.lm-toggle');
    const content = collapsible.querySelector('.lm-content');
    const icon = collapsible.querySelector('.lm-icon');
    if (!toggle || !content) return;
    // 点击切换
    toggle.addEventListener('click', () => {
      const isOpen = collapsible.classList.toggle('lm-open');
      // 切换 + / -
      icon.textContent = isOpen ? '−' : '+';
      // 可选:平滑展开/收起
      if (isOpen) {
        content.style.maxHeight = content.scrollHeight + 'px';
      } else {
        content.style.maxHeight = '0px';
      }
    });
    // 初始化为关闭状态
    collapsible.classList.remove('lm-open');
    if (content) content.style.maxHeight = '0px';
  });
});
// ==================== 折叠图片框 增强 JS ====================
mw.hook('wikipage.content').add(function($content) {
    // 自动修复图片折叠(确保 figure 完全隐藏)
    $content.find('.image-collapsible.mw-collapsed').each(function() {
        var $this = $(this);
        $this.find('figure, img').hide(); // 额外保险
    });
    // 可选:点击标题栏也能折叠(更像原生 App)
    $content.find('.image-collapsible .mw-collapsible-always-show').on('click', function(e) {
        if (!$(e.target).is('a')) {
            $(this).closest('.mw-collapsible').find('.mw-collapsible-toggle a').trigger('click');
        }
    });
});

2026年3月22日 (日) 20:31的最新版本

//用户名前显示头像
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: '1.5em',
						    height: '1.5em',
						    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);
    }
})();
*/

//折叠
document.addEventListener('DOMContentLoaded', () => {
  // 找到页面上所有 .lm-collapsible
  document.querySelectorAll('.lm-collapsible').forEach(collapsible => {
    const toggle = collapsible.querySelector('.lm-toggle');
    const content = collapsible.querySelector('.lm-content');
    const icon = collapsible.querySelector('.lm-icon');

    if (!toggle || !content) return;

    // 点击切换
    toggle.addEventListener('click', () => {
      const isOpen = collapsible.classList.toggle('lm-open');

      // 切换 + / -
      icon.textContent = isOpen ? '−' : '+';

      // 可选:平滑展开/收起
      if (isOpen) {
        content.style.maxHeight = content.scrollHeight + 'px';
      } else {
        content.style.maxHeight = '0px';
      }
    });

    // 初始化为关闭状态
    collapsible.classList.remove('lm-open');
    if (content) content.style.maxHeight = '0px';
  });
});
// ==================== 折叠图片框 增强 JS ====================
mw.hook('wikipage.content').add(function($content) {
    // 自动修复图片折叠(确保 figure 完全隐藏)
    $content.find('.image-collapsible.mw-collapsed').each(function() {
        var $this = $(this);
        $this.find('figure, img').hide(); // 额外保险
    });

    // 可选:点击标题栏也能折叠(更像原生 App)
    $content.find('.image-collapsible .mw-collapsible-always-show').on('click', function(e) {
        if (!$(e.target).is('a')) {
            $(this).closest('.mw-collapsible').find('.mw-collapsible-toggle a').trigger('click');
        }
    });
});