MediaWiki:Common.js:修订间差异
MediaWiki界面页面
更多操作
避免红链新建页保存后自动跳转触发离开提示 |
修复可视化编辑器保存确认框高度未及时重算 |
||
| 第140行: | 第140行: | ||
} ); | } ); | ||
}() ); | }() ); | ||
/* 让可视化编辑器保存确认框在打开后重新计算高度 */ | |||
( function () { | |||
function resizeSaveDialog() { | |||
var target = window.ve && ve.init && ve.init.target; | |||
var dialog = target && target.saveDialog; | |||
if ( dialog && typeof dialog.updateSize === function ) { | |||
dialog.updateSize(); | |||
} | |||
window.dispatchEvent( new Event( resize ) ); | |||
} | |||
function scheduleResize() { | |||
[ 50, 200, 600, 1200 ].forEach( function ( delay ) { | |||
setTimeout( resizeSaveDialog, delay ); | |||
} ); | |||
} | |||
mw.hook( ve.saveDialog.stateChanged ).add( scheduleResize ); | |||
mw.hook( ve.newTarget ).add( function ( target ) { | |||
if ( target && typeof target.on === function ) { | |||
target.on( saveWorkflowBegin, scheduleResize ); | |||
target.on( saveWorkflowChangePanel, scheduleResize ); | |||
} | |||
} ); | |||
}() ); | |||
EOF | |||
node --check /tmp/acwiki-common-ve-dialog-resize.js | |||
php maintenance/run.php edit --server https://ac-wiki.cn --user "天明" --summary "修复可视化编辑器保存确认框高度未及时重算" "MediaWiki:Common.js" < /tmp/acwiki-common-ve-dialog-resize.js | |||
2026年6月30日 (二) 01:21的版本
/* 这里的任何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 );
}
} );
/* 红链新建页在可视化编辑保存后自动回到阅读页 */
( function () {
if ( mw.config.get( 'wgAction' ) !== 'edit' && mw.config.get( 'wgAction' ) !== 'submit' ) {
return;
}
var currentUrl = new URL( location.href );
if ( currentUrl.searchParams.get( 'redlink' ) !== '1' ) {
return;
}
var pageName = mw.config.get( 'wgRelevantPageName' );
if ( !pageName ) {
return;
}
function clearEditWarning( target ) {
if ( !target ) {
return;
}
target.edited = false;
target.submitting = false;
if ( typeof target.teardownUnloadHandlers === 'function' ) {
target.teardownUnloadHandlers();
}
}
function redirectToCreatedPage( target ) {
clearEditWarning( target );
location.href = mw.util.getUrl( pageName, { venotify: 'saved' } );
}
mw.hook( 've.newTarget' ).add( function ( target ) {
if ( !target || typeof target.on !== 'function' ) {
return;
}
target.on( 'save', function ( data ) {
if ( data && data.newrevid !== undefined ) {
redirectToCreatedPage( target );
}
} );
} );
}() );
/* 让可视化编辑器保存确认框在打开后重新计算高度 */
( function () {
function resizeSaveDialog() {
var target = window.ve && ve.init && ve.init.target;
var dialog = target && target.saveDialog;
if ( dialog && typeof dialog.updateSize === function ) {
dialog.updateSize();
}
window.dispatchEvent( new Event( resize ) );
}
function scheduleResize() {
[ 50, 200, 600, 1200 ].forEach( function ( delay ) {
setTimeout( resizeSaveDialog, delay );
} );
}
mw.hook( ve.saveDialog.stateChanged ).add( scheduleResize );
mw.hook( ve.newTarget ).add( function ( target ) {
if ( target && typeof target.on === function ) {
target.on( saveWorkflowBegin, scheduleResize );
target.on( saveWorkflowChangePanel, scheduleResize );
}
} );
}() );
EOF
node --check /tmp/acwiki-common-ve-dialog-resize.js
php maintenance/run.php edit --server https://ac-wiki.cn --user "天明" --summary "修复可视化编辑器保存确认框高度未及时重算" "MediaWiki:Common.js" < /tmp/acwiki-common-ve-dialog-resize.js