跳转到内容

MediaWiki:Common.js:修订间差异

来自Minimalist Tower Defense Wiki
Jerry c9​(留言 | 贡献)
小无编辑摘要
标签:已被回退
Jerry c9​(留言 | 贡献)
小 撤销Jerry c9(讨论)的修订版本8654
标签:撤销
第95行: 第95行:
   else { start(); }
   else { start(); }
} )();
} )();
/* 灰白里程:把分区页头提到正文最前面 */
(function () {
  mw.hook('wikipage.content').add(function ($content) {
    var root = $content && $content[0];
    if (!root) return;
    var hero = root.querySelector('.gbm-hero');
    if (hero && hero.parentNode === root && root.firstElementChild !== hero) {
      root.insertBefore(hero, root.firstElementChild);
    }
  });
})();

2026年11月11日 (三) 13:54的版本

/* 这里的任何JavaScript将为所有用户在每次页面加载时加载。 */
/* 灰白里程有尽矿井计算器 */
( function () {
  'use strict';
  var state = { x: 0 };
  var recs = [];                 // { node, expr, unit, raw }

  function toJS( raw ) {
    var s = String( raw ).trim();
    s = s.replace( /×/g, '*' ).replace( /÷/g, '/' )
         .replace( /(/g, '(' ).replace( /)/g, ')' )
         .replace( /[-—–−]/g, '-' ).replace( /\s+/g, '' );
    s = s.replace( /(\d)(x)/g, '$1*$2' ).replace( /(x)(\d)/g, '$1*$2' )
         .replace( /(\d)\(/g, '$1*(' ).replace( /\)(\d)/g, ')*$1' )
         .replace( /\)(x)/g, ')*$1' ).replace( /(x)\(/g, '$1*(' )
         .replace( /⌊([^⌋]*)⌋/g, function ( m, inner ) { return 'Math.floor(' + inner + ')'; } );
    return s;
  }
  function evalExpr( raw, x ) {
    try {
      var v = ( new Function( 'x', 'return (' + toJS( raw ) + ');' ) )( x );
      if ( typeof v !== 'number' || !isFinite( v ) ) return null;
      return Math.round( v );
    } catch ( e ) { return null; }
  }
  function asFormula( text ) {
    var s = String( text ).trim();
    if ( s.length < 2 || s.length > 80 ) return null;
    if ( /[\u4e00-\u9fff]/.test( s ) ) return null;
    var unit = '', m = s.match( /^(.*?)([GC])$/ ), body = m ? m[1].trim() : s;
    if ( m ) unit = m[2];
    if ( body.indexOf( 'x' ) === -1 ) return null;
    if ( !/[+\-×*⌊⌋]/.test( body ) ) return null;
    if ( !/^[0-9x+\-×*/().⌊⌋\s]+$/.test( body ) ) return null;
    return { expr: body, unit: unit, raw: s };
  }
  function skipNode( node ) {
    var p = node.parentNode;
    if ( !p ) return true;
    var tag = ( p.nodeName || '' ).toLowerCase();
    if ( tag === 'script' || tag === 'style' || tag === 'code' || tag === 'pre' ) return true;
    if ( p.closest && ( p.closest( '.calc-box' ) || p.closest( '.calc-out' ) ) ) return true;
    return false;
  }
  function collect( root ) {
    var out = [];
    if ( !root ) return out;
    var walker = document.createTreeWalker( root, NodeFilter.SHOW_TEXT, null ), node;
    while ( ( node = walker.nextNode() ) ) {
      if ( skipNode( node ) ) continue;
      var info = asFormula( node.nodeValue );
      if ( info ) out.push( { node: node, expr: info.expr, unit: info.unit, raw: info.raw } );
    }
    return out;
  }
  function sanitizeX( v ) {
    var n = Math.floor( Number( v ) );
    if ( !isFinite( n ) || n < 0 ) n = 0;
    return n;
  }
  function render() {
    for ( var i = 0; i < recs.length; i++ ) {
      var r = recs[ i ], v = evalExpr( r.expr, state.x );
      r.node.nodeValue = ( v === null ? r.raw : String( v ) + r.unit );
    }
  }
  function buildInput( box ) {
    if ( box.querySelector( '.calc-input' ) ) return;
    var labelText = box.textContent.trim() || '数量 x';
    box.textContent = '';
    var label = document.createElement( 'label' );
    label.className = 'calc-label';
    label.appendChild( document.createTextNode( labelText.replace( /[::]\s*$/, '' ) + ':' ) );
    var input = document.createElement( 'input' );
    input.type = 'number'; input.min = '0'; input.step = '1';
    input.value = String( state.x ); input.className = 'calc-input';
    input.setAttribute( 'inputmode', 'numeric' );
    input.setAttribute( 'aria-label', labelText );
    input.addEventListener( 'input', function () { state.x = sanitizeX( input.value ); render(); } );
    input.addEventListener( 'blur', function () {
      state.x = sanitizeX( input.value ); input.value = String( state.x ); render();
    } );
    label.appendChild( input );
    box.appendChild( label );
  }
  function eachNode( sel, fn ) { Array.prototype.forEach.call( document.querySelectorAll( sel ), fn ); }
  function start() {
    for ( var i = 0; i < recs.length; i++ ) { try { recs[ i ].node.nodeValue = recs[ i ].raw; } catch ( e ) {} }
    recs = collect( document.querySelector( '.mw-parser-output' ) || document.body );
    eachNode( '.calc-box', buildInput );
    render();
  }
  if ( window.mw && mw.hook ) { mw.hook( 'wikipage.content' ).add( start ); }
  if ( document.readyState === 'loading' ) { document.addEventListener( 'DOMContentLoaded', start ); }
  else { start(); }
} )();