User:Polygnotus/Scripts/XC.js

This is an old revision of this page, as edited by Polygnotus (talk | contribs) at 20:38, 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() {
        // Find links to user pages that aren't inside the "talk" part of signatures
        const links = $('a[href^="/wiki/User:"]:not([href*="talk"]):not([data-ec-checked])');
        console.log('Found user links:', links.length);
        links.each((_, link) => console.log('User link:', $(link).text(), $(link).attr('href')));
        return links;
    }

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

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

            console.log('API response:', response);

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

    // Add status indicator next to username
    function addStatusIndicator(link, isExtendedConfirmed) {
        // Remove any existing indicators next to this link
        $(link).siblings('.ec-status-indicator').remove();
        
        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>