User:Sam Sailor/Scripts/BoldMyName.js

Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/**
 * Script Name: BoldMyName
 * Description: Highlights your own username in bold and color (default: bright blue)
 * on revision history pages, Recent Changes, Recent Changes Linked,
 * and your Watchlist. This makes it easier to spot your own contributions
 * or actions at a glance.
 * Customization:
 * To change the color, add the following line to your common.js file, replacing 
 * '#F0A000' with your preferred hexadecimal color code (e.g., #00AA00 for green):
 * window.historyBoldingColor = '#F0A000';
 * Installation:
 * To install this script, add the following line to your personal common.js page 
 * (e.g., User:YOURNAME/common.js) and bypass your browser cache:
 * importScript('User:Sam_Sailor/Scripts/BoldMyName.js');
 * */
$(function() {
    var boldingColor = window.historyBoldingColor || '#0000FF';
    
    // Define the list of page types where the script should run
    var allowedActions = ['history'];
    var allowedSpecialPages = ['Recentchanges', 'Recentchangeslinked', 'Watchlist'];
    
    var currentPageAction = mw.config.get('wgAction');
    var currentPageSpecialPage = mw.config.get('wgCanonicalSpecialPageName');
    
    // Check if the current page is one of the allowed action pages or special pages
    // $.inArray returns -1 if the item is not found in the array.
    if (
        $.inArray(currentPageAction, allowedActions) === -1 &&
        $.inArray(currentPageSpecialPage, allowedSpecialPages) === -1
    ) {
        return; // Exit if the page is not in the allowed list
    }
    
    // Ensure mediawiki.util is loaded before trying to add CSS
    mw.loader.using(['mediawiki.util'], function() {
        // Define the CSS style
        var css = 'a.history-bolding { color: ' + boldingColor + ' !important; font-weight: bold; background-color: transparent; }';
        mw.util.addCSS(css);
        
        // Apply the style to the current user's links
        $('.mw-userlink').each(function() {
            var $this = $(this);
            // Compare the link text (username) to the current user's name
            if ($this.text() == mw.config.get('wgUserName'))
                $this.addClass('history-bolding');
        });
    });
});