Collapse comments

Add a button to collapse comments of lwn.net

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

You will need to install an extension such as Tampermonkey to install this script.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name        Collapse comments
// @namespace   https://franklinyu.github.io
// @match       https://lwn.net/Articles/*
// @grant       none
// @version     0.1
// @author      FranklinYu
// @description Add a button to collapse comments of lwn.net
// @license     Apache-2.0
// ==/UserScript==

const COLLAPSED = '▷'
const EXPANDED = '▽'

function getTree(comment) {
  const tree = comment?.nextElementSibling
  if (tree && tree.classList.contains('Comment')) {
    return tree
  } else {
    return null
  }
}

for (const comment of document.getElementsByClassName('CommentBox')) {
  if (!getTree(comment)) {
    continue  // leaf comment
  }

  const toggle = document.createElement('button')
  comment.append(toggle)
  toggle.textContent = EXPANDED

  toggle.addEventListener('click', e => {
    const button = e.target
    const tree = getTree(button.parentElement)
    if (!tree) {
      console.warn('unexpected comment event: ', e)
    }

    if (button.textContent === EXPANDED) {
      button.textContent = COLLAPSED
      tree.style.display = 'none'
    } else {
      button.textContent = EXPANDED
      tree.style.display = ''
    }
  })
}