MediaWiki:Gadget-UserLinkAvatar.js: Difference between revisions
From HUIJIA FUN
PlurimodSU (talk | contribs) Create |
PlurimodSU (talk | contribs) Edit |
||
| (3 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
/* From https://otomad.wiki/MediaWiki:Gadget-UserLinkAvatar.js */ | /* From https://otomad.wiki/MediaWiki:Gadget-UserLinkAvatar.js */ | ||
$('.mw-userlink').each(function( | (function() { | ||
// 确保在页面加载后执行 | |||
mw.hook('wikipage.content').add(function($content) { | |||
}); | const users = {}; | ||
const $userLinks = $content.find('.mw-userlink'); | |||
// 1. 收集页面上所有的用户名 | |||
$userLinks.each(function() { | |||
const username = $(this).text().trim(); | |||
if (username && !users[username]) { | |||
users[username] = { elements: [], id: null }; | |||
} | |||
if (username) users[username].elements.push($(this)); | |||
}); | |||
const userList = Object.keys(users); | |||
if (userList.length === 0) return; | |||
// 2. 调用 API 批量获取用户 ID | |||
new mw.Api().get({ | |||
action: 'query', | |||
list: 'users', | |||
ususers: userList.join('|'), | |||
format: 'json' | |||
}).done(function(data) { | |||
const fetchedUsers = data.query.users; | |||
fetchedUsers.forEach(function(userData) { | |||
if (userData.userid) { | |||
// 3. 按照你的格式拼接头像路径 | |||
// 格式:/images/avatars/u745476817_AmmVf_{ID}_l.jpg | |||
const avatarUrl = '/images/avatars/u745476817_AmmVf_' + userData.userid + '_l.jpg'; | |||
// 4. 将头像插入到对应的用户名链接前 | |||
users[userData.name].elements.forEach(function($link) { | |||
if ($link.find('.userlink-avatar').length === 0) { | |||
const $img = $('<img>').attr({ | |||
'src': avatarUrl, | |||
'class': 'userlink-avatar', | |||
'onerror': "this.style.display='none'" // 如果没头像则隐藏 | |||
}); | |||
$link.prepend($img); | |||
} | |||
}); | |||
} | |||
}); | |||
}); | |||
}); | |||
})(); | |||
Latest revision as of 11:01, 22 December 2025
/* From https://otomad.wiki/MediaWiki:Gadget-UserLinkAvatar.js */
(function() {
// 确保在页面加载后执行
mw.hook('wikipage.content').add(function($content) {
const users = {};
const $userLinks = $content.find('.mw-userlink');
// 1. 收集页面上所有的用户名
$userLinks.each(function() {
const username = $(this).text().trim();
if (username && !users[username]) {
users[username] = { elements: [], id: null };
}
if (username) users[username].elements.push($(this));
});
const userList = Object.keys(users);
if (userList.length === 0) return;
// 2. 调用 API 批量获取用户 ID
new mw.Api().get({
action: 'query',
list: 'users',
ususers: userList.join('|'),
format: 'json'
}).done(function(data) {
const fetchedUsers = data.query.users;
fetchedUsers.forEach(function(userData) {
if (userData.userid) {
// 3. 按照你的格式拼接头像路径
// 格式:/images/avatars/u745476817_AmmVf_{ID}_l.jpg
const avatarUrl = '/images/avatars/u745476817_AmmVf_' + userData.userid + '_l.jpg';
// 4. 将头像插入到对应的用户名链接前
users[userData.name].elements.forEach(function($link) {
if ($link.find('.userlink-avatar').length === 0) {
const $img = $('<img>').attr({
'src': avatarUrl,
'class': 'userlink-avatar',
'onerror': "this.style.display='none'" // 如果没头像则隐藏
});
$link.prepend($img);
}
});
}
});
});
});
})();