User:Polygnotus/Scripts/XC.js

This is an old revision of this page, as edited by Polygnotus (talk | contribs) at 20:36, 12 February 2025. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// <nowiki>
// ExtendedConfirmedChecker.js
// Adds indicators next to usernames on talk pages showing extended confirmed status
// License: MIT

$(function() {
    'use strict';
    
    // Only run on talk pages
    const namespace = mw.config.get('wgNamespaceNumber');
    console.log('Current namespace:', namespace);
    if (namespace % 2 !== 1) {
        console.log('Not a talk page, exiting');
        return;
    }
    console.log('Running on talk page');

    const processedUsers = new Set();
    const userGroups = new Map();
    
    // Find all user links in signatures
    function findUserLinks() {
        const links = $('a.mw-userlink:not([data-ec-checked]), a.mw-signature-link:not([data-ec-checked])');
        console.log('Found user links:', links.length);
        links.each((_, link) => console.log('User link:', $(link).text()));
        return links;
    }

    // Extract username from link
    function getUsernameFromLink(link) {
        const username = $(link).text();
        return username.replace(/^User:/, '');
    }

    // Batch process users to reduce API calls
    async function processUserBatch(users) {
        if (users.length === 0) return;
        
        const userList = users.join('|');
        
        try {
            const response = await $.ajax({
                url: mw.util.wikiScript('api'),
                data: {
                    action: 'query',
                    format: 'json',
                    list: 'users',
                    usprop: 'groups',
                    ususers: userList
                }
            });

            if (response.query && response.query.users) {
                response.query.users.forEach(user => {
                    const groups = user.groups || [];
                    userGroups.set(user.name, groups.includes('extendedconfirmed'));
                });
            }
        } catch (error) {
            console.error('Error fetching user groups:', error);
        }
    }

    // Add status indicator next to username
    function addStatusIndicator(link, isExtendedConfirmed) {
        const indicator = $('<span>')
            .addClass('ec-status-indicator')
            .css({
                'margin-left': '4px',
                'font-size': '0.85em',
                'color': isExtendedConfirmed ? '#14866d' : '#72777d',
                'cursor': 'help'
            })
            .attr('title', isExtendedConfirmed ? 
                'Extended confirmed user' : 
                'Not extended confirmed')
            .text(isExtendedConfirmed ? '★' : '☆');
        
        $(link).after(indicator);
        $(link).attr('data-ec-checked', 'true');
    }

    // Main processing function
    async function processPage() {
        console.log('Processing page...');
        const userLinks = findUserLinks();
        const batchSize = 50;
        const users = [];

        userLinks.each((_, link) => {
            const username = getUsernameFromLink(link);
            if (!processedUsers.has(username)) {
                users.push(username);
                processedUsers.add(username);
            }
        });

        // Process users in batches
        for (let i = 0; i < users.length; i += batchSize) {
            const batch = users.slice(i, i + batchSize);
            await processUserBatch(batch);
        }

        // Add indicators
        userLinks.each((_, link) => {
            const username = getUsernameFromLink(link);
            const isExtendedConfirmed = userGroups.get(username);
            addStatusIndicator(link, isExtendedConfirmed);
        });
    }

    // Run on page load and when new content is added
    processPage();
    mw.hook('wikipage.content').add(processPage);
});
// </nowiki>