MediaWiki:Common.js
MediaWiki界面页面
更多操作
注意:在发布之后,您可能需要清除浏览器缓存才能看到所作出的更改的影响。
- Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5或Ctrl-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 );
} );
} );
} );
/* 增加外部搜索推荐提示 */
mw.loader.using( [ 'mediawiki.util' ], function () {
$( function () {
// 1. 只在 Special:Search 页面运行
if ( mw.config.get( 'wgCanonicalSpecialPageName' ) !== 'Search' ) {
return;
}
var searchTerm = mw.config.get( 'wgSearchTerms' );
if ( !searchTerm ) {
return;
}
var $info = $( '.mw-search-results-info' );
if ( !$info.length ) {
return;
}
// 2. 防止重复注入
if ( $info.find( '.mw-search-external' ).length ) {
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 ) {
var $a = $( '<a>' )
.attr( {
href: link.url,
target: '_blank',
rel: 'nofollow noopener'
} )
.text( link.name );
if ( index > 0 ) {
$p.append( ' / ' );
}
$p.append( $a );
} );
// 4. 插入到 info 区块末尾(紧跟 create link 后)
$info.append( $p );
} );
} );