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.

目前为 2021-06-02 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Hacker News Contextual Comments (sticky tree)
  3. // @version 0.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.
  5. // @author phil294
  6. // @match https://news.ycombinator.com/item?id=*
  7. // @icon https://www.google.com/s2/favicons?domain=ycombinator.com
  8. // @grant GM_addStyle
  9. // @namespace https://greasyfork.org/users/779094
  10. // ==/UserScript==
  11.  
  12. GM_addStyle(`
  13. tr.comtr {
  14. position: sticky;
  15. display: block;
  16. background: #f6f6ef;
  17. min-height: 40px;
  18. cursor: pointer;
  19. }
  20. tr.comtr.noshow {
  21. display: none;
  22. }`)
  23.  
  24. const sticky_row_height = 40
  25. let i_zindex = 0
  26.  
  27. for(const com of document.querySelectorAll('tr.comtr')) {
  28. const nesting_level = com.querySelector('.ind > img').width / 40
  29. com.style.top = nesting_level * sticky_row_height
  30. com.style.zindex = ++i_zindex
  31. com.onclick = async () => {
  32. com.querySelector('a.togg').click()
  33. com.style.position = 'static'
  34. com.scrollIntoView({block: "start", inline: "nearest", behavior: "smooth"})
  35. com.style.position = 'sticky'
  36. }
  37. }