MediaWiki:Common.js: Difference between revisions
Chinaenglish (talk | contribs) Replaced content with "→Any JavaScript here will be loaded for all users on every page load.: " Tags: Replaced Manual revert Reverted |
Chinaenglish (talk | contribs) No edit summary |
||
| (6 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
/* Any JavaScript here will be loaded for all users on every page load. */ | /* Any JavaScript here will be loaded for all users on every page load. */ | ||
//viesz加入下面自动 insource 正则搜索的小工具,用 JavaScript 小工具自动转换查询 | |||
// 首页右上角的搜索输入框ID通常是searchInput,所以这个搜索框id定为homeSearchInput | |||
( function ( $, mw ) { | |||
'use strict'; | |||
$( function () { | |||
// InputBox 把 id 放在了 form 上,所以先找 form,再找里面的 input | |||
var $form = $( '#homeSearchInput' ); | |||
var $customSearch = $form.find( 'input[name="search"]' ); | |||
if ( !$customSearch.length ) return; | |||
$form.on( 'submit', function () { | |||
var term = $customSearch.val().trim(); | |||
if ( term === '' || term.indexOf( 'insource:' ) === 0 ) return; | |||
var escaped = term.replace( /([.*+?^${}()|[\]\\])/g, '\\$1' ); | |||
$customSearch.val( 'insource:/' + escaped + '/i' ); | |||
} ); | |||
} ); | |||
}( jQuery, mediaWiki ) ); | |||
//viesz991开始端为了解决移动端点搜索时跳到了顶部搜索那里去了,焦点失焦了。 | |||
( function () { | |||
function fixCustomSearch() { | |||
var form = document.getElementById( 'homeSearchInput' ); | |||
if ( !form ) { | |||
return; | |||
} | |||
// 关键:真正的 input 在 form 内部,而不是 form 本身 | |||
var input = form.querySelector( 'input[name="search"]' ); | |||
if ( !input ) { | |||
return; | |||
} | |||
// 1. 移除触发 Minerva 搜索组件劫持的类 | |||
input.classList.remove( 'mw-searchInput' ); | |||
// 2. 双重保险:如果焦点仍被抢走,延迟强制夺回 | |||
input.addEventListener( 'focus', function () { | |||
var self = this; | |||
setTimeout( function () { | |||
if ( document.activeElement !== self ) { | |||
self.focus(); | |||
} | |||
}, 0 ); | |||
} ); | |||
} | |||
// 尽早执行一次 | |||
fixCustomSearch(); | |||
// DOM 就绪后再执行一次 | |||
if ( document.readyState === 'loading' ) { | |||
document.addEventListener( 'DOMContentLoaded', fixCustomSearch ); | |||
} | |||
// 在 mediawiki.page.ready 加载完成后再兜底执行一次 | |||
if ( window.mw && mw.loader ) { | |||
mw.loader.using( 'mediawiki.page.ready' ).then( fixCustomSearch ); | |||
} | |||
} )(); | |||
//viesz991结束端为了解决移动端点搜索时跳到了顶部搜索那里去了,焦点失焦了。 | |||