MediaWiki:Common.js:修订间差异
跳转到导航
跳转到搜索
| 第132行: | 第132行: | ||
container.appendChild(button); | container.appendChild(button); | ||
} | } | ||
})(); | |||
//迁站测试 | |||
// Wikidot 迁站提示 | |||
// 在匹配页面顶部添加原 Wikidot 链接 | |||
(function () { | |||
'use strict'; | |||
// 只在页面查看模式下运行 | |||
if (mw.config.get('wgAction') !== 'view') return; | |||
const pageTitle = mw.config.get('wgTitle'); | |||
fetch('https://code.hw233.cn/tool/Mediawiki/wikidot.json') | |||
.then(response => { | |||
if (!response.ok) throw new Error('JSON加载失败'); | |||
return response.json(); | |||
}) | |||
.then(data => { | |||
const match = data.find(item => item.title === pageTitle); | |||
if (match && match.url) { | |||
const html = ` | |||
<div class="styled-quote"> | |||
<p>迁站前该页面原Wikidot页面: | |||
<a href="https://lostmedia.wikidot.com/${match.url}" target="_blank" rel="noopener"> | |||
${match.title} | |||
</a> | |||
</p> | |||
</div>`; | |||
const content = document.getElementById('mw-content-text'); | |||
if (content) { | |||
content.insertAdjacentHTML('afterbegin', html); | |||
} | |||
} | |||
}) | |||
.catch(error => { | |||
console.warn('Wikidot迁站提示加载失败:', error); | |||
}); | |||
})(); | })(); | ||
2026年5月10日 (日) 14:31的版本
//用户名前显示头像
mw.loader.using( 'mediawiki.util', function () {
$( document ).ready( function () {
function addAvatarsToElements( $elements ) {
$elements.each( function () {
var $link = $( this );
if ( $link.prev( '.sp-avatar-wrapper' ).length || $link.data('avatar-added') ) {
return;
}
$link.data('avatar-added', true);
const excludeContainers = [
'#p-views',
'#p-cactions',
'#p-tb',
'#right-navigation',
'#p-personal',
'.mw-portlet-personal'
];
// 检查是否匹配任意一个排除容器
const isExcluded = excludeContainers.some(selector => $link.closest(selector).length);
if (isExcluded) {
return;
}
var href = $link.attr( 'href' );
if ( !href ) return;
var userPart = decodeURIComponent( href.split( '/wiki/' )[1] );
if ( !userPart ) return;
if ( userPart.indexOf( '/' ) !== -1 ) {
return;
}
var userName = userPart.replace( /^User:|^用户:/, '' ).replace(/_/g, ' ');
var linkText = $link.text().trim().replace(/^User:|^用户:/, '');
if ( linkText !== userName ) {
var $target = $link.find( 'span, bdi' ).first();
var targetText = $target.length ? $target.text().trim().replace(/^User:|^用户:/, '') : '';
if ( !$target.length || targetText !== userName ) {
return;
}
}
new mw.Api().get( {
action: 'parse',
text: '{{#avatar:' + userName + '|l}}',
contentmodel: 'wikitext',
disablelimitreport: true,
disableeditsection: true
} ).done( function ( data ) {
if ( data.parse && data.parse.text ) {
var avatarHtml = data.parse.text[ '*' ];
var $avatarWrapper = $( '<span class="sp-avatar-wrapper" style="vertical-align: middle; margin-right: 4px; display: inline-block;"></span>' ).html( avatarHtml );
$avatarWrapper.find( 'img' ).css( {
width: '1.5em',
height: '1.5em',
objectFit: 'cover',
display: 'inline-block',
verticalAlign: 'middle',
border: 'var(--background-color-neutral, #eaecf0) 2px solid',
boxSizing: 'border-box'
} );
$link.before( $avatarWrapper );
if ( mw.user.isAnon() ) {
return;
}
var currentUserName = mw.user.getName();
if ( userName !== currentUserName ) {
return;
}
var avatarImg = $avatarWrapper.find( 'img' );
var avatarUrl = avatarImg.attr( 'src' );
if ( avatarUrl && avatarUrl !== '/images/avatars/default_l.gif' ) {
var $styleTag = $( '#dynamic-avatar-style' );
if ( !$styleTag.length ) {
$styleTag = $( '<style id="dynamic-avatar-style"></style>' ).appendTo( 'head' );
}
var css = '.mw-header .citizen-dropdown-summary[title="打开/关闭个人菜单"] .citizen-ui-icon.mw-ui-icon-wikimedia-userAvatar { background-image: url("' + avatarUrl + '"); background-repeat: no-repeat; background-size: contain; width: 30px; height: 30px; } .mw-header .citizen-dropdown-summary[title="打开/关闭个人菜单"] .citizen-ui-icon.mw-ui-icon-wikimedia-userAvatar::before{mask-image: unset;background-color: transparent;}';
$styleTag.text( css );
}
}
} );
} );
}
addAvatarsToElements( $( 'a[href*="/wiki/User:"], a[href*="/wiki/%E7%94%A8%E6%88%B7:"], a[href*="/wiki/用户:"]' ) );
var observer = new MutationObserver( function ( mutations ) {
mutations.forEach( function ( mutation ) {
$( mutation.addedNodes ).find( 'a[href*="/wiki/User:"], a[href*="/wiki/%E7%94%A8%E6%88%B7:"], a[href*="/wiki/用户:"]' ).each( function() {
addAvatarsToElements( $( this ) );
} );
} );
} );
observer.observe( document.body, { childList: true, subtree: true } );
} );
} );
//DPL4翻页跳转
(function() {
var container = document.getElementById('dpl-jump-area');
if (container) {
var count = parseInt(container.getAttribute('data-count'));
var total = parseInt(container.getAttribute('data-total'));
var offset = parseInt(container.getAttribute('data-offset'));
var pageName = container.getAttribute('data-pagename');
var maxPage = Math.ceil(total / count);
var currentPage = Math.floor(offset / count) + 1;
var input = document.createElement('input');
input.type = 'number';
input.value = currentPage;
input.min = 1;
input.max = maxPage;
input.style.width = '45px';
input.style.margin = '0 5px';
var button = document.createElement('button');
button.innerText = '确认';
button.onclick = function() {
var pageNum = parseInt(input.value);
if (pageNum >= 1 && pageNum <= maxPage) {
var targetOffset = (pageNum - 1) * count;
window.location.href = mw.util.getUrl(pageName) + '?DPL_offset=' + targetOffset;
} else {
alert('请输入 1 到 ' + maxPage + ' 之间的页码');
}
};
container.innerHTML = ' 跳至第 ';
container.appendChild(input);
container.appendChild(document.createTextNode(' 页 '));
container.appendChild(button);
}
})();
//迁站测试
// Wikidot 迁站提示
// 在匹配页面顶部添加原 Wikidot 链接
(function () {
'use strict';
// 只在页面查看模式下运行
if (mw.config.get('wgAction') !== 'view') return;
const pageTitle = mw.config.get('wgTitle');
fetch('https://code.hw233.cn/tool/Mediawiki/wikidot.json')
.then(response => {
if (!response.ok) throw new Error('JSON加载失败');
return response.json();
})
.then(data => {
const match = data.find(item => item.title === pageTitle);
if (match && match.url) {
const html = `
<div class="styled-quote">
<p>迁站前该页面原Wikidot页面:
<a href="https://lostmedia.wikidot.com/${match.url}" target="_blank" rel="noopener">
${match.title}
</a>
</p>
</div>`;
const content = document.getElementById('mw-content-text');
if (content) {
content.insertAdjacentHTML('afterbegin', html);
}
}
})
.catch(error => {
console.warn('Wikidot迁站提示加载失败:', error);
});
})();