User:Username/BlockAbuser.js

From Test Wiki
Revision as of 02:34, 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('wgPageName') !== 'Special:AbuseLog') {
        return;
    }

    const $content = $('#mw-content-text');
    const userData = {}; // username => { latestLogId, extraHits, $li, $userLink, $blockLink, allLinks: [] }
    const ipRegex = /^(?:\d{1,3}\.){3}\d{1,3}$|:/;
    const api = new mw.Api();

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

    // 1. Collect user info, group links by username
    $content.find('li').each(function () {
        const $li = $(this);

        const $userLinks = $li.find('a').filter(function () {
            const href = $(this).attr('href') || '';
            return href.includes('/wiki/User:');
        });

        if ($userLinks.length === 0) return;

        $userLinks.each(function () {
            const $link = $(this);
            const username = getUsernameFromHref($link.attr('href'));
            if (!username || ipRegex.test(username)) return;

            // Find abuse log id
            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;
                }
            });
            if (!logId) return;

            // Find block link for this user in this li (link with href containing 'block=' and username)
            const $blockLink = $li.find('a').filter(function () {
                const href = $(this).attr('href') || '';
                return href.includes('block=') && href.includes(encodeURIComponent(username).replace(/%20/g, '+'));
            }).first();

            if (!userData[username]) {
                userData[username] = {
                    latestLogId: logId,
                    extraHits: 0,
                    $li: $li,
                    $userLink: $link,
                    $blockLink: $blockLink.length ? $blockLink : null,
                    allLinks: [$link]
                };
            } else {
                userData[username].allLinks.push($link);
                if (parseInt(logId) > parseInt(userData[username].latestLogId)) {
                    userData[username].latestLogId = logId;
                    userData[username].$li = $li;
                    userData[username].$userLink = $link;
                    if ($blockLink.length) userData[username].$blockLink = $blockLink;
                }
                userData[username].extraHits++;
            }
        });
    });

    // 2. Delink all but most recent user link per user
    Object.entries(userData).forEach(([username, data]) => {
        data.allLinks.forEach(($link) => {
            if ($link[0] !== data.$userLink[0]) {
                $link.replaceWith(document.createTextNode($link.text()));
            }
        });
    });

    // 3. Check blocked status via API
    const usernames = Object.keys(userData);
    if (usernames.length === 0) {
        addButtonAndSetup(userData);
        return;
    }

    const api = new mw.Api();

    api.get({
        action: 'query',
        list: 'users',
        ususers: usernames.join('|'),
        usprop: 'blockinfo'
    }).done(function (data) {
        if (data && data.query && data.query.users) {
            data.query.users.forEach(user => {
                if (user.blockid) {
                    const u = user.name;
                    if (userData[u] && userData[u].$blockLink) {
                        // Underline the block link to indicate this user is blocked
                        userData[u].$blockLink.css({
                            'text-decoration': 'underline',
                            'font-weight': 'bold',
                            'color': '#c00',
                            'cursor': 'pointer'
                        });
                    }
                }
            });
        }

        addButtonAndSetup(userData);
    }).fail(function () {
        addButtonAndSetup(userData);
    });

    // 4. Add control button and setup click
    function addButtonAndSetup(userData) {
        const $btn = $('<button>')
            .text('Open all user AbuseLogs, talk deletion, and show summary')
            .css({
                margin: '1em 0',
                padding: '6px 12px',
                cursor: 'pointer'
            });

        $content.prepend($btn);

        $btn.on('click', function () {
            const usernames = Object.keys(userData);
            if (usernames.length === 0) {
                alert('No users found to process.');
                return;
            }

            let summaryLines = [];
            usernames.forEach(username => {
                const data = userData[username];
                const latestLogId = data.latestLogId;
                const extraHits = parseInt(data.extraHits, 10);

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

                // Open User Talk deletion page with prefilled reason
                const talkDeleteUrl = mw.util.getUrl('Special:Delete/' + 'User_talk:' + encodeURIComponent(username), {
                    reason: `Talk page of an indefinitely blocked user that has little value. The content was: blahblah.`
                });
                window.open(talkDeleteUrl, '_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);
        });
    }
});