MetaFilter number all comments

On MetaFilter.com, adds a comment number like "comment 1 of 30" to each comment.

当前为 2019-08-27 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name        MetaFilter number all comments
// @version     10
// @grant       none
// @match       http://*.metafilter.com/*
// @match       https://*.metafilter.com/*
// @description On MetaFilter.com, adds a comment number like "comment 1 of 30" to each comment.
// @locale      en-us
// @run-at      document-idle
// @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 = Array.from(document.getElementsByClassName('tehhundUserScriptCommentCount'));
  for (let currentSpan of allCommentCountSpans) { currentSpan.remove(); }

  // then add counts to every comment div.
  let allCommentDivs = document.getElementsByClassName('comments');
  allCommentDivs = Array.from(allCommentDivs).filter( function(div) { return div.previousSibling.tagName == 'A'; }); // Some "comment" divs are not actually comments. All comments, and only comments, are preceeded by an anchor tag. So this filters down to only real comment divs.
  for (let [i,divToHighlight] of allCommentDivs.entries()) {
    divToHighlight.lastChild.innerHTML += '<span class=\'tehhundUserScriptCommentCount\'>Comment ' + (i+1) + ' of ' + allCommentDivs.length + '. </span>';
  }
}

function runIfCommentAdded(mutationsList, observer) { // When a mutation event occurs, check if it was adding a comment and if so renumber the comments.
  for (let mutation of mutationsList) { // Loop over all mutations for this event.
    try {
      if (mutation.target.previousSibling.tagName == 'A') { // If the target's previousSibling is an anchor tag, it's probably an added comment so renumber the comments. Otherwise do nothing.
        addCountToComments();
      }
    } catch(e) { /* previousSibling is null if the mutation was adding a Favorite, and that throws an error on the console every time. This try/catch discards the error. */ }
  }
}

addCountToComments(); // Run once when the page is idle

// After the initial run, observe the main content div for changes such as new comments and run addCountToComments() again.
const mutationObserver = new MutationObserver(runIfCommentAdded);
mutationObserver.observe(document.getElementById('posts'), { attributes: true, childList: true, subtree: true })