User:Panamitsu/script/Talk page user mute.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:Panamitsu/script/Talk page user mute. |
// 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";
}
}
}
} );