|
|
| 第1行: |
第1行: |
| /* 这里的任何JavaScript将为所有用户在每次页面加载时加载。 */ | | /* 这里的任何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(); }
| |
| } )();
| |
|
| |
| /* 灰白里程页头子页面标题精简、徽标跳转 */
| |
| (function () {
| |
| var ROOT = '灰白里程';
| |
|
| |
| function init() {
| |
| if (!/(^|\s)page-灰白里程/.test(document.body.className)) return;
| |
| if (!/(^|\s)action-view(\s|$)/.test(document.body.className)) return;
| |
|
| |
| var h1 = document.getElementById('firstHeading');
| |
| if (!h1) return;
| |
|
| |
| /*子页面标题精简*/
| |
| var span = h1.querySelector('.mw-page-title-main') || h1;
| |
| var text = (span.textContent || '').trim();
| |
| if (text.indexOf(ROOT + '/') === 0) {
| |
| span.textContent = text.slice(ROOT.length + 1);
| |
| }
| |
|
| |
| /* 徽标跳转 */
| |
| if (h1.querySelector('.gbm-hero-home')) return;
| |
| var path = mw.config.get('wgArticlePath') || '/index.php?title=$1';
| |
| var a = document.createElement('a');
| |
| a.className = 'gbm-hero-home';
| |
| a.href = path.replace('$1', encodeURIComponent(ROOT));
| |
| a.title = '返回' + ROOT;
| |
| a.setAttribute('aria-label', '返回' + ROOT);
| |
| h1.appendChild(a);
| |
| }
| |
|
| |
| if (document.readyState === 'loading') {
| |
| document.addEventListener('DOMContentLoaded', init);
| |
| } else {
| |
| init();
| |
| }
| |
| })();
| |
/* 这里的任何JavaScript将为所有用户在每次页面加载时加载。 */