MediaWiki:Gadget-AutoInsource.js

From ChinaEnglish
Revision as of 11:30, 13 November 2026 by 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...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
//viesz加入下面自动 insource 正则搜索的小工具,用 JavaScript 小工具自动转换查询
( function ( $, mw ) {
    'use strict';
    $( function () {
        // 仅当页面存在自定义搜索框时才执行
        var $customSearch = $( '#searchInput' ); // InputBox 生成的输入框ID通常是 searchInput
        if ( !$customSearch.length ) return;

        // 监听该表单的提交事件
        $customSearch.closest( 'form' ).on( 'submit', function () {
            var term = $customSearch.val().trim();
            if ( term === '' || term.indexOf( 'insource:' ) === 0 ) return;

            // 转义正则中的特殊字符(简化处理,实际需更严谨的转义)
            var escaped = term.replace( /([.*+?^${}()|[\]\\])/g, '\\$1' );
            // 自动包装为 insource 正则,并忽略大小写
            $customSearch.val( 'insource:/' + escaped + '/i' );
        } );
    } );
}( jQuery, mediaWiki ) );