Skip to content

Instantly share code, notes, and snippets.

@LazyMammal
Created September 20, 2017 11:54
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LazyMammal/1c60c45e9df26602f688d025f3b20f0c to your computer and use it in GitHub Desktop.
Save LazyMammal/1c60c45e9df26602f688d025f3b20f0c to your computer and use it in GitHub Desktop.
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 );
})();
@lbmaian
Copy link

lbmaian commented Mar 7, 2021

Based off @milroneth's version with following improvements:

  • Avoid opening live chat if it's closed by default (premiered video pages).
  • Works when navigating to YouTube watch page from another YouTube page, which involves AJAX rather than new page load, via polling for URL changes and watching all non-embed YouTube pages.
// ==UserScript==
// @name         YouTube - Hide Live Chat
// @namespace    https://gist.github.com/LazyMammal/1c60c45e9df26602f688d025f3b20f0c#gistcomment-3656586
// @version      0.4
// @description  Hide live chat by default on live streams
// @author       LM, bastiMQ, IrisNebula, lbmaian
// @match        https://www.youtube.com/*
// @exclude      https://www.youtube.com/embed/*
// @run-at       document-end
// @grant        none
// @icon         https://www.youtube.com/favicon.ico
// ==/UserScript==

function log(str) {
	console.log('[YouTube - Hide Live Chat] ' + str);
}

function pressHideButton() {
	var el = document.getElementById('chat');
	if(el) {
		if(el.hasAttribute('collapsed')) {
			log('Live Chat already hidden');
			return true;
		} else {
			el = document.getElementById('show-hide-button');
			if(el) {
				while(el.children.length > 0) {
					if(el.firstChild.getAttribute('id') == 'button') {
						el.firstChild.click();
						log('Live Chat hidden');
						return true;
					}
					el = el.firstChild;
				}
			}
		}
	}
	return false;
}

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

function checkForWatchPage(url) {
	if(url.startsWith('https://www.youtube.com/watch')) {
		log('Matched ' + url);
		keepTrying(pressHideButton, 12, 200);
	}
}

(function() {
	'use strict';

	// Navigating to YouTube watch page can happen via AJAX rather than new page load, so keep polling for URL changes.
	// This also requires watching all YouTube pages, rather than just watching https://www.youtube.com/watch*
	var curURL = document.URL;
	window.setInterval(function() {
		var newURL = document.URL;
		if(curURL != newURL) {
			curURL = newURL;
			checkForWatchPage(curURL);
		}
	}, 500);

	checkForWatchPage(curURL);
})();

@Azmaeus
Copy link

Azmaeus commented Jul 27, 2021

I'd love to see this as a repo so updates can be automatically received via a userscript manager.

@lbmaian
Copy link

lbmaian commented Feb 8, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment