MediaWiki:Minerva.js: Difference between revisions

No edit summary
Tags: Reverted Mobile edit Mobile web edit
No edit summary
Tag: Reverted
Line 84: Line 84:
// 修复 Minerva 皮肤下 InputBox 首次点击被主搜索框抢焦点的问题
// 修复 Minerva 皮肤下 InputBox 首次点击被主搜索框抢焦点的问题
( function () {
( function () {
     // 保存原始的模块加载方法
     // 仅在 Minerva 皮肤下执行
     var originalLoad = mw.loader.load;
     if ( mw.config.get( 'skin' ) !== 'minerva' ) {
        return;
    }


     // 重写加载方法,加入针对性的拦截逻辑
    var customInputId = 'homeSearchInput';
     mw.loader.load = function ( moduleName ) {
    var originalFocus = HTMLInputElement.prototype.focus;
         // 当尝试加载 Minerva 搜索模块,并且当前焦点在自定义搜索框上时,直接跳过加载
 
         if ( moduleName === 'skins.minerva.search' &&
     // 重写所有 input 元素的 focus 方法
            document.activeElement &&
     HTMLInputElement.prototype.focus = function () {
            document.activeElement.id === 'homeSearchInput' ) {
         // 当前正在被聚焦的元素(this)如果不是我们的自定义搜索框
            // 返回一个已解决的 Promise,避免调用方出错,但模块实际未被加载
         if ( this.id !== customInputId ) {
            return Promise.resolve();
            var active = document.activeElement;
            // 检查当前实际获得焦点的元素是否是我们的自定义搜索框
            if ( active && active.id === customInputId ) {
                // 如果是,说明有脚本(Minerva 组件)正试图从我们的输入框抢走焦点
                // 直接终止这次 focus 操作,不执行原始逻辑
                return;
            }
         }
         }
         // 其他所有情况,都正常调用原始的加载方法
         // 其他所有情况(例如正常点击主搜索框、页面初始化聚焦等),放行原始方法
         return originalLoad.apply( this, arguments );
         return originalFocus.apply( this, arguments );
     };
     };
} )();
} )();