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.

目前为 2019-08-09 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name MetaFilter highlight selected comment
  3. // @version 3
  4. // @grant none
  5. // @match http://*.metafilter.com/*
  6. // @match https://*.metafilter.com/*
  7. // @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.
  8. // @locale en-us
  9. // @namespace https://greasyfork.org/users/324881
  10. // ==/UserScript==
  11.  
  12. function highlightSelectedComment() {
  13. // first, clear all borders when this function is called.
  14. let allCommentDivs = document.getElementsByClassName('comments');
  15. for (commentDiv of allCommentDivs) {
  16. commentDiv.style.outline = '';
  17. commentDiv.style.outlineOffset = '';
  18. }
  19. // then if there's a hash/fragment ID, highlight it.
  20. if(window.location.hash) {
  21. let fragment = window.location.hash.substring(1);
  22. let anchor = document.getElementsByName(fragment )[0];
  23. let divToHighlight = anchor.nextSibling;
  24. divToHighlight.style.outline = '.3em solid #9cc754';
  25. divToHighlight.style.outlineOffset = '.3em';
  26. divToHighlight.lastChild.innerHTML += 'Selected comment';
  27. }
  28. }
  29.  
  30. // Attach event listeners
  31.  
  32. highlightSelectedComment(); // In case this script runs after the window load event, highlight as soon as Greasemonkey runs the script.
  33. window.addEventListener('load',highlightSelectedComment); // In case this script runs before the window load event, highlight when that event fires.
  34. window.addEventListener('hashchange',highlightSelectedComment); // Run every time the hash changes.