User:Username/BlockAbuser.js

From Test Wiki
Revision as of 01:55, 21 January 2026 by Username (talk | contribs) (Let me just test this out)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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.api', 'mediawiki.util' ] ).then( function () {

    if ( mw.config.get( 'wgCanonicalSpecialPageName' ) !== 'AbuseLog' ) {
        return;
    }

    const api = new mw.Api();
    const $content = $( '#mw-content-text' );

    const $btn = $( '<button>' )
        .text( 'Generate spambot block summary' )
        .css( {
            margin: '10px 0',
            padding: '6px 10px',
            cursor: 'pointer'
        } );

    $content.prepend( $btn );

    $btn.on( 'click', async function () {

        const params = new URLSearchParams( window.location.search );
        const username = params.get( 'wpSearchUser' );

        if ( !username ) {
            alert( 'Open Special:AbuseLog filtered to a single user first.' );
            return;
        }

        try {
            // 1. Get user info (edit count)
            const userInfo = await api.get( {
                action: 'query',
                list: 'users',
                ususers: username,
                usprop: 'editcount'
            } );

            const user = userInfo.query.users[0];

            if ( user.editcount > 0 ) {
                alert( 'This account has edits. Script only applies to 0-edit accounts.' );
                return;
            }

            // 2. Get AbuseLog entries (last 24h)
            const since = new Date( Date.now() - 86400000 ).toISOString();

            const abuseData = await api.get( {
                action: 'query',
                list: 'abuselog',
                afluser: username,
                afllimit: 'max',
                aflstart: since
            } );

            const logs = abuseData.query.abuselog;

            if ( !logs.length ) {
                alert( 'No recent AbuseFilter hits found.' );
                return;
            }

            const mostRecent = logs[0];
            const extraHits = logs.length - 1;

            let summary =
                'Spambot or spam-only account. The following cites the action they committed ' +
                '[[Special:AbuseLog/' + mostRecent.id + ']]';

            if ( extraHits > 0 ) {
                summary +=
                    ' (+[[Special:AbuseLog/' + username + '|' + extraHits + ']])';
            }

            // 3. Display output
            mw.notify(
                $( '<pre>' ).text( summary ),
                { autoHide: false }
            );

        } catch ( e ) {
            console.error( e );
            alert( 'Failed to generate summary. See console.' );
        }
    } );
} );