MediaWiki:Gadget-AutoInsource.js: Difference between revisions
Chinaenglish (talk | contribs) No edit summary |
Chinaenglish (talk | contribs) No edit summary |
||
| (2 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
//viesz加入下面自动 insource 正则搜索的小工具,用 JavaScript 小工具自动转换查询 | //viesz加入下面自动 insource 正则搜索的小工具,用 JavaScript 小工具自动转换查询 | ||
// 首页右上角的搜索输入框ID通常是searchInput,所以这个搜索框id定为homeSearchInput | |||
( function ( $, mw ) { | ( function ( $, mw ) { | ||
'use strict'; | 'use strict'; | ||
$( function () { | $( function () { | ||
// | // InputBox 把 id 放在了 form 上,所以先找 form,再找里面的 input | ||
var $ | var $form = $( '#homeSearchInput' ); | ||
var $customSearch = $form.find( 'input[name="search"]' ); | |||
if ( !$customSearch.length ) return; | if ( !$customSearch.length ) return; | ||
$form.on( 'submit', function () { | |||
$ | |||
var term = $customSearch.val().trim(); | var term = $customSearch.val().trim(); | ||
if ( term === '' || term.indexOf( 'insource:' ) === 0 ) return; | if ( term === '' || term.indexOf( 'insource:' ) === 0 ) return; | ||
var escaped = term.replace( /([.*+?^${}()|[\]\\])/g, '\\$1' ); | var escaped = term.replace( /([.*+?^${}()|[\]\\])/g, '\\$1' ); | ||
$customSearch.val( 'insource:/' + escaped + '/i' ); | $customSearch.val( 'insource:/' + escaped + '/i' ); | ||
} ); | } ); | ||
} ); | } ); | ||
}( jQuery, mediaWiki ) ); | }( jQuery, mediaWiki ) ); | ||
// v2 | |||