User:Username/BlockAbuser.js: Difference between revisions

From Test Wiki
Jump to navigation Jump to search
Content deleted Content added
Let me just test this out
 
edit
Line 1: Line 1:
mw.loader.using( [ 'mediawiki.api', 'mediawiki.util' ] ).then( function () {
mw.loader.using( [ 'mediawiki.util' ] ).then( function () {


// Only run on Special:AbuseFilter
if ( mw.config.get( 'wgCanonicalSpecialPageName' ) !== 'AbuseLog' ) {
if ( mw.config.get( 'wgCanonicalSpecialPageName' ) !== 'AbuseFilter' ) {
return;
return;
}
}


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


// Very strict IPv4 / IPv6 detection
const $btn = $( '<button>' )
const ipRegex = /^(?:\d{1,3}\.){3}\d{1,3}$|:/;
.text( 'Generate spambot block summary' )
.css( {
margin: '10px 0',
padding: '6px 10px',
cursor: 'pointer'
} );


// Find all links inside the abuse log table/content
$content.prepend( $btn );
$( '#mw-content-text a' ).each( function () {
const $link = $( this );
const href = $link.attr( 'href' );
const text = $link.text();


$btn.on( 'click', async function () {
if ( !href || !text ) {
return;
}


// Only User: links
const params = new URLSearchParams( window.location.search );
const username = params.get( 'wpSearchUser' );
if ( !href.includes( '/wiki/User:' ) ) {

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


try {
const username = decodeURIComponent(
// 1. Get user info (edit count)
href.split( '/wiki/User:' )[1]
).replace( /_/g, ' ' );
const userInfo = await api.get( {
action: 'query',
list: 'users',
ususers: username,
usprop: 'editcount'
} );


// Skip IPs
const user = userInfo.query.users[0];
if ( ipRegex.test( username ) ) {
return;
}


if ( user.editcount > 0 ) {
// Only one checkbox per user
if ( seenUsers.has( username ) ) {
alert( 'This account has edits. Script only applies to 0-edit accounts.' );
return;
return;
}
}
seenUsers.add( username );


// 2. Get AbuseLog entries (last 24h)
// Create checkbox
const since = new Date( Date.now() - 86400000 ).toISOString();
const $checkbox = $( '<input>' )
.attr( {

const abuseData = await api.get( {
type: 'checkbox',
action: 'query',
'data-username': username
list: 'abuselog',
} )
afluser: username,
.css( {
afllimit: 'max',
marginRight: '4px',
aflstart: since
verticalAlign: 'middle'
} );
} );


// Insert checkbox before the username link
const logs = abuseData.query.abuselog;
$link.before( $checkbox );
} );


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.' );
}
} );
} );
} );

Revision as of 02:10, 21 January 2026

mw.loader.using( [ 'mediawiki.util' ] ).then( function () {

    // Only run on Special:AbuseFilter
    if ( mw.config.get( 'wgCanonicalSpecialPageName' ) !== 'AbuseFilter' ) {
        return;
    }

    const seenUsers = new Set();

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

    // Find all links inside the abuse log table/content
    $( '#mw-content-text a' ).each( function () {
        const $link = $( this );
        const href = $link.attr( 'href' );
        const text = $link.text();

        if ( !href || !text ) {
            return;
        }

        // Only User: links
        if ( !href.includes( '/wiki/User:' ) ) {
            return;
        }

        const username = decodeURIComponent(
            href.split( '/wiki/User:' )[1]
        ).replace( /_/g, ' ' );

        // Skip IPs
        if ( ipRegex.test( username ) ) {
            return;
        }

        // Only one checkbox per user
        if ( seenUsers.has( username ) ) {
            return;
        }
        seenUsers.add( username );

        // Create checkbox
        const $checkbox = $( '<input>' )
            .attr( {
                type: 'checkbox',
                'data-username': username
            } )
            .css( {
                marginRight: '4px',
                verticalAlign: 'middle'
            } );

        // Insert checkbox before the username link
        $link.before( $checkbox );
    } );

} );