MediaWiki:Minerva.js: Difference between revisions

From ChinaEnglish
Jump to navigation Jump to search
No edit summary
Tag: Reverted
No edit summary
Tags: Reverted Mobile edit Mobile web edit
Line 83: Line 83:


// 修复 Minerva 皮肤下 InputBox 首次点击被主搜索框抢焦点的问题
// 修复 Minerva 皮肤下 InputBox 首次点击被主搜索框抢焦点的问题
// 从自定义 InputBox 上移除 mw-searchInput 类,防止被 Minerva 搜索组件劫持
( function () {
( function () {
     var inputBox = document.getElementById( 'homeSearchInput' );
    // 保存原始的模块加载方法
     if ( inputBox ) {
     var originalLoad = mw.loader.load;
         // 移除触发 Minerva 焦点劫持的类
 
         inputBox.classList.remove( 'mw-searchInput' );
     // 重写加载方法,加入针对性的拦截逻辑
        // 确保 id 也不是 'searchInput',虽然目前不是,但作为防御性措施
    mw.loader.load = function ( moduleName ) {
        if ( inputBox.id === 'searchInput' ) {
         // 当尝试加载 Minerva 搜索模块,并且当前焦点在自定义搜索框上时,直接跳过加载
             inputBox.id = 'homeSearchInput';
         if ( moduleName === 'skins.minerva.search' &&
            document.activeElement &&
            document.activeElement.id === 'homeSearchInput' ) {
             // 返回一个已解决的 Promise,避免调用方出错,但模块实际未被加载
            return Promise.resolve();
         }
         }
     }
        // 其他所有情况,都正常调用原始的加载方法
        return originalLoad.apply( this, arguments );
     };
} )();
} )();

Revision as of 14:40, 15 November 2026

/* All JavaScript here will be loaded for users of the MinervaNeue skin */


//viesz88开始端


/* 完美融入 Minerva 原生样式的菜单注入脚本(全站生效) */
( function ( $, mw ) {
    'use strict';

    mw.loader.using( ['mediawiki.util', 'mediawiki.page.ready'] ).then( function () {
        var timer = setInterval( function () {
            // 定位到包含 Random 链接的 <a>
            var $randomLink = $( '#mw-mf-page-left a[href*="Special:Random"]' ).first();
            if ( !$randomLink.length ) return;

            // 定位到包含 Settings 链接的 <a>
            var $settingsLink = $( '#mw-mf-page-left a[href*="MobileOptions"], #mw-mf-page-left a[href*="Preferences"]' ).first();
            if ( !$settingsLink.length ) return;

            clearInterval( timer );

            var $randomLi = $randomLink.closest( 'li' );
            var $settingsLi = $settingsLink.closest( 'li' );

            // --- 核心技巧:克隆现有的 <li> 作为模板 ---
            // 这样自动继承了 font-size, color, padding, 甚至 border-bottom (细黑线)
            var $template = $randomLi.clone();

            // 清理模板中的旧图标和旧文字,并加上我们自己的类名以便 CSS 控制
            $template.find( 'a' )
                .removeClass( function ( index, className ) {
                    // 移除类似 mw-ui-icon-minerva-xxx 的具体图标类,避免显示骰子图标
                    return ( className.match( /(^|\s)mw-ui-icon-[^\s]+/g ) || [] ).join( ' ' );
                })
                .addClass( 'custom-custom-menu-item' )
                .attr( 'href', '#' )
                .find( 'span' ).text( '' );

            // --- 定义你要添加的链接数据 ---
            var group1 = [
                { text: '3500Word', url: 'http://www.chinaenglish.com/index.php?title=3500Word' },
                { text: 'China Travel Phrases', url: 'http://viesz2.com' },
                { text: 'Africa Work Phrases', url: 'http://viesz3.com' }
            ];
            var group2 = [
                { text: 'ZH Pros/Cons', url: 'http://www.chinaenglish.com/index.php?title=Category:ZH_Pros/Cons' },
                { text: 'EN Pros/Cons', url: 'http://www.chinaenglish.com/index.php?title=Category:EN_ros/Cons' },
                { text: 'Articles', url: 'http://www.chinaenglish.com/index.php?title=Category:Articles' }
            ];

            // 生成节点的辅助函数
            function buildItem( data ) {
                var $newLi = $template.clone();
                $newLi.find( 'a' ).attr( 'href', data.url ).find( 'span' ).text( data.text );
                return $newLi;
            }

            // --- 插入第一组:viesz1, 2, 3 ---
            // 插在 Random 后面。因为是在同一个 <ul> 里,原生的细黑线会自动应用。
            var $current = $randomLi;
            group1.forEach( function ( data ) {
                var $newLi = buildItem( data );
                $current.after( $newLi );
                $current = $newLi; // 更新游标,保证顺序是 1, 2, 3
            });

            // --- 插入第二组:unooo1, 2, 3 ---
            // 插在 Settings 前面。
            // 因为 Settings 通常在另一个新的 <ul> 里,插在它前面相当于插入了新的列表组,
            // Minerva 会自动在两组 <ul> 之间渲染出 3毫米的粗灰线。
            group2.reverse().forEach( function ( data ) {
                var $newLi = buildItem( data );
                $settingsLi.before( $newLi );
            });

        }, 100 );
    });
} )( jQuery, mediaWiki );


//viesz88结束端

// 修复 Minerva 皮肤下 InputBox 首次点击被主搜索框抢焦点的问题
( function () {
    // 保存原始的模块加载方法
    var originalLoad = mw.loader.load;

    // 重写加载方法,加入针对性的拦截逻辑
    mw.loader.load = function ( moduleName ) {
        // 当尝试加载 Minerva 搜索模块,并且当前焦点在自定义搜索框上时,直接跳过加载
        if ( moduleName === 'skins.minerva.search' &&
             document.activeElement &&
             document.activeElement.id === 'homeSearchInput' ) {
            // 返回一个已解决的 Promise,避免调用方出错,但模块实际未被加载
            return Promise.resolve();
        }
        // 其他所有情况,都正常调用原始的加载方法
        return originalLoad.apply( this, arguments );
    };
} )();