打开/关闭菜单
打开/关闭外观设置菜单
打开/关闭个人菜单
未登录
未登录用户的IP地址会在进行任意编辑后公开展示。

网站测试中,如需帮助或提出建议
联系维护员 @天明

MediaWiki:Common.js:修订间差异

MediaWiki界面页面
天明留言 | 贡献
无编辑摘要
天明留言 | 贡献
无编辑摘要
第30行: 第30行:
} );
} );


mw.loader.using( [ 'mediawiki.util' ], function () {
/* 提供外部搜索推荐 */
$(function () {
    if (mw.config.get("wgCanonicalSpecialPageName") !== "Search") {
        return;
    }


     function injectExternalSearch( $content ) {
     // 当前搜索词
    const searchTerm = mw.util.getParamValue("search");
    if (!searchTerm) {
        return;
    }


        // 1. 只处理搜索页
    // 构造外部搜索链接
        if ( mw.config.get( 'wgCanonicalSpecialPageName' ) !== 'Search' ) {
    const googleUrl = "https://www.google.com/search?q=" + encodeURIComponent(searchTerm);
            return;
    const bingUrl  = "https://www.bing.com/search?q="  + encodeURIComponent(searchTerm);
        }
    const baiduUrl  = "https://www.baidu.com/s?wd="      + encodeURIComponent(searchTerm);


        var searchTerm = mw.config.get( 'wgSearchTerms' );
    const $externalLinks = $("<p>").html(
         if ( !searchTerm ) {
         `你也可以使用外部搜索:<a href="${googleUrl}" target="_blank" rel="noopener">Google</a> / ` +
            return;
        `<a href="${bingUrl}" target="_blank" rel="noopener">Bing</a> / ` +
         }
         `<a href="${baiduUrl}" target="_blank" rel="noopener">百度</a> 来查找“${searchTerm}”。`
    ).addClass("mw-search-external");


        var $info = $content.find( '.mw-search-results-info' );
    // 优先插入到有搜索结果的地方
        if ( !$info.length ) {
    if ($(".mw-search-results").length) {
            return;
         $externalLinks.insertAfter(".mw-search-results");
         }
    }
 
    // 否则插入到没有结果提示后
        // 2. 防止重复注入
    else if ($(".mw-search-nonefound").length) {
        if ( $info.find( '.mw-search-external' ).length ) {
         $externalLinks.insertAfter(".mw-search-nonefound");
            return;
        }
 
        // 3. 构造外部搜索链接
        var links = [
            {
                name: 'Google',
                url: 'https://www.google.com/search?q=' + encodeURIComponent( searchTerm )
            },
            {
                name: 'Bing',
                url: 'https://www.bing.com/search?q=' + encodeURIComponent( searchTerm )
            },
            {
                name: '百度',
                url: 'https://www.baidu.com/s?wd=' + encodeURIComponent( searchTerm )
            }
        ];
 
        var $p = $( '<p>' )
            .addClass( 'mw-search-external' )
            .text( '或使用外部搜索:' );
 
         links.forEach( function ( link, index ) {
            if ( index > 0 ) {
                $p.append( ' / ' );
            }
 
            $p.append(
                $( '<a>' )
                    .attr( {
                        href: link.url,
                        target: '_blank',
                        rel: 'nofollow noopener'
                    } )
                    .text( link.name )
            );
        } );
 
        $info.append( $p );
     }
     }
 
});
    // 4. 在内容加载 / 刷新后执行
    mw.hook( 'wikipage.content' ).add( injectExternalSearch );
 
} );

2025年12月21日 (日) 19:24的版本

/* 这里的任何JavaScript将为所有用户在每次页面加载时加载。 */

/* 为表格自动加载默认排序规则 */
jQuery( document ).ready( function ( $ ) {

    // 确保 MediaWiki 自带的表格排序模块已加载
    mw.loader.using( 'jquery.tablesorter', function () {

        // 遍历页面中所有可排序表格
        $( 'table.sortable' ).each( function () {

            var $table = $( this );

            // 如果表格已经指定了默认排序(例如通过 data-sort-initial)
            // 则尊重作者意图,不进行干预
            if ( $table.data( 'sort-initialized' ) ) {
                return;
            }

            // 对表格设置默认排序:
            // 第一列,升序
            $table.tablesorter( {
                sortList: [ [ 0, 0 ] ]
            } );

            // 标记该表格已被初始化,避免重复执行
            $table.data( 'sort-initialized', true );
        } );
    } );
} );

/* 提供外部搜索推荐 */
$(function () {
    if (mw.config.get("wgCanonicalSpecialPageName") !== "Search") {
        return;
    }

    // 当前搜索词
    const searchTerm = mw.util.getParamValue("search");
    if (!searchTerm) {
        return;
    }

    // 构造外部搜索链接
    const googleUrl = "https://www.google.com/search?q=" + encodeURIComponent(searchTerm);
    const bingUrl   = "https://www.bing.com/search?q="   + encodeURIComponent(searchTerm);
    const baiduUrl  = "https://www.baidu.com/s?wd="      + encodeURIComponent(searchTerm);

    const $externalLinks = $("<p>").html(
        `你也可以使用外部搜索:<a href="${googleUrl}" target="_blank" rel="noopener">Google</a> / ` +
        `<a href="${bingUrl}" target="_blank" rel="noopener">Bing</a> / ` +
        `<a href="${baiduUrl}" target="_blank" rel="noopener">百度</a> 来查找“${searchTerm}”。`
    ).addClass("mw-search-external");

    // 优先插入到有搜索结果的地方
    if ($(".mw-search-results").length) {
        $externalLinks.insertAfter(".mw-search-results");
    }
    // 否则插入到没有结果提示后
    else if ($(".mw-search-nonefound").length) {
        $externalLinks.insertAfter(".mw-search-nonefound");
    }
});