User:Ahecht/Scripts/discussionrows.js
Appearance
< User:Ahecht | Scripts
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.
This code will be executed when previewing this page.
This user script seems to have a documentation page at User:Ahecht/Scripts/discussionrows.
$(document).ready( function() {
function highlightDiscussionRows() {
const colorA = '#f0f7ff';
const colorB = '#fffbf0';
const startMarkers = Array.from(document.querySelectorAll('span[data-mw-comment-start][id^="c-"]'));
let colorIndex = 0;
if (startMarkers.length === 0) {
console.warn("No 'data-mw-comment-start' markers found on this page.");
return;
}
startMarkers.forEach(startMarker => {
const commentId = startMarker.id;
const endMarker = document.querySelector(`[data-mw-comment-sig="${commentId.replace(/"/g, '\\"')}"]`);
if (!endMarker) return;
const chosenColor = (colorIndex % 2 === 0) ? colorA : colorB;
const walker = document.createTreeWalker(
document.querySelector('.mw-parser-output') || document.body,
NodeFilter.SHOW_TEXT
);
const blocksToColor = [];
let insideComment = false;
let node = walker.nextNode();
while (node) {
if (startMarker.compareDocumentPosition(node) & Node.DOCUMENT_POSITION_CONTAINED_BY ||
startMarker.compareDocumentPosition(node) & Node.DOCUMENT_POSITION_FOLLOWING) {
insideComment = true;
}
if (endMarker.compareDocumentPosition(node) & Node.DOCUMENT_POSITION_CONTAINED_BY ||
!(endMarker.compareDocumentPosition(node) & Node.DOCUMENT_POSITION_PRECEDING)) {
insideComment = false;
break;
}
if (insideComment) {
if (node.textContent.trim().length > 0) {
let block = node.parentElement.closest('p, dd, li, div.mw-parser-output > div, td');
if (block && !blocksToColor.includes(block)) {
blocksToColor.push(block);
}
}
}
node = walker.nextNode();
}
blocksToColor.forEach((block, bIndex) => {
block.style.backgroundColor = chosenColor;
if (blocksToColor.length > 1) { // If there are multiple paragraphs, close the margin gap and use padding instead
block.style.marginTop = '0px';
block.style.marginBottom = '0px';
block.style.paddingTop = '4px';
block.style.paddingBottom = '4px';
if (bIndex === 0) {
block.style.paddingTop = '8px';
}
if (bIndex === blocksToColor.length - 1) {
block.style.paddingBottom = '8px';
}
}
});
if (blocksToColor.length > 0) {
colorIndex++;
}
});
}
var nsNum = mw.config.get("wgNamespaceNumber");
if ((nsNum % 2 !== 0) || (nsNum == 4)) {highlightDiscussionRows();} // Talk: and Wikipedia: namespaces only
} );