MediaWiki:Minerva.js: Difference between revisions

No edit summary
Tag: Reverted
No edit summary
Tag: Reverted
Line 83: Line 83:


// 修复 Minerva 皮肤下 InputBox 首次点击被主搜索框抢焦点的问题
// 修复 Minerva 皮肤下 InputBox 首次点击被主搜索框抢焦点的问题
// 针对 T402078/T410229 的改进方案:在 pointerdown 时记录目标,在焦点被抢后强制夺回
// 从自定义 InputBox 上移除 mw-searchInput 类,防止被 Minerva 搜索组件劫持
( function () {
( function () {
     var inputBox = document.getElementById( 'homeSearchInput' );
     var inputBox = document.getElementById( 'homeSearchInput' );
     if ( !inputBox ) {
     if ( inputBox ) {
         return;
         // 移除触发 Minerva 焦点劫持的类
        inputBox.classList.remove( 'mw-searchInput' );
        // 确保 id 也不是 'searchInput',虽然目前不是,但作为防御性措施
        if ( inputBox.id === 'searchInput' ) {
            inputBox.id = 'homeSearchInput';
        }
     }
     }
    var targetInput = null;
    // 1. 在 pointerdown(早于 focus 事件)时,记录用户实际点击的目标输入框
    document.addEventListener( 'pointerdown', function ( event ) {
        if ( event.target === inputBox ) {
            targetInput = inputBox;
        } else {
            // 如果点击的是其他元素,清空记录,避免影响其他搜索框
            targetInput = null;
        }
    }, true ); // 使用捕获阶段,确保最先执行
    // 2. 在 focusin 事件中,检查焦点是否被错误地转移到了主搜索框
    document.addEventListener( 'focusin', function ( event ) {
        // 如果用户原本点击的是我们的输入框,但焦点现在却在别处
        if ( targetInput && event.target !== targetInput ) {
            // 强制将焦点夺回我们的输入框
            targetInput.focus();
            // 夺回后重置记录,避免重复触发
            targetInput = null;
        }
    }, true ); // 同样使用捕获阶段
} )();
} )();