MediaWiki:Common.js:修订间差异

MediaWiki界面页面
无编辑摘要
无编辑摘要
第1行: 第1行:
/* =====================================================
  服务器状态实时显示
  数据源:https://api.mcsrvstat.us/3/game.xhcity.top
  显示元素:#xh-server-status
  ===================================================== */
( function () {
( function () {
     var el = document.getElementById( 'xh-server-status' );
     function run() {
    if ( !el ) return;
        var el = document.getElementById( 'xh-server-status' );
        if ( !el ) return;


    fetch( 'https://api.mcsrvstat.us/3/game.xhcity.top', {
        var ctrl = new AbortController();
        headers: { 'User-Agent': 'xhcity-wiki-status-widget/1.0' }
        var timer = setTimeout( function () { ctrl.abort(); }, 6000 );
    } )
 
        .then( function ( r ) {
        fetch( 'https://api.mcsrvstat.us/3/game.xhcity.top', {
            if ( !r.ok ) throw new Error( r.status );
            signal: ctrl.signal,
            return r.json();
            cache: 'no-store'
         } )
         } )
        .then( function ( data ) {
            .then( function ( r ) {
            if ( data.online ) {
                clearTimeout( timer );
                var online = data.players ? data.players.online : 0;
                if ( !r.ok ) throw new Error( r.status );
                var max = data.players ? data.players.max : '?';
                return r.json();
                el.innerHTML = '<span style="color:#17b3a8; font-weight:700;">● 服务器运行中</span>' +
            } )
                    ' &nbsp;·&nbsp; 在线人数:<b style="color:#111827;">' + online + '</b> / ' + max;
            .then( function ( data ) {
            } else {
                if ( data.online ) {
                el.innerHTML = '<span style="color:#d22; font-weight:700;">● 服务器已离线</span>';
                    var online = data.players ? data.players.online : 0;
            }
                    var max = data.players ? data.players.max : '?';
        } )
                    el.innerHTML = '<span style="color:#17b3a8; font-weight:700;">● 服务器运行中</span>' +
        .catch( function ( err ) {
                        ' &nbsp;·&nbsp; 在线人数:<b style="color:#111827;">' + online + '</b> / ' + max;
            el.innerHTML = '<span style="color:#d22;">● 状态获取失败(' + err.message + ')</span>';
                } else {
         } );
                    el.innerHTML = '<span style="color:#d22; font-weight:700;">● 服务器已离线</span>';
                }
            } )
            .catch( function () {
                el.innerHTML = '<span style="color:#9ca3af;">● 状态暂时无法获取</span>';
            } );
    }
 
    /* 等首屏空闲后再请求,避免和页面渲染抢资源 */
    if ( 'requestIdleCallback' in window ) {
         requestIdleCallback( run, { timeout: 3000 } );
    } else {
        setTimeout( run, 1500 );
    }
} )();
} )();

2026年9月23日 (三) 20:06的版本

( function () {
    function run() {
        var el = document.getElementById( 'xh-server-status' );
        if ( !el ) return;

        var ctrl = new AbortController();
        var timer = setTimeout( function () { ctrl.abort(); }, 6000 );

        fetch( 'https://api.mcsrvstat.us/3/game.xhcity.top', {
            signal: ctrl.signal,
            cache: 'no-store'
        } )
            .then( function ( r ) {
                clearTimeout( timer );
                if ( !r.ok ) throw new Error( r.status );
                return r.json();
            } )
            .then( function ( data ) {
                if ( data.online ) {
                    var online = data.players ? data.players.online : 0;
                    var max = data.players ? data.players.max : '?';
                    el.innerHTML = '<span style="color:#17b3a8; font-weight:700;">● 服务器运行中</span>' +
                        ' &nbsp;·&nbsp; 在线人数:<b style="color:#111827;">' + online + '</b> / ' + max;
                } else {
                    el.innerHTML = '<span style="color:#d22; font-weight:700;">● 服务器已离线</span>';
                }
            } )
            .catch( function () {
                el.innerHTML = '<span style="color:#9ca3af;">● 状态暂时无法获取</span>';
            } );
    }

    /* 等首屏空闲后再请求,避免和页面渲染抢资源 */
    if ( 'requestIdleCallback' in window ) {
        requestIdleCallback( run, { timeout: 3000 } );
    } else {
        setTimeout( run, 1500 );
    }
} )();