打开/关闭菜单
26
6679
46
1.2万
OGAS数据中枢
打开/关闭外观设置菜单
打开/关闭个人菜单
未登录
未登录用户的IP地址会在进行任意编辑后公开展示。

Widget:GameSwitcher:修订间差异

来自OGAS数据中枢
弃权者留言 | 贡献
无编辑摘要
弃权者留言 | 贡献
无编辑摘要
标签手工回退
 
(未显示同一用户的19个中间版本)
第1行: 第1行:
<script type="text/javascript">
<noinclude>
document.addEventListener('DOMContentLoaded', function () {
少女前线系列多游戏首页切换Widget。调用方式:<code><nowiki>{{#widget:GameSwitcher}}</nowiki></code>
   var switcher = document.querySelector('.game-switcher');
</noinclude><includeonly><script>
  if (!switcher) return;
( function () {
   'use strict';


   var heroWrap = document.getElementById('gs-hero-wrap');
   var heroImages = {
  var heroImg = heroWrap ? heroWrap.querySelector('img') : null;
    'gf':  'index_gf_hero.jpg',
  var loaded = {};
    'gf2''index_gf2_hero.jpg',
   var prefetching = {};
    'gfnc': 'index_gfnc_hero.jpg',
  var storageKey = 'gs-cache-';
    'gfb': 'index_gfb_hero.jpg'
   };


   // ============ 初始化 ============
   function getApiBase() {
  var firstBtn = switcher.querySelector('.gs-tab-btn');
    var canonical = document.querySelector( 'link[rel="canonical"]' );
  if (firstBtn) {
    if ( canonical ) {
    firstBtn.classList.add('gs-active');
      var m = canonical.href.match( /^(https?:\/\/[^\/]+(?:\/[^\/]+)*?)\/wiki\// );
    firstBtn.setAttribute('aria-selected', 'true');
      if ( m ) { return m[ 1 ] + '/api.php'; }
    if (heroImg && !heroImg.getAttribute('src')) {
      heroImg.src = firstBtn.dataset.hero;
     }
     }
    return window.location.origin + '/api.php';
   }
   }
  if (heroWrap) heroWrap.style.opacity = '1';


   // ============ 点击 ============
   function getWikiImageUrl( filename, callback ) {
  switcher.addEventListener('click', function (e) {
    fetch( getApiBase() + '?action=query&titles=File:' + encodeURIComponent( filename ) +
    var btn = e.target.closest('.gs-tab-btn');
          '&prop=imageinfo&iiprop=url&format=json&origin=*' )
    if (btn) activate(btn);
      .then( function ( r ) { return r.json(); } )
  });
      .then( function ( data ) {
        var pages = data.query.pages;
        var page = pages[ Object.keys( pages )[ 0 ] ];
        if ( page.imageinfo && page.imageinfo[ 0 ] ) {
          callback( page.imageinfo[ 0 ].url );
        }
      } )
      .catch( function ( e ) { console.warn( 'GameSwitcher: 图片加载失败', filename, e ); } );
  }


   // ============ 键盘 ============
   function applyFadeColor() {
  switcher.addEventListener('keydown', function (e) {
     var style = getComputedStyle( document.documentElement );
     var btn = e.target.closest('.gs-tab-btn');
    var bg = style.getPropertyValue( '--color-surface-0' ).trim()
    if (!btn) return;
          || style.getPropertyValue( '--background-color-base' ).trim()
    if (e.key === 'Enter' || e.key === ' ') {
          || '#1d1e20';
      e.preventDefault();
    var el = document.getElementById( 'gf-hero-fade-bottom' );
       activate(btn);
    if ( el ) {
       el.style.background = 'linear-gradient(to bottom, transparent 0%, ' + bg + ' 100%)';
     }
     }
    if (e.key === 'ArrowRight' || e.key === 'ArrowLeft') {
   }
      e.preventDefault();
      var btns = Array.from(switcher.querySelectorAll('.gs-tab-btn'));
      var idx = btns.indexOf(btn);
      var next = e.key === 'ArrowRight' ? idx + 1 : idx - 1;
      if (next >= 0 && next < btns.length) btns[next].focus();
    }
   });


  // ============ 悬停预加载 ============
function moveIndicator( tab ) {
  switcher.addEventListener('mouseenter', function (e) {
  var indicator = document.getElementById( 'gf-tab-indicator' );
    var btn = e.target.closest('.gs-tab-btn');
  if ( !indicator ) { return; }
    if (!btn) return;
  indicator.style.width     = tab.offsetWidth + 'px';
     var panelId = btn.dataset.panel;
  indicator.style.transform = 'translateX(' + tab.offsetLeft + 'px)';
    var pageName = btn.dataset.page;
}
    if (pageName && !loaded[panelId] && !prefetching[panelId]) {
      prefetch(panelId, pageName);
    }
  }, true);


   // ============ 激活面板 ============
   var currentGame = 'gf';
   function activate(btn) {
  var switching  = false;
     if (btn.classList.contains('gs-active')) return;
   var pending     = null;


     var panelId = btn.dataset.panel;
  function doSwitch( game, tabEl ) {
     var heroSrc = btn.dataset.hero;
     var slides = document.querySelectorAll( '#gf-homepage .gf-hero-slide' );
     var pageName = btn.dataset.page;
     var panels = document.querySelectorAll( '#gf-homepage .gf-content-panel' );
     var tabs  = document.querySelectorAll( '#gf-homepage .gf-tab' );


     // 头图
     var outSlide = document.getElementById( 'hero-' + currentGame );
     switchHero(heroSrc);
     var inSlide  = document.getElementById( 'hero-' + game );


     // 按钮高亮
     if ( inSlide ) {
    switcher.querySelectorAll('.gs-tab-btn').forEach(function (b) {
       inSlide.style.zIndex = '1';
       b.classList.remove('gs-active');
       inSlide.classList.add( 'active' );
       b.setAttribute('aria-selected', 'false');
     }
    });
     if ( outSlide ) {
    btn.classList.add('gs-active');
       outSlide.style.zIndex = '0';
     btn.setAttribute('aria-selected', 'true');
       outSlide.classList.remove( 'active' );
 
     // 面板切换
    switcher.querySelectorAll('.gs-content-panel').forEach(function (p) {
       p.classList.remove('gs-active');
       p.classList.remove('gs-anim');
    });
 
    var panel = document.getElementById(panelId);
    if (!panel) {
      panel = document.createElement('div');
      panel.id = panelId;
      panel.className = 'gs-content-panel';
      switcher.querySelector('.gs-content').appendChild(panel);
      loadContent(panel, panelId, pageName);
     }
     }


     panel.classList.add('gs-active');
     tabs.forEach( function ( t ) {
    panel.offsetWidth; // 强制重排,确保动画触发
      var on = t.getAttribute( 'data-game' ) === game;
    panel.classList.add('gs-anim');
      t.classList.toggle( 'active', on );
      t.setAttribute( 'aria-selected', on ? 'true' : 'false' );
    } );
    if ( tabEl ) { moveIndicator( tabEl ); }


     schedulePrefetchOthers();
     panels.forEach( function ( p ) {
  }
      p.classList.toggle( 'active', p.id === 'content-' + game );
    } );


  // ============ 头图切换 ============
    currentGame = game;
  function switchHero(url) {
     switching  = true;
    if (!heroWrap || !heroImg || !url) return;
     if (heroImg.src === url) return;


     var next = new Image();
     setTimeout( function () {
    next.onload = function () {
      slides.forEach( function ( s ) { s.style.zIndex = ''; } );
      heroWrap.style.opacity = '0';
       switching = false;
       setTimeout(function () {
      if ( pending && pending.game !== currentGame ) {
         heroImg.src = url;
         var p = pending;
         heroWrap.style.opacity = '1';
        pending = null;
       }, 300);
         doSwitch( p.game, p.tabEl );
    };
       } else {
     next.src = url;
        pending = null;
      }
     }, 520 );
   }
   }


   // ============ 内容加载 ============
   function switchGame( game, tabEl ) {
  function loadContent(panel, panelId, pageName) {
    if ( game === currentGame ) { return; }
    if (loaded[panelId]) {
    if ( switching ) {
      panel.innerHTML = loaded[panelId];
      pending = { game: game, tabEl: tabEl };
      var tabs = document.querySelectorAll( '#gf-homepage .gf-tab' );
      tabs.forEach( function ( t ) {
        var on = t.getAttribute( 'data-game' ) === game;
        t.classList.toggle( 'active', on );
        t.setAttribute( 'aria-selected', on ? 'true' : 'false' );
      } );
      if ( tabEl ) { moveIndicator( tabEl ); }
       return;
       return;
     }
     }
     try {
     doSwitch( game, tabEl );
      var stored = localStorage.getItem(storageKey + panelId);
  }
      if (stored) {
        loaded[panelId] = stored;
        panel.innerHTML = stored;
        return;
      }
    } catch (e) {}


    if (!pageName) return;
  function init() {
     panel.innerHTML = '<div class="gs-loading">加载中...</div>';
     var tabs = document.querySelectorAll( '#gf-homepage .gf-tab' );
    if ( !tabs.length ) { return; }


     fetchPage(pageName).then(function (html) {
     Object.keys( heroImages ).forEach( function ( game ) {
       loaded[panelId] = html;
       getWikiImageUrl( heroImages[ game ], function ( url ) {
      try { localStorage.setItem(storageKey + panelId, html); } catch (e) {}
        var bg  = document.getElementById( 'hero-' + game + '-bg' );
      panel.innerHTML = html;
        var fg  = document.getElementById( 'hero-' + game + '-fg' );
    }).catch(function () {
        var val = 'url("' + url + '")';
       panel.innerHTML = '<div class="gs-loading">加载失败,<a href="javascript:location.reload()">刷新</a>重试</div>';
        if ( bg ) { bg.style.backgroundImage = val; }
     });
        if ( fg ) { fg.style.backgroundImage = val; }
  }
       } );
     } );


  // ============ 预加载 ============
     applyFadeColor();
  function prefetch(panelId, pageName) {
    prefetching[panelId] = true;
     try {
      var stored = localStorage.getItem(storageKey + panelId);
      if (stored) {
        loaded[panelId] = stored;
        prefetching[panelId] = false;
        return;
      }
    } catch (e) {}


     fetchPage(pageName).then(function (html) {
     var firstActive = document.querySelector( '#gf-homepage .gf-tab.active' );
      loaded[panelId] = html;
    if ( firstActive ) {
      try { localStorage.setItem(storageKey + panelId, html); } catch (e) {}
      setTimeout( function () { moveIndicator( firstActive ); }, 50 );
    }).finally(function () {
    }
      prefetching[panelId] = false;
    });
  }


  function schedulePrefetchOthers() {
    tabs.forEach( function ( tab ) {
    setTimeout(function () {
      tab.addEventListener( 'click', function () {
      switcher.querySelectorAll('.gs-tab-btn').forEach(function (btn) {
        switchGame( this.getAttribute( 'data-game' ), this );
         var panelId = btn.dataset.panel;
      } );
        var pageName = btn.dataset.page;
      tab.addEventListener( 'keydown', function ( e ) {
        if (!loaded[panelId] && !prefetching[panelId] && pageName) {
         if ( e.key === 'Enter' || e.key === ' ' ) {
           prefetch(panelId, pageName);
          e.preventDefault();
           switchGame( this.getAttribute( 'data-game' ), this );
         }
         }
       });
       } );
     }, 500);
     } );
 
    window.addEventListener( 'resize', function () {
      var active = document.querySelector( '#gf-homepage .gf-tab.active' );
      if ( active ) { moveIndicator( active ); }
    } );
   }
   }


   // ============ API 请求 ============
   if ( document.readyState === 'loading' ) {
  function fetchPage(pageName) {
     document.addEventListener( 'DOMContentLoaded', init );
     var url = mw.util.wikiScript('api') +
  } else {
      '?action=parse&page=' + encodeURIComponent(pageName) +
    init();
      '&prop=text&format=json&disablelimitreport=1&origin=*';
    return fetch(url)
      .then(function (r) { return r.json(); })
      .then(function (data) {
        if (data.parse && data.parse.text) return data.parse.text['*'];
        throw new Error('empty');
      });
   }
   }
});
 
</script>
} )();
</script></includeonly>

2026年6月20日 (六) 23:23的最新版本

少女前线系列多游戏首页切换Widget。调用方式:{{#widget:GameSwitcher}}