User:Panamitsu/script/Talk page user mute.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.
// Mute a certain user from a page's edit history.

// Get username (WARNING: Document.currentScript does not work on Internet Explorer)
// The odd variable name is to prevent name conflicts with other scripts.
const TALK_PAGE_USER_MUTE_USERNAME = new URLSearchParams(document.currentScript.src).get("username");
const HEADING_CLASS = "mw-heading2";

$( document ).ready( function () {
   // Make sure the edit history page is open
   if (mw.config.get('wgNamespaceNumber') % 2 == 1) { // talk namespaces have odd numbers
	   	console.log("Blocking discussions with user: " + TALK_PAGE_USER_MUTE_USERNAME);
	   	
	   	// Go through all the discussions
	   	let headings = document.getElementsByClassName(HEADING_CLASS);
	   	for (var i=0; i < headings.length; i++) {
	   		let heading = headings[i];
	   		
	   		// Get all elements in the discussion and determine whether the muted user is in there
	   		let discussion_elems = [];
	   		let next = heading.nextElementSibling;
	   		let containsMutedUser = false;
	   		while (next != null && !next.className.includes(HEADING_CLASS)) {
	   			discussion_elems.push(next);
	   
	   			// Check if the muted user is in there
	   			if (next.innerHTML.toLowerCase().includes(TALK_PAGE_USER_MUTE_USERNAME.toLowerCase())) {
	   				containsMutedUser = true;
	   			}
	   			
	   			next = next.nextElementSibling;
	   		}
	   		
	   		// Hide the discussion if the muted user is in there
	   		if (containsMutedUser) {
	   			for (const elem of discussion_elems) {
	   				elem.style.display = "none";
	   			}
	   			// Change the heading to say that it was muted
	   			heading.innerText += " (redacted due to the presence of a muted user)";
	   			heading.style.color = "red";
	   		}
	   	}
    }
} );