Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Hide live chat by default on YouTube live streams
// ==UserScript==
// @name YouTube - Hide Live Chat
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Hide live chat by default on live streams
// @author LM
// @match https://www.youtube.com/watch*
// @run-at document-end
// @grant none
// ==/UserScript==
function AddClass() {
var el = document.getElementById("live-chat-iframe");
if(el) {
el.parentElement.classList.add("yt-uix-expander-collapsed");
console.log('Hide Live Chat');
return true;
}
return false;
}
function KeepTrying(func, attempts, delay) {
console.log('Keep Trying ' + attempts);
if( !func() && attempts-1 > 0 ) {
window.setTimeout( function() {
KeepTrying(func, attempts-1, delay);
});
}
}
(function() {
'use strict';
KeepTrying( AddClass, 5, 10 );
})();
@foxmajik

This comment has been minimized.

Copy link

@foxmajik foxmajik commented Sep 3, 2018

It doesn't hide the chat in Chrome it just moves the "hide chat" button below the chat.

@b-mq

This comment has been minimized.

Copy link

@b-mq b-mq commented Nov 13, 2018

I edited the code
should work now ;)

`// ==UserScript==
// @name YouTube - Hide Live Chat
// @namespace http://tampermonkey.net/
// @Version 0.1
// @description Hide live chat by default on live streams
// @author LM
// @match https://www.youtube.com/watch*
// @run-at document-end
// @grant none
// ==/UserScript==

function PressHideButton() {
var el = document.getElementById("show-hide-button");
if(el) {
while(el.children.length > 0) {
if (el.firstChild.getAttribute("id") == "button") {
el.firstChild.click();
console.log('Hide Live Chat');
return true;
}
el = el.firstChild;
}
}
return false;
}

function KeepTrying(func, attempts, delay) {
console.log('Keep Trying ' + attempts);
if( !func() && attempts-1 > 0 ) {
window.setTimeout( function() {
KeepTrying(func, attempts-1, delay);
});
}
}

(function() {
'use strict';

KeepTrying( PressHideButton, 9, 10 );

})();`

@milroneth

This comment has been minimized.

Copy link

@milroneth milroneth commented Sep 14, 2019

Updated again to counteract YouTube's lazy loading of other elements than the video, based on original and bastiMQ's changes.

  • Reformatted
  • Increased attempt count
  • Fixed attempt delay (wasn't applied at all)
  • improved console logging.
// ==UserScript==
// @name         YouTube - Hide Live Chat
// @namespace    https://gist.github.com/LazyMammal/1c60c45e9df26602f688d025f3b20f0c
// @version      0.3
// @description  Hide live chat by default on live streams
// @author       LM, bastiMQ, IrisNebula
// @match        https://www.youtube.com/watch*
// @run-at       document-end
// @grant        none
// ==/UserScript==

function PressHideButton() {
	var el = document.getElementById("show-hide-button");
	if(el) {
		while(el.children.length > 0) {
			if(el.firstChild.getAttribute("id") == "button") {
				el.firstChild.click();
				console.log('Live Chat Hidden');
				return true;
			}
			el = el.firstChild;
		}
	}
	return false;
}

function KeepTrying(func, attempts, delayMillis) {
	console.log('Trying to Hide Live Chat, remaining attempts: ' + attempts);
	if(!func() && (attempts-1 > 0)) {
		window.setTimeout(function() {
			KeepTrying(func, attempts-1, delayMillis);
		}, delayMillis);
	}
}

(function() {
	'use strict';

	KeepTrying(PressHideButton, 12, 200);
})();

If your computer is too slow and it doesn't work, try increasing the 200 in that last line by 100 until it does.

EDIT: I just realized this version works for realtime Live chat only, but not for videos that premiered live and now have a "Live chat" replay section. Will look into it when I find the time unless someone else wants to step in and fix it instead.

@LiphiTheCat

This comment has been minimized.

Copy link

@LiphiTheCat LiphiTheCat commented Oct 25, 2020

It's one problem... It's add new chat after resize (cause there are different for desktop and mobile), sow you need to register keep trying in resize event window.addEventListener('resize', KeepTrying(PressHideButton, 12, 200));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.