Hacker News Contextual Comments (sticky tree)

Sticks the first line of all comments in the visible comment tree to the top of the screen so you always know where exactly you are. See the screenshots for a better understanding. Also adds the ability to expand/collapse a comment by clicking anywhere on it.

  1. // ==UserScript==
  2. // @name Hacker News Contextual Comments (sticky tree)
  3. // @version 0.4.1
  4. // @description Sticks the first line of all comments in the visible comment tree to the top of the screen so you always know where exactly you are. See the screenshots for a better understanding. Also adds the ability to expand/collapse a comment by clicking anywhere on it.
  5. // @author phil294
  6. // @match https://news.ycombinator.com/item?id=*
  7. // @icon https://www.google.com/s2/favicons?domain=ycombinator.com
  8. // @grant none
  9. // @namespace https://greasyfork.org/users/779094
  10. // ==/UserScript==
  11.  
  12. const style = document.createElement('style')
  13. style.type = 'text/css'
  14. style.textContent = `
  15. tr.comtr {
  16. position: sticky;
  17. display: block;
  18. background: #f6f6ef;
  19. min-height: 40px;
  20. cursor: pointer;
  21. }
  22. tr.comtr.noshow {
  23. display: none;
  24. }
  25. td.default > div:first-child {
  26. margin: 0 !important;
  27. }
  28. td.default > br {
  29. display: none;
  30. }`
  31. document.head.appendChild(style)
  32.  
  33. let i_zindex = 0
  34.  
  35. for(const com of document.querySelectorAll('tr.comtr')) {
  36. const nesting_level = com.querySelector('.ind > img').width / 40
  37. com.style.top = `calc(${nesting_level} * 2.3em)`
  38. com.style.zindex = ++i_zindex
  39. com.onclick = async () => {
  40. com.querySelector('a.togg').click()
  41. com.style.position = 'static'
  42. com.scrollIntoView({block: "start", inline: "nearest", behavior: "smooth"})
  43. com.style.position = 'sticky'
  44. }
  45. }