YouTube - Hide chats by default

Hide chats on YouTube live videos by default.

As of 2024-09-09. See the latest version.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

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

(I already have a user script manager, let me install it!)

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.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         YouTube - Hide chats by default
// @namespace    amekusa.yt-hide-chats
// @author       amekusa
// @version      1.0.1
// @description  Hide chats on YouTube live videos by default.
// @match        https://www.youtube.com/watch?*
// @run-at       document-start
// @grant        none
// @license      MIT
// @homepage     https://github.com/amekusa/monkeyscripts
// ==/UserScript==

(function (doc) {
	// --- config ---
	let interval = 1000;
	let debug = false ? console.debug : (() => {});
	// --------------

	let states = {
		FIND:   1,
		ENSURE: 2,
		DONE:   3,
	};
	let state = states.FIND;
	let url = window.location.href;

	let setState = x => {
		state = states[x];
		debug('state:', x);
	};

	let find = () => {
		try {
			let frame = doc.querySelector('iframe#chatframe');
			if (!frame) return null;
			if (!frame.contentWindow.document.querySelector('yt-live-chat-renderer #contents')) return null;
			return frame.contentWindow.document.querySelector('yt-live-chat-renderer #close-button button');
		} catch (e) {
			return e;
		}
	};

	let update = () => {
		let next;
		switch (state) {
		case states.FIND: // mission: find & click the close button
			var btn = find();
			if (btn) {
				if (btn instanceof Error) {
					setState('DONE');
				} else {
					debug('click:', btn);
					btn.dispatchEvent(new Event('click'));
					setState('ENSURE');
				}
			}
			return;
		case states.ENSURE: // mission: ensure the close button does not exist anymore
			var btn = find();
			if (btn) {
				setState('FIND'); // the button is stil there. do it again
				update();
			} else setState('DONE'); // success
			return;
		case states.DONE: // mission: detect url changes
			if (url != window.location.href) {
				url = window.location.href;
				debug('url changed:', url);
				setState('FIND');
			}
			return;
		default:
			console.error('Invalid State');
		}
	};

	doc.addEventListener('DOMContentLoaded', () => {
		setInterval(update, interval);
	});

})(document);