MediaWiki:Common.js

From Qa2triots Wiki
Revision as of 11:59, 7 June 2026 by Yoot (talk | contribs) (Created page with "Any JavaScript here will be loaded for all users on every page load.: // makes admins red mw.loader.using( [ 'mediawiki.api', 'mediawiki.util' ] ).then( function () { 'use strict'; var api = new mw.Api(); var adminPromise = null; function normalizeUsername( username ) { return username .replace( /_/g, ' ' ) .trim() .toLowerCase(); } function fetchAdministrators( continuation, names ) { v...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump 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.
/* Any JavaScript here will be loaded for all users on every page load. */

// makes admins red
mw.loader.using( [ 'mediawiki.api', 'mediawiki.util' ] ).then( function () {
    'use strict';

    var api = new mw.Api();
    var adminPromise = null;

    function normalizeUsername( username ) {
        return username
            .replace( /_/g, ' ' )
            .trim()
            .toLowerCase();
    }

    function fetchAdministrators( continuation, names ) {
        var params = {
            action: 'query',
            list: 'allusers',
            augroup: 'sysop',
            aulimit: 'max',
            formatversion: 2
        };

        if ( continuation ) {
            Object.keys( continuation ).forEach( function ( key ) {
                params[ key ] = continuation[ key ];
            } );
        }

        return api.get( params ).then( function ( data ) {
            data.query.allusers.forEach( function ( user ) {
                names.add( normalizeUsername( user.name ) );
            } );

            if ( data.continue ) {
                return fetchAdministrators( data.continue, names );
            }

            return names;
        } );
    }

    function getAdministrators() {
        if ( !adminPromise ) {
            adminPromise = fetchAdministrators( null, new Set() );
        }

        return adminPromise;
    }

    function getLinkPageTitle( link ) {
        try {
            var url = new URL( link.href, window.location.href );
            var queryTitle = url.searchParams.get( 'title' );

            if ( queryTitle ) {
                return queryTitle.replace( /_/g, ' ' );
            }

            var articlePath = mw.config.get( 'wgArticlePath' );
            var pieces = articlePath.split( '$1' );

            if (
                pieces.length === 2 &&
                url.pathname.indexOf( pieces[ 0 ] ) === 0 &&
                (
                    pieces[ 1 ] === '' ||
                    url.pathname.slice( -pieces[ 1 ].length ) === pieces[ 1 ]
                )
            ) {
                var encodedTitle;

                if ( pieces[ 1 ] ) {
                    encodedTitle = url.pathname.slice(
                        pieces[ 0 ].length,
                        -pieces[ 1 ].length
                    );
                } else {
                    encodedTitle = url.pathname.slice( pieces[ 0 ].length );
                }

                return decodeURIComponent( encodedTitle ).replace( /_/g, ' ' );
            }
        } catch ( error ) {
        }

        return ( link.getAttribute( 'title' ) || '' ).replace( /_/g, ' ' );
    }

    function getUsernameFromLink( link ) {
        var pageTitle = getLinkPageTitle( link );
        var namespaces = mw.config.get( 'wgFormattedNamespaces' );
        var userNamespace = namespaces[ 2 ] || 'User';
        var prefix = userNamespace + ':';

        if (
            pageTitle.slice( 0, prefix.length ).toLowerCase() !==
            prefix.toLowerCase()
        ) {
            return null;
        }

        return pageTitle
            .slice( prefix.length )
            .split( '#' )[ 0 ];
    }

    function markAdministratorLinks( root ) {
        var $root = root && root.jquery ? root : $( root || document );

        getAdministrators().then( function ( administrators ) {
            var $links = $root
                .find( 'a.mw-userlink' )
                .add( $root.filter( 'a.mw-userlink' ) );

            $links.each( function () {
                var username = getUsernameFromLink( this );

                if (
                    username &&
                    administrators.has( normalizeUsername( username ) )
                ) {
                    this.classList.add( 'mw-admin-name' );
                }
            } );
        } );
    }

    $( function () {
        markAdministratorLinks( document );
    } );

    mw.hook( 'wikipage.content' ).add( markAdministratorLinks );
} );