User:Username/BlockAbuser.js

From Test Wiki
Revision as of 02:21, 21 January 2026 by Username (talk | contribs)
Jump to navigation Jump to search

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.
mw.loader.using(['mediawiki.util', 'mediawiki.api', 'jquery'], function () {
    if (mw.config.get('wgCanonicalSpecialPageName') !== 'AbuseLog') {
        return;
    }

    const $content = $('#mw-content-text');
    const seenUsers = new Set();
    const userData = {}; // username => { latestLogId, extraHits, $li }

    const ipRegex = /^(?:\d{1,3}\.){3}\d{1,3}$|:/;

    // Helper: decode username from URL
    function getUsernameFromHref(href) {
        const parts = href.split('/wiki/User:');
        if (parts.length < 2) return null;
        return decodeURIComponent(parts[1]).replace(/_/g, ' ').trim();
    }

    // Find all <li> entries inside mw-content-text (the AbuseLog entries)
    $content.find('li').each(function () {
        const $li = $(this);

        // Find user link inside this <li>
        const $userLink = $li.find('a').filter(function () {
            const href = $(this).attr('href') || '';
            return href.includes('/wiki/User:');
        }).first();

        if ($userLink.length === 0) return; // no user link, skip

        const username = getUsernameFromHref($userLink.attr('href'));
        if (!username) return;
        if (ipRegex.test(username)) return; // skip IPs

        // Find AbuseLog id from details/examine links inside this <li>
        let logId = null;
        $li.find('a').each(function () {
            const href = $(this).attr('href') || '';
            const match = href.match(/Special:AbuseLog\/(\d+)/);
            if (match) {
                logId = match[1];
                return false; // break
            }
        });

        if (!logId) return;

        // Track user data, pick most recent log id
        if (!userData[username]) {
            userData[username] = { latestLogId: logId, extraHits: 0, $li: $li, $userLink: $userLink };
        } else {
            // Update latestLogId if newer
            if (parseInt(logId) > parseInt(userData[username].latestLogId)) {
                userData[username].latestLogId = logId;
                userData[username].$li = $li;
                userData[username].$userLink = $userLink;
            }
            userData[username].extraHits++;
        }
    });

    // Add checkboxes inside <li>, before username link (only once per user)
    Object.entries(userData).forEach(([username, data]) => {
        if (data.$userLink.prev('.blockabuser-checkbox').length) {
            return; // already has checkbox
        }
        const $checkbox = $('<input>', {
            type: 'checkbox',
            class: 'blockabuser-checkbox',
            'data-username': username,
            'data-latestlogid': data.latestLogId,
            'data-extrahits': data.extraHits
        }).css({
            marginRight: '6px',
            verticalAlign: 'middle',
            cursor: 'pointer'
        }).attr('title', 'Select this user for block review');

        data.$userLink.before($checkbox);
    });

    // Button at top (same as before)
    const $btn = $('<button>')
        .text('Open selected user AbuseLogs and generate summary')
        .css({
            margin: '1em 0',
            padding: '6px 12px',
            cursor: 'pointer'
        });

    $content.prepend($btn);

    $btn.on('click', function () {
        const checked = $('.blockabuser-checkbox:checked');
        if (!checked.length) {
            alert('Please select at least one user.');
            return;
        }

        let summaryLines = [];
        checked.each(function () {
            const $cb = $(this);
            const username = $cb.data('username');
            const latestLogId = $cb.data('latestlogid');
            const extraHits = parseInt($cb.data('extrahits'), 10);

            // Open AbuseLog filtered by user
            const abuseLogUrl = mw.util.getUrl('Special:AbuseLog', {
                wpSearchUser: username
            });
            window.open(abuseLogUrl, '_blank');

            // Compose summary line
            let line = `[[Special:AbuseLog/${latestLogId}]]`;
            if (extraHits > 0) {
                line += ` (+[[Special:AbuseLog/${username}|${extraHits}]])`;
            }
            summaryLines.push(line);
        });

        const summaryText =
            'Spambot or spam-only accounts detected. Details:\n' +
            summaryLines.join('\n');

        alert(summaryText);
    });
});