MediaWiki:Common.js

来自神国反恶俗
七叶​(留言 | 贡献)2026年10月11日 (日) 14:31的版本
跳到导航跳到搜索

注意:在发布之后,您可能需要清除浏览器缓存才能看到所作出的更改的影响。

  • Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5或Ctrl-R(Mac为⌘-R)
  • Google Chrome:按Ctrl-Shift-R(Mac为⌘-Shift-R)
  • Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5。
/* 这里的任何JavaScript将为所有用户在每次页面加载时加载。 */
( function () {

    /* ==========================================================
       一、把页面里的目录(TOC)搬到左侧栏
       —— 你原来就有的功能,保持不动
       ========================================================== */
    function moveTOC() {
        var toc = document.getElementById( 'toc' )
            || document.querySelector( '.mw-table-of-contents-container' )
            || document.querySelector( '.mw-table-of-contents' );
        var sidebar = document.getElementById( 'column-one' )
            || document.getElementById( 'mw-panel' );

        if ( !toc || !sidebar || document.getElementById( 'p-toc' ) ) {
            return;
        }

        var box = document.createElement( 'div' );
        box.className = 'portlet';
        box.id = 'p-toc';

        var title = document.createElement( 'h3' );
        title.textContent = '目录';

        var inner = document.createElement( 'div' );
        inner.className = 'pBody';
        inner.appendChild( toc );

        box.appendChild( title );
        box.appendChild( inner );
        sidebar.appendChild( box );
    }

    /* ==========================================================
       二、吸顶:左侧栏 / 顶部编辑标签栏 / 右上账号栏
       —— 按页面实际有没有这些栏目动态处理,不会多留空白
       ========================================================== */
    var STICKY_CSS = [
        '#sg-personal-bar,#sg-cactions-bar{',
        '  position:fixed;top:0;z-index:401;',
        '  background:#fff;box-shadow:0 1px 4px rgba(0,0,0,.18);',
        '  padding:4px 10px;font-size:88%;line-height:1.4;',
        '  white-space:nowrap;overflow-x:auto;overflow-y:hidden;',
        '}',
        '#sg-personal-bar{right:0;border-radius:0 0 0 8px;}',
        '#sg-cactions-bar{border-radius:0 0 8px 0;}',
        '#sg-personal-bar a,#sg-cactions-bar a{',
        '  color:#36c;text-decoration:none;padding:2px 5px;',
        '}',
        '#sg-personal-bar a:hover,#sg-cactions-bar a:hover{',
        '  text-decoration:underline;',
        '}',
        '#sg-cactions-bar a{border-right:1px solid #e3e5e8;}',
        '#sg-cactions-bar a:last-child{border-right:none;}',
        '#column-one{',
        '  position:sticky;top:var(--sg-top-offset,0px);',
        '  max-height:calc(100vh - var(--sg-top-offset,0px));',
        '  overflow-y:auto;',
        '}',
        'body.sg-sticky-on #content{',
        '  padding-top:var(--sg-top-offset,0px) !important;',
        '}'
    ].join( '\n' );

    function injectCSS() {
        if ( document.getElementById( 'sg-sticky-style' ) ) { return; }
        var s = document.createElement( 'style' );
        s.id = 'sg-sticky-style';
        s.type = 'text/css';
        s.textContent = STICKY_CSS;
        document.head.appendChild( s );
    }

    /* 把某个栏目里的链接复制到一条固定栏上;栏目为空就不做 */
    function buildBar( portlet, barId ) {
        if ( !portlet ) { return null; }

        var links = portlet.querySelectorAll( 'ul a' );
        if ( !links.length ) { return null; }

        var old = document.getElementById( barId );
        if ( old && old.parentNode ) { old.parentNode.removeChild( old ); }

        var bar = document.createElement( 'div' );
        bar.id = barId;

        Array.prototype.forEach.call( links, function ( a ) {
            var copy = document.createElement( 'a' );
            copy.href = a.href;
            copy.textContent = a.textContent;
            bar.appendChild( copy );
        } );

        document.body.appendChild( bar );
        return bar;
    }

    function makeSticky() {
        injectCSS();

        var personal = document.getElementById( 'p-personal' );  /* 账号那排 */
        var cactions = document.getElementById( 'p-cactions' );  /* 编辑标签那排 */

        /* 账号栏:永远吸顶、靠右 */
        if ( buildBar( personal, 'sg-personal-bar' ) && personal ) {
            personal.style.visibility = 'hidden';
        }

        /* 编辑标签栏:吸顶,排在账号栏下面 */
        var ca = buildBar( cactions, 'sg-cactions-bar' );
        if ( ca && cactions ) { cactions.style.visibility = 'hidden'; }

        /* 算出顶部实际占多高,动态决定正文留白 */
        var topH = 0;
        var pBar = document.getElementById( 'sg-personal-bar' );
        if ( pBar ) { topH += pBar.offsetHeight; }
        if ( ca ) {
            ca.style.top = topH + 'px';
            topH += ca.offsetHeight;
        }

        document.documentElement.style.setProperty( '--sg-top-offset', topH + 'px' );

        if ( topH > 0 ) {
            document.body.classList.add( 'sg-sticky-on' );
        } else {
            document.body.classList.remove( 'sg-sticky-on' );
        }
    }

    /* ==========================================================
       启动
       ========================================================== */
    function run() {
        moveTOC();
        makeSticky();
    }

    if ( document.readyState === 'loading' ) {
        document.addEventListener( 'DOMContentLoaded', run );
    } else {
        run();
    }

    window.addEventListener( 'resize', function () {
        clearTimeout( window.__sgTimer );
        window.__sgTimer = setTimeout( makeSticky, 150 );
    } );

}() );