MediaWiki:Common.js:修订间差异
MediaWiki界面页面
更多操作
小无编辑摘要 |
小无编辑摘要 |
||
| 第70行: | 第70行: | ||
// }); | // }); | ||
/* | /* 将后置源码的信息框移到正文开头,使右浮动从页面顶部开始 */ | ||
$( function () { | $( function () { | ||
var root = document.querySelector( '.mw-parser-output' ); | var root = document.querySelector( '.mw-parser-output' ); | ||
if ( !root | if ( !root ) { | ||
return; | return; | ||
} | } | ||
root. | var infobox = root.querySelector( '.acw-infobox' ); | ||
Array.prototype. | if ( !infobox ) { | ||
return; | |||
} | |||
var firstContent = Array.prototype.find.call( root.children, function ( el ) { | |||
return el.tagName.toLowerCase() !== 'style' && | |||
el.classList. | !el.classList.contains( 'acw-infobox' ); | ||
} ); | } ); | ||
if ( firstContent && infobox !== firstContent ) { | |||
root.insertBefore( infobox, firstContent ); | |||
} | |||
} ); | } ); | ||
2026年6月27日 (六) 15:04的版本
/* 这里的任何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 ) {
return;
}
var infobox = root.querySelector( '.acw-infobox' );
if ( !infobox ) {
return;
}
var firstContent = Array.prototype.find.call( root.children, function ( el ) {
return el.tagName.toLowerCase() !== 'style' &&
!el.classList.contains( 'acw-infobox' );
} );
if ( firstContent && infobox !== firstContent ) {
root.insertBefore( infobox, firstContent );
}
} );