User:Username/BlockAbuser.js

From Test Wiki
Revision as of 02:10, 21 January 2026 by Username (talk | contribs) (edit)
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' ] ).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 );
    } );

} );