YouTube - Hide chats by default

Hide chats on YouTube live videos by default.

09.09.2024 itibariyledir. En son verisyonu görün.

Bu betiği kurabilmeniz için Tampermonkey, Greasemonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

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

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Userscripts gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

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

Bu komut dosyasını yüklemek için bir kullanıcı komut dosyası yöneticisi uzantısı yüklemeniz gerekecek.

(Zaten bir kullanıcı komut dosyası yöneticim var, kurmama izin verin!)

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.

(Zateb bir user-style yöneticim var, yükleyeyim!)

// ==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);