MediaWiki:Common.js:修订间差异
MediaWiki界面页面
更多操作
无编辑摘要 |
无编辑摘要 |
||
| 第58行: | 第58行: | ||
// ---------- 辅助函数 ---------- | // ---------- 辅助函数 ---------- | ||
function getUserName(link) { | function getUserName(link) { | ||
var $span = $(link).find('span').first(); | var $span = $(link).find('span').first(); | ||
| 第65行: | 第64行: | ||
} | } | ||
// | // 判断是否为真正的用户链接(用于添加颜色) | ||
function isUserLink(link) { | function isUserLink(link) { | ||
return $(link).is('a.mw-userlink') || $(link).find('span').length > 0; | return $(link).is('a.mw-userlink') || $(link).find('span').length > 0; | ||
| 第74行: | 第73行: | ||
if ($links.length === 0) return; | if ($links.length === 0) return; | ||
var $fresh = $links.filter(function () { | var $fresh = $links.filter(function () { | ||
return !$(this).data('user-status-processed'); | return !$(this).data('user-status-processed'); | ||
| 第80行: | 第78行: | ||
if ($fresh.length === 0) return; | if ($fresh.length === 0) return; | ||
var users = []; | var users = []; | ||
$fresh.each(function () { | $fresh.each(function () { | ||
| 第88行: | 第85行: | ||
if (users.length === 0) return; | if (users.length === 0) return; | ||
$fresh.each(function () { | $fresh.each(function () { | ||
$(this).data('user-status-processed', true); | $(this).data('user-status-processed', true); | ||
}); | }); | ||
// 1. | // 1. 应用颜色(所有用户链接,包括卡片内) | ||
var batchSize = 50; | var batchSize = 50; | ||
var batches = []; | var batches = []; | ||
| 第138行: | 第134行: | ||
}); | }); | ||
// 2. | // 2. 只为【非卡片内】的用户链接添加状态圆点 | ||
$fresh.each(function () { | $fresh.each(function () { | ||
if (isUserLink(this)) { | if (isUserLink(this)) { | ||
addStatusDot($(this), getUserName(this)); | // 排除卡片容器内的链接 | ||
if ($(this).closest('.citizen-menu_card-content, .citizen-userMenu').length === 0) { | |||
addStatusDot($(this), getUserName(this)); | |||
} | |||
} | } | ||
}); | }); | ||
| 第148行: | 第147行: | ||
// 给单个链接添加状态圆点 | // 给单个链接添加状态圆点 | ||
function addStatusDot($link, username) { | function addStatusDot($link, username) { | ||
if ($link.data('status-dot-added')) return; | if ($link.data('status-dot-added')) return; | ||
$link.data('status-dot-added', true); | $link.data('status-dot-added', true); | ||
var $dot = $('<span class="user-status-dot status-offline" title="离线">'); | var $dot = $('<span class="user-status-dot status-offline" title="离线">'); | ||
$link.after($dot); | $link.after($dot); | ||
var cacheKey = 'mw_user_status_' + mw.config.get('wgDBname') + '_' + username; | var cacheKey = 'mw_user_status_' + mw.config.get('wgDBname') + '_' + username; | ||
var cached = localStorage.getItem(cacheKey); | var cached = localStorage.getItem(cacheKey); | ||
| 第170行: | 第166行: | ||
} | } | ||
var api = new mw.Api(); | var api = new mw.Api(); | ||
api.get({ | api.get({ | ||
| 第188行: | 第183行: | ||
})); | })); | ||
updateDotStyle($dot, lastEditTime); | updateDotStyle($dot, lastEditTime); | ||
}).fail(function () { | }).fail(function () {}); | ||
} | } | ||
| 第213行: | 第206行: | ||
} | } | ||
var linkSelector = 'a.mw-userlink, .citizen-menu_card-content a, .citizen-userMenu a'; | var linkSelector = 'a.mw-userlink, .citizen-menu_card-content a, .citizen-userMenu a'; | ||
colorizeAndStatus($(linkSelector)); | colorizeAndStatus($(linkSelector)); | ||
var observer = new MutationObserver(function () { | var observer = new MutationObserver(function () { | ||
colorizeAndStatus($(linkSelector)); | colorizeAndStatus($(linkSelector)); | ||
| 第228行: | 第218行: | ||
}); | }); | ||
setInterval(function () { | setInterval(function () { | ||
colorizeAndStatus($(linkSelector)); | colorizeAndStatus($(linkSelector)); | ||
}, 3000); | }, 3000); | ||
}); | }); | ||
2026年5月31日 (日) 07:52的版本
/* 这里的任何JavaScript将为所有用户在每次页面加载时加载。 */
// 自动加载 MediaWiki:Footer 页面内容,并插入到每个页面底部
$(document).ready(function() {
const namespace = mw.config.get('wgNamespaceNumber');
const action = mw.config.get('wgAction');
if (namespace !== 0 || action !== 'view') {
return;
}
fetch("/api.php?action=parse&page=MediaWiki:Footer&format=json")
.then(res => res.json())
.then(data => {
if (data.parse && data.parse.text) {
const html = data.parse.text['*'];
$('#mw-content-text').append('<div class="global-footer">' + html + '</div>');
}
});
});
$(function () {
// 动态注入彩虹样式
var rainbowStyle = document.createElement('style');
rainbowStyle.textContent =
'.rainbow-user {' +
'font-weight: bold;' +
'background: repeating-linear-gradient(90deg, red 0px, orange 10px, yellow 20px, green 30px, blue 40px, indigo 50px, violet 60px);' +
'-webkit-background-clip: text;' +
'-webkit-text-fill-color: transparent;' +
'background-clip: text;' +
'color: #FFD700;' +
'}' +
'.rainbow-user::after {' +
'content: "⭐⭐⭐⭐⭐";' +
'margin-left: 2px;' +
'font-weight: normal;' +
'-webkit-text-fill-color: initial;' +
'color: #FFD700;' +
'}';
document.head.appendChild(rainbowStyle);
// 注入状态圆点样式
var statusStyle = document.createElement('style');
statusStyle.textContent =
'.user-status-dot {' +
'display: inline-block;' +
'width: 8px;' +
'height: 8px;' +
'border-radius: 50%;' +
'margin-left: 4px;' +
'vertical-align: middle;' +
'}' +
'.status-online { background-color: #4CAF50; }' +
'.status-away { background-color: #FF9800; }' +
'.status-offline { background-color: #9E9E9E; }';
document.head.appendChild(statusStyle);
// ---------- 辅助函数 ----------
function getUserName(link) {
var $span = $(link).find('span').first();
if ($span.length) return $span.text().trim();
return $(link).text().trim();
}
// 判断是否为真正的用户链接(用于添加颜色)
function isUserLink(link) {
return $(link).is('a.mw-userlink') || $(link).find('span').length > 0;
}
// 核心:给链接上色 + 状态标签
function colorizeAndStatus($links) {
if ($links.length === 0) return;
var $fresh = $links.filter(function () {
return !$(this).data('user-status-processed');
});
if ($fresh.length === 0) return;
var users = [];
$fresh.each(function () {
var name = getUserName(this);
if (name && users.indexOf(name) === -1) users.push(name);
});
if (users.length === 0) return;
$fresh.each(function () {
$(this).data('user-status-processed', true);
});
// 1. 应用颜色(所有用户链接,包括卡片内)
var batchSize = 50;
var batches = [];
for (var i = 0; i < users.length; i += batchSize) {
batches.push(users.slice(i, i + batchSize));
}
var processColorBatch = function (batch) {
var api = new mw.Api();
return api.get({
action: 'query',
list: 'users',
ususers: batch.join('|'),
usprop: 'editcount'
}).then(function (data) {
var classMap = {};
if (data.query && data.query.users) {
data.query.users.forEach(function (u) {
var ec = u.editcount || 0;
if (ec >= 5000) classMap[u.name] = 'rainbow-user';
else if (ec >= 2000) classMap[u.name] = 'gold-user';
else if (ec >= 1000) classMap[u.name] = 'platinum-user';
else if (ec >= 500) classMap[u.name] = 'silver-user';
else if (ec >= 1) classMap[u.name] = 'bronze-user';
});
}
$fresh.each(function () {
var $this = $(this);
var name = getUserName(this);
var cls = classMap[name];
if (cls) {
$this.removeClass('bronze-user silver-user platinum-user gold-user rainbow-user');
$this.addClass(cls);
}
});
});
};
var colorPromise = $.Deferred().resolve();
batches.forEach(function (batch) {
colorPromise = colorPromise.then(function () {
return processColorBatch(batch);
});
});
// 2. 只为【非卡片内】的用户链接添加状态圆点
$fresh.each(function () {
if (isUserLink(this)) {
// 排除卡片容器内的链接
if ($(this).closest('.citizen-menu_card-content, .citizen-userMenu').length === 0) {
addStatusDot($(this), getUserName(this));
}
}
});
}
// 给单个链接添加状态圆点
function addStatusDot($link, username) {
if ($link.data('status-dot-added')) return;
$link.data('status-dot-added', true);
var $dot = $('<span class="user-status-dot status-offline" title="离线">');
$link.after($dot);
var cacheKey = 'mw_user_status_' + mw.config.get('wgDBname') + '_' + username;
var cached = localStorage.getItem(cacheKey);
var now = Date.now();
if (cached) {
try {
var data = JSON.parse(cached);
if (now - data.timestamp < 5 * 60 * 1000) {
updateDotStyle($dot, data.lastEditTime);
return;
}
} catch (e) {}
}
var api = new mw.Api();
api.get({
action: 'query',
list: 'usercontribs',
ucuser: username,
uclimit: 1,
ucprop: 'timestamp'
}).then(function (data) {
var lastEditTime = null;
if (data.query && data.query.usercontribs && data.query.usercontribs.length > 0) {
lastEditTime = data.query.usercontribs[0].timestamp;
}
localStorage.setItem(cacheKey, JSON.stringify({
lastEditTime: lastEditTime,
timestamp: now
}));
updateDotStyle($dot, lastEditTime);
}).fail(function () {});
}
function updateDotStyle($dot, lastEditTime) {
if (!lastEditTime) {
$dot.attr('title', '离线');
$dot.removeClass('status-online status-away').addClass('status-offline');
return;
}
var last = new Date(lastEditTime).getTime();
var diffMinutes = (Date.now() - last) / 60000;
if (diffMinutes < 15) {
$dot.attr('title', '在线(15分钟内活跃)');
$dot.removeClass('status-offline status-away').addClass('status-online');
} else if (diffMinutes < 60) {
$dot.attr('title', '近期活跃(1小时内)');
$dot.removeClass('status-offline status-online').addClass('status-away');
} else {
$dot.attr('title', '离线');
$dot.removeClass('status-online status-away').addClass('status-offline');
}
}
var linkSelector = 'a.mw-userlink, .citizen-menu_card-content a, .citizen-userMenu a';
colorizeAndStatus($(linkSelector));
var observer = new MutationObserver(function () {
colorizeAndStatus($(linkSelector));
});
observer.observe(document.body, {
childList: true,
subtree: true
});
setInterval(function () {
colorizeAndStatus($(linkSelector));
}, 3000);
});