YouTube - Hide chats by default

Hide chats on YouTube live videos by default.

2024-09-09 기준 버전입니다. 최신 버전을 확인하세요.

이 스크립트를 설치하려면 Tampermonkey, Greasemonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

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

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Userscripts와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 유저 스크립트 관리자 확장 프로그램이 필요합니다.

(이미 유저 스크립트 관리자가 설치되어 있습니다. 설치를 진행합니다!)

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

(이미 유저 스타일 관리자가 설치되어 있습니다. 설치를 진행합니다!)

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