r/GreaseMonkey • • Jun 18 '26 • Bravo 1

Are there any userscripts to collapse child comments?

Good afternoon everyone,

I would like to know if there's any userscript that collapses child comments. It just makes it harder reading the comments under posts when all of them are expanded

Thanks in advance btw I'm using Tampermonkey if that's of any importance

4 Upvotes

6 comments sorted by

5

u/mapsedge Jun 18 '26 edited Jun 18 '26 • Bravo

EDIT: It should be noted that you're going to want your script to wait until the comments section loads or it won't have anything to do. Point #2: Reddit uses infinite scroll, so you'll want your script to run on every scroll event while avoiding elements that have already been collapsed.

Claude AI is a huge help here:

document.querySelectorAll('shreddit-comment[depth]').forEach(el => {
    const depth = parseInt(el.getAttribute('depth'));
    if (depth > 0) {
      const summary = el.querySelector('summary');
      if (summary) summary.click();
    }
  });

1

u/bboyjkang 28d ago edited 28d ago

Thank you so much for making this comment!

I've been using old Reddit with Reddit Enhancement Suite (RES) for years, and one of the main reasons I've stuck with it is the ability to control comment nesting depth. Specifically, I want to see top-level comments and their direct replies, but hide replies to replies and anything deeper.

For years, I've been using this simple bookmarklet on old Reddit:

Old Reddit — Bookmarklet (one-liner)

javascript:$('.commentarea .child .child').toggle();void 0

Old Reddit — Expanded version with explanations

javascript:(function () {
    // Find nested child containers within the Reddit comment section.
    $('.commentarea .child .child').toggle();

    // Toggle deeper replies while keeping top-level comments and direct replies visible.
    // Prevent the bookmarklet from returning a page value.
    void 0;
})();

I have it saved as a bookmark on my browser's bookmarks bar. Whenever I want to hide or show deeper replies, I just click it.

Lately, I've been seeing more comments about Reddit making old Reddit harder to use, so I wanted to check whether there had been any new developments since the last time I looked into switching.

I asked ChatGPT whether there were any extensions that offered similar functionality. It suggested Reddit Redux (around 1,000 users) and Sink It for Reddit (around 10,000 users), but neither did exactly what I wanted. They offer comment-collapsing features, but I couldn't find a way to preserve the first level of replies while hiding everything deeper. It seemed to be all or nothing: either just the top-level comments, or all the nested replies.

I explained this to ChatGPT, and it suggested that a Tampermonkey userscript might be a solution. I asked it to find any existing scripts, and it pointed me to this thread and your comment.

I pasted your code into ChatGPT and asked it to combine the approach with what I wanted from my old bookmarklet, and it worked!

New Reddit — Working bookmarklet (one-liner)

javascript:(()=>{document.querySelectorAll('shreddit-comment[depth]').forEach(e=>{if(Number(e.getAttribute('depth'))>1)e.style.display=e.style.display==='none'?'':'none'});void 0})()

New Reddit — Expanded version with explanations

javascript:(() => {
    // Find all comments with a depth attribute.
    document.querySelectorAll('shreddit-comment[depth]').forEach(element => {
        // Get the comment depth (0 = top-level, 1 = direct replies, 2 = replies to replies, etc.).
        const depth = Number(element.getAttribute('depth'));

        // Only toggle comments deeper than the selected nesting depth.
        if (depth > 1) {
            // Hide visible comments and show previously hidden comments.
            element.style.display = element.style.display === 'none' ? '' : 'none';
        }
    });

    // Prevent the bookmarklet from returning a page value.
    void 0;
})();

Customizing the comment nesting depth

You can adjust how many levels of replies remain visible by changing the number in if (depth > 1):

  • Depth 0: Only top-level comments are visible.
  • Depth 1: Top-level comments and their direct replies are visible.
  • Depth 2: Top-level comments, their direct replies, and replies to those replies are visible.
  • Depth 3: Top-level comments and three levels of nested replies are visible.

For example, if (depth > 1) keeps comments visible through depth 1, while if (depth > 2) keeps comments visible through depth 2.

Any comments deeper than your chosen limit are hidden. Clicking the bookmarklet again brings them back.

For anyone else who stumbles upon this thread, here's how to use it:

Right-click your browser's bookmarks bar, select "Add page" (or "Add bookmark"), give it a name, and paste the one-line JavaScript into the URL field. Then, while viewing a Reddit comment thread, click the bookmarklet to toggle deeper replies.

The only limitation is that it operates on comments that have already loaded. Since new Reddit uses infinite scrolling, newly loaded comments won't automatically be affected, so you may need to run it again.

Anyway, thanks again for sharing your code. This was one of the biggest things keeping me on old Reddit, if not the biggest. There are features I actually like about new Reddit, such as being able to search within comments, which is one reason I've been switching back and forth between the two interfaces. Having this bookmarklet working on new Reddit will definitely make me much more comfortable using it going forward.

1

u/mapsedge 27d ago • Heartwarming

You're welcome!

2

u/alien2003 Jun 18 '26

It’s not possible to determine if a comment was made by child currently as there is no age verification badges or something like that on Reddit

2

u/RanMouri26 Jun 18 '26

What are you talking about? Child comments on Reddit are comments below other comments

2

u/mapsedge Jun 18 '26

Heh. Good one.