Collapse HackerNews Parent Comments

Adds vertical bars to the left of the comments, enabling you to easily collapse the parent comments. It also can leave only a specified number of comments expanded and auto-collapse the rest

目前為 2020-08-21 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name Collapse HackerNews Parent Comments
  3. // @description Adds vertical bars to the left of the comments, enabling you to easily collapse the parent comments. It also can leave only a specified number of comments expanded and auto-collapse the rest
  4. // @author BLBC (github.com/hjk789, greasyfork.org/users/679182-hjk789)
  5. // @copyright 2020+, BLBC (github.com/hjk789, greasyfork.org/users/679182-hjk789)
  6. // @version 1.0
  7. // @homepage https://github.com/hjk789/Creations/tree/master/Userscripts/Collapse-HackerNews-Parent-Comments
  8. // @license https://github.com/hjk789/Creations/tree/master/Userscripts/Collapse-HackerNews-Parent-Comments#license
  9. // @grant none
  10. // @include https://news.ycombinator.com/item*
  11. // @namespace https://greasyfork.org/users/679182
  12. // ==/UserScript==
  13.  
  14.  
  15. //--------------- Settings -----------------
  16.  
  17. const collapse = true // Whether all comments, other than the number of comments below, should be collapsed.
  18. // If set to false, all comments will be left expanded and the settings below have no effect.
  19. const numberOfRoots = 3
  20. const numberOfReplies = 2
  21. const numberOfRepliesOfReplies = 1
  22. //------------------------------------------
  23.  
  24.  
  25.  
  26. // HackerNews puts a 1x1 image before each comment and sets it's width according to each comment depth. Each level of depth adds
  27. // 40px of width to this image, starting from 0 which are the root comments. The ones with a 14px width are flagged comments and
  28. // the "More comments" link. And because HackerNews layout doesn't have any easier way of identifying the hierarchy of the
  29. // comments (it's just a list of comments pushed to the right), that's the only way I've found to achieve this.
  30.  
  31. spacingImgs = document.querySelectorAll(".ind img[height='1']:not([width='14'])")
  32.  
  33. let root = 0
  34. let i = 0
  35. let commentHier = []
  36.  
  37. if (collapse) spacingImgs[0].parentElement.parentElement.querySelector(".togg").click()
  38.  
  39. collapseAll = setInterval(function() // An interval of 1ms is being used to prevent the page from freezing until it finishes. Also, it creates a cool effect when
  40. { // the comments are being collapsed. It does make it take a few more seconds to finish in comment-heavy posts (150+) though.
  41. const level = spacingImgs[i].width / 40
  42.  
  43. let commentToggle = spacingImgs[i].parentElement.parentElement.querySelector(".togg")
  44. if (collapse) commentToggle.click() // Collapse the first comment so it's expanded again below and you can read the first comment while the collapsing is under process
  45.  
  46.  
  47. // Store the current hierarchy in an array
  48.  
  49. if (commentHier[level] == null)
  50. commentHier.push(commentToggle)
  51. else
  52. commentHier[level] = commentToggle
  53.  
  54. const commentContainer = spacingImgs[i].parentElement.parentElement.parentElement.parentElement.parentElement
  55. commentContainer.style = "border-top: 5px transparent solid" // To visually separate each vertical bar
  56. spacingImgs[i].parentElement.style = "position: relative"
  57.  
  58. let divs = []
  59. for (j = spacingImgs[i].width; j >= 0; j -= 40) // Start adding the vertical bar from the current depth and go backwards
  60. {
  61. // Create the vertical bar
  62.  
  63. const div = document.createElement("div");
  64. div.commentHier = commentHier[j/40] // Store in an attribute of the element this comment's parent respective to the level of the vertical bar, for easy access
  65. div.onclick = commentHier[j/40].onclick // When a vertical bar is clicked, collapse the respective parent comment
  66. div.onmouseup = function(e)
  67. {
  68. if (e.target.commentHier.getBoundingClientRect().y < 0) // If the parent comment is off-screen above,
  69. e.target.commentHier.scrollIntoView() // scroll to it
  70. }
  71.  
  72. let style = "left: " + (-5 + j) + "px; width: 12px; background-color: lightgray; position: absolute; z-index: 99; "
  73.  
  74. // Make it so that the vertical bars are only separated when followed by comments of same level of depth
  75.  
  76. if (j == spacingImgs[i].width && spacingImgs[i-1] != null && spacingImgs[i].width <= spacingImgs[i-1].width)
  77. style += "top: 5px; height: calc(100% + 8px); "
  78. else
  79. style += "top: 0px; height: calc(100% + 13px); "
  80.  
  81. div.style = style
  82.  
  83. divs.push(div)
  84. }
  85.  
  86. for (j = divs.length - 1; j >= 0; j--)
  87. spacingImgs[i].parentElement.appendChild(divs[j])
  88.  
  89. i++
  90.  
  91. if (i == spacingImgs.length) // When finished collapsing and adding the vertical bars to all comments, now it's time to expand only a few of the first comments
  92. {
  93. clearInterval(collapseAll)
  94.  
  95. if (collapse)
  96. {
  97. spacingImgs[0].parentElement.parentElement.querySelector(".togg").click() // Expand the first comment
  98. root = 0
  99. for (i=0; i < spacingImgs.length; i++)
  100. {
  101. commentToggle = spacingImgs[i].parentElement.parentElement.querySelector(".togg")
  102.  
  103. if (spacingImgs[i].width == 0) // If it's a root comment
  104. {
  105. root++
  106. if (root == numberOfRoots + 1) break // If there's already <numberOfRoots> comments expanded, then stop expanding
  107.  
  108. commentToggle.click()
  109. sub40 = 0
  110. sub80 = 0
  111. }
  112. else if (spacingImgs[i].width == 40 && sub40 < numberOfReplies) // If it's a reply to the root comment, only expand up to <numberOfReplies>
  113. {
  114. commentToggle.click()
  115. sub40++
  116. sub80 = 0
  117. }
  118. else if (spacingImgs[i].width == 80 && sub80 < numberOfRepliesOfReplies) // If it's a reply to the reply, only expand up to <numberOfRepliesOfReplies>
  119. {
  120. commentToggle.click()
  121. sub80++
  122. }
  123. }
  124. }
  125. }
  126.  
  127. }, 1)
  128.