Jump to content

User:Rusalkii/hideOrHighlightInvolvedRfDs.js

From Wikipedia, the free encyclopedia
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.
/** This script highlights XfD discussions where the current user has
 *  participated in, or hides them. It should probably be incorporated into XFCD.
 */
(function() {
    'use strict';
    var debug = false;
    
    function log(text) {
    	if (debug) {
    		console.log(text);
    	}
    }
    
    // Only run on supported XfD pages, and set corresponding headings
    var venueHeadingLevel;
    if (mw.config.get('wgPageName').match(/Wikipedia:Redirects_for_discussion/) ||
    	mw.config.get('wgPageName').match(/Wikipedia:Files_for_discussion/) ||
    	mw.config.get('wgPageName').match(/Wikipedia:Miscellany_for_deletion/) ||
    	mw.config.get('wgPageName').match(/Wikipedia:Templates_for_discussion/)) {
    	venueHeadingLevel = '.mw-heading.mw-heading4'; 
    } else if (mw.config.get('wgPageName').match(/Wikipedia:Articles_for_deletion/) ||
    	       mw.config.get('wgPageName').match(/Wikipedia:Categories_for_discussion/) ||
    	       mw.config.get('wgPageName').match(/Commons:Deletion_requests/)) {
    	venueHeadingLevel = '.mw-heading.mw-heading3';
    } else { 
    	log('Not a deletion venue');
    	return;
    }
    
    // Get the current username
    const currentUser = mw.config.get('wgUserName');    

    function addStyles() {
        const css = `
            .rfd-user-participated {
                background-color: rgba(255, 255, 100, 0.15);
=            }
	        .rfd-user-participated.rfd-hidden {
	            display: none;
	        }
	        #rfd-toggle {
	            position: fixed;
	            bottom: 10px;
	            left: 10px;
	            z-index: 1000;
            	background: rgba(248, 249, 250, 0.8);
            	border: 1px solid #ccc;
	            padding: 5px 10px;
	            cursor: pointer;
	            border-radius: 4px;
	        }
        `;
        
        $('<style>').text(css).appendTo('head');
    }
    
    // Add button to hide/unhide
    let hideParticipated = false;
	function addToggle() {
	    const toggle = $('<button id="rfd-toggle">Hide my discussions</button>');
	    $('body').append(toggle);
	    
	    $('#rfd-toggle').click(function() {
	        hideParticipated = !hideParticipated;
	        $(this).text(hideParticipated ? 'Show my discussions' : 'Hide my discussions');
	        $('.rfd-user-participated').toggleClass('rfd-hidden', hideParticipated);
	    });
	}
    
	// Highlight discussions the user has participated in
    function highlightDiscussions() {

        // Looking at headers and content between them
        let highlightedCount = 0;
        const headers = $(venueHeadingLevel);
        log(`Found ${headers.length} headers`);
        
        headers.each(function(index) {
            const header = $(this);
            const content = header.nextUntil(venueHeadingLevel);

            // Check if the user's name appears in the discussion anywhere other than relists
            const userLink = '/wiki/User:' + currentUser;
            log(userLink);
            
            const contentClone = content.clone();
            contentClone.find('.xfd_relist').remove();
			const cloneLinks = contentClone.find('a[href*="' + userLink + '"]');
            
            if (cloneLinks.length !== 0) {
            	log(`Found user participation in discussion ${index} (header: ${header.text().substring(0, 30)}...)`);

            	// Wrap the discussion in a div and highlight that
	            const wrapper = $('<div class="rfd-user-participated"></div>');
	            header.before(wrapper);
	            wrapper.append(header).append(content);
	        
	            highlightedCount++;
            }
        });
        
        log(`Highlighted ${highlightedCount} discussions`);
        
        const finalHighlightCount = $('.rfd-user-participated').length;
        log(`Highlighting complete. Total elements highlighted: ${finalHighlightCount}`);
    }
    
    $(document).ready(function() {
        addStyles();
        addToggle();
        setTimeout(function() {
            highlightDiscussions();
            
        }, 200);
    });
})();