MediaWiki:Gadget-AutoInsource.js: Difference between revisions
Chinaenglish (talk | contribs) Created page with "//viesz加入下面自动 insource 正则搜索的小工具,用 JavaScript 小工具自动转换查询 ( function ( $, mw ) { 'use strict'; $( function () { // 仅当页面存在自定义搜索框时才执行 var $customSearch = $( '#searchInput' ); // InputBox 生成的输入框ID通常是 searchInput if ( !$customSearch.length ) return; // 监听该表单的提交事件 $customSearch.closest( 'form' ).on( 'submit', fun..." |
Chinaenglish (talk | contribs) No edit summary |
||
| (3 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 $customSearch = $( ' | 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 | |||