Skip to content

Instantly share code, notes, and snippets.

@nunocunha
Last active February 28, 2026 15:30
Show Gist options
  • Select an option

  • Save nunocunha/2484bfcf3e4d0b743da26c592582da82 to your computer and use it in GitHub Desktop.

Select an option

Save nunocunha/2484bfcf3e4d0b743da26c592582da82 to your computer and use it in GitHub Desktop.
Adds functionality to collapse entire Youtube comments on double-click, leaving only the thumbnail and context menu visible
// ==UserScript==
// @name Collapse Youtube comments
// @namespace https://nunocunha.pt/
// @version 1.0.3
// @description Adds functionality to collapse entire Youtube comments on double-click, leaving only the thumbnail and context menu visible
// @author Nuno Cunha
// @match https://www.youtube.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @license MIT
// @updateURL https://gist.github.com/nunocunha/2484bfcf3e4d0b743da26c592582da82/raw/collapse-youtube-comments.user.js
// @downloadURL https://gist.github.com/nunocunha/2484bfcf3e4d0b743da26c592582da82/raw/collapse-youtube-comments.user.js
// ==/UserScript==
/*
* MIT License
*
* Copyright (c) 2026 Nuno Cunha
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
(function() {
'use strict';
function showComment(comment) {
comment.dataset.isHidden = false;
comment.style.height = "";
comment.style.overflow = "";
const commentBody = comment.querySelector('#comment #body #main');
if (commentBody != null) {
commentBody.style.visibility = "";
}
}
function hideComment(comment) {
const avatarHeight = comment.querySelector('yt-img-shadow')?.clientHeight ?? 36;
comment.dataset.isHidden = true;
comment.style.height = `${avatarHeight}px`;
comment.style.overflow = "hidden";
const commentBody = comment.querySelector('#comment #body #main');
if (commentBody != null) {
commentBody.style.visibility = "hidden";
}
}
function toggleCommentVisibility(comment) {
const isHidden = JSON.parse(comment.dataset.isHidden?.toLocaleLowerCase() ?? false);
if (isHidden) {
showComment(comment);
} else {
hideComment(comment);
}
}
const bodyObserver = new MutationObserver((mutationList) => {
const newNodes = mutationList.map(m => [...m.addedNodes]).flat();
const comments = newNodes.filter(n => n.nodeName.toLocaleLowerCase() === 'ytd-comment-thread-renderer');
comments.forEach((comment) => {
const commentBody = comment.querySelector('#body');
commentBody.addEventListener('dblclick', () => toggleCommentVisibility(comment));
});
});
bodyObserver.observe(document.querySelector('ytd-app'), {childList: true, subtree: true});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment