User:SaoMikoto/js/Usergroup.js

From Test Wiki
Revision as of 11:32, 5 October 2025 by SaoMikoto (talk | contribs) (// Edit via InPageEdit)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
// <nowiki>
(function () {
    'use strict';

    const USER_GROUPS = {
        sysadmin: { label: '系', color: '#FF4500', name: '系统管理员' },
        steward: { label: '裁', color: '#9400D3', name: '监管员' },
        bureaucrat: { label: '行', color: '#008080', name: '行政员' },
        checkuser: { label: '查', color: '#4B0082', name: '用户查核员' },
        suppress: { label: '监', color: '#000000', name: '监督员' },
        sysop: { label: '管', color: '#2E8B57', name: '管理员' },
        'interface-admin': { label: '界', color: '#FF8C00', name: '界面管理员' },
        'interwiki-admin': { label: '域', color: '#4682B4', name: '跨wiki管理员' },
        electionadmin: { label: '选', color: '#9932CC', name: '选举管理员' },
        'abusefilter-admin': { label: '滤', color: '#DC143C', name: '滥用过滤器管理员' },
        translateadmin: { label: '译', color: '#008B8B', name: '翻译管理员' },
        chatmod: { label: '聊', color: '#20B2AA', name: '聊天版主' },
        patroller: { label: '巡', color: '#3CB371', name: '巡查员' },
        autopatrol: { label: '免', color: '#6B8E23', name: '巡查豁免者' },
        bot: { label: '机', color: '#696969', name: '机器人' },
        confirmed: { label: '确', color: '#4682B4', name: '确认用户' }
    };
    const GROUP_ORDER = Object.keys(USER_GROUPS);
    const CACHE = new Map();
    const CACHE_TTL = 5 * 60 * 1000;

    function extractUsername(href) {
        if (!href) return null;
        try {
            const decoded = decodeURIComponent(href);
            const match = decoded.match(/[?/]title=User:([^&#/?]+)|[?/]User:([^&#/?]+)/i);
            const user = match ? (match[1] || match[2]) : null;
            return user ? user.replace(/_/g, ' ') : null;
        } catch {
            return null;
        }
    }

    function getUserGroups(usernames, callback) {
        if (!usernames.length) return callback({});
        const api = new mw.Api();
        const results = {};
        const toFetch = [];

        usernames.forEach(name => {
            const cached = CACHE.get(name);
            if (cached && Date.now() - cached.time < CACHE_TTL) {
                results[name] = cached.groups;
            } else {
                toFetch.push(name);
            }
        });

        if (!toFetch.length) return callback(results);

        api.get({
            action: 'query',
            list: 'users',
            ususers: toFetch.join('|'),
            usprop: 'groups',
            formatversion: 2
        }).done(res => {
            (res.query?.users || []).forEach(u => {
                const groups = (u.groups || []).filter(g => USER_GROUPS[g]);
                results[u.name] = groups;
                CACHE.set(u.name, { groups, time: Date.now() });
            });
            callback(results);
        }).fail(() => callback(results));
    }

    function makeBadge(groups) {
        if (!groups?.length) return null;
        const container = document.createElement('sup');
        container.style.cssText = 'font-size:85%;vertical-align:super;margin-left:2px;line-height:1;';
        groups.sort((a, b) => GROUP_ORDER.indexOf(a) - GROUP_ORDER.indexOf(b))
            .forEach((g, i) => {
                const cfg = USER_GROUPS[g];
                if (!cfg) return;
                const span = document.createElement('span');
                span.textContent = cfg.label;
                span.title = cfg.name;
                span.style.cssText = `color:${cfg.color};cursor:help;${i ? 'margin-left:1px;' : ''}`;
                container.appendChild(span);
            });
        return container;
    }

    function processUserLinks() {
        const links = document.querySelectorAll('.mw-userlink, .plainlinks .userlink');
        if (!links.length) return;

        const map = new Map();
        links.forEach(link => {
            if (link.dataset.groupProcessed) return;
            const name = link.classList.contains('mw-userlink')
                ? extractUsername(link.href)
                : link.textContent.trim();
            if (!name) return;
            link.dataset.groupProcessed = 'true';
            if (!map.has(name)) map.set(name, []);
            map.get(name).push(link);
        });

        const names = [...map.keys()];
        if (!names.length) return;

        getUserGroups(names, data => {
            for (const [name, groups] of Object.entries(data)) {
                if (!groups?.length) continue;
                const badge = makeBadge(groups);
                if (!badge) continue;
                for (const link of map.get(name) || []) {
                    if (link.nextElementSibling?.tagName === 'SUP') continue;
                    link.parentNode.insertBefore(badge.cloneNode(true), link.nextSibling);
                }
            }
        });
    }

    function init() {
        if (document.readyState === 'loading') {
            document.addEventListener('DOMContentLoaded', processUserLinks);
        } else processUserLinks();

        new MutationObserver(muts => {
            for (const m of muts) {
                if ([...m.addedNodes].some(n =>
                    n.nodeType === 1 && n.querySelector?.('.mw-userlink, .plainlinks .userlink')
                )) {
                    setTimeout(processUserLinks, 100);
                    break;
                }
            }
        }).observe(document.body, { childList: true, subtree: true });
    }

    if (typeof mw !== 'undefined') {
        (mw.Api ? Promise.resolve() : mw.loader.using('mediawiki.api')).then(init);
    }
})();
// </nowiki>