On MetaFilter.com, adds a "comment 1 of 30" to each comment.
当前为
// ==UserScript==
// @name MetaFilter number all comments
// @version 2
// @grant none
// @match http://*.metafilter.com/*
// @match https://*.metafilter.com/*
// @description On MetaFilter.com, adds a "comment 1 of 30" to each comment.
// @locale en-us
// @namespace https://greasyfork.org/users/324881
// ==/UserScript==
function addCountToComments() {
// first, clear all counts in case this runs more than once on a page.
let allCommentCountSpans = document.getElementsByClassName('tehhundUserScriptCommentCount');
let loopCount = 0;
while (allCommentCountSpans.length > 0) { // .remove() is failing on random spans for an unknown reason, so loop this until it finishes removing everything. <Gary Oldman voice>EVERYTHING!</Gary Oldman voice>
loopCount += 1;
console.log("Number of loops: " + loopCount);
for (currentSpan of allCommentCountSpans) {
currentSpan.remove();
}
allCommentCountSpans = document.getElementsByClassName('tehhundUserScriptCommentCount'); // Grab all elements so the loop can check whether it's done.
}
// then add counts to every comment div.
let allCommentDivs = document.getElementsByClassName('comments');
allCommentDivs = Array.from(allCommentDivs).filter( function(div) { return div.previousSibling.tagName == 'A'; });
let index = 0;
for (let divToHighlight of allCommentDivs) {
index += 1;
divToHighlight.lastChild.innerHTML += '<span class=\'tehhundUserScriptCommentCount\'>. Comment ' + index + ' of ' + allCommentDivs.length + ' </span>';
}
}
// Attach event listeners
window.addEventListener('load',addCountToComments); // In case this script runs before the window load event, highlight when that event fires.
addCountToComments(); // In case this script runs after the window load event, highlight as soon as Greasemonkey runs the script.