MetaFilter highlight selected comment

On MetaFilter.com, adds a border to the selected comment to make it stand out visually, and adds "selected comment" to the small text to make it easy to search if you lose your place.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        MetaFilter highlight selected comment
// @version     14
// @grant       none
// @match       *://*.metafilter.com/*
// @description On MetaFilter.com, adds a border to the selected comment to make it stand out visually, and adds "selected comment" to the small text to make it easy to search if you lose your place.
// @namespace   https://greasyfork.org/users/324881
// ==/UserScript==

// on load, run highlight and attach event listeners
if (window.location.hash.substring(1) != "") highlightSelectedComment(window.location.hash.substring(1)); // Highlight selected comment on first load. No need to remove highlight on first load as it won't exist.
window.addEventListener('hashchange', handleHashChangeEvent); // Run every time the hash changes.

function handleHashChangeEvent(event) {
  if (event.oldURL.split('#')[1] != null) removeSelectedCommentHighlight(event.oldURL.split('#')[1]);
  if (event.newURL.split('#')[1] != null) highlightSelectedComment(event.newURL.split('#')[1]);
}

function highlightSelectedComment(newHash) {
  let divToHighlight = getTargetDiv(newHash);
  divToHighlight.style.outline = '.2em solid #9cc754';
  divToHighlight.style.outlineOffset = '.2em';
  //divToHighlight.lastChild.innerHTML += '<span class=\'tehhundUserScriptSelectedComment\'>Selected comment. </span>';
  divToHighlight.querySelector('.smallcopy').innerHTML += '<span class=\'tehhundUserScriptSelectedComment\'>Selected comment. </span>';
}

function removeSelectedCommentHighlight(oldHash) {
  let commentDiv = getTargetDiv(oldHash);

  // Remove styles
  commentDiv.style.outline = '';
  commentDiv.style.outlineOffset = '';

  // Remove 'Selected comment' text
  let allSelectedCommentsSpans = commentDiv.getElementsByClassName('tehhundUserScriptSelectedComment');
  for (selectedCommentSpan of allSelectedCommentsSpans) {
    selectedCommentSpan.remove();
  }
}

function getTargetDiv(hash) {
  let anchor = document.getElementsByName(hash)[0];
  let targetDiv = anchor.nextSibling;
  return targetDiv;
}