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

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

MediaWiki:Common.js

MediaWiki界面页面
天明留言 | 贡献2026年6月27日 (六) 15:01的版本

注意:在发布之后,您可能需要清除浏览器缓存才能看到所作出的更改的影响。

  • Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5Ctrl-R(Mac为⌘-R
  • Google Chrome:Ctrl-Shift-R(Mac为⌘-Shift-R
  • Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5
/* 这里的任何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");
    }
});

// 隐藏模板调用产生的空行
// document.querySelectorAll('.mw-parser-output p').forEach(p => {
// 	if (p.innerHTML.trim().toLowerCase() === '<br>') {
// 		p.style.display = 'none';
// 	}
// });

/* 标记含信息框的条目,用于后置源码信息框布局 */
$( function () {
	var root = document.querySelector( '.mw-parser-output' );

	if ( !root || !root.querySelector( '.acw-infobox' ) ) {
		return;
	}

	root.classList.add( 'acw-has-infobox-layout' );

	Array.prototype.forEach.call( root.children, function ( el ) {
		if ( el.classList.contains( 'acw-infobox' ) ) {
			el.classList.add( 'acw-infobox-layout-side' );
		} else if ( el.classList.contains( 'external-encyclopedia-nav' ) ) {
			el.classList.add( 'acw-infobox-layout-wide' );
		} else if ( el.tagName.toLowerCase() !== 'style' ) {
			el.classList.add( 'acw-infobox-layout-main' );
		}
	} );
} );