YouTube - Hide chat by default

Hide chat on YouTube live videos by default.

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

이 스크립트를 설치하려면 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 chat by default
// @namespace    amekusa.yt-hide-chat
// @author       amekusa
// @version      1.2.1
// @description  Hide chat on YouTube live videos by default.
// @match        https://www.youtube.com/*
// @run-at       document-start
// @grant        none
// @license      MIT
// @homepage     https://github.com/amekusa/monkeyscripts
// ==/UserScript==

(function (doc) {
	// --- config ---
	let wait = 4000; // initial wait time (ms)
	let interval = 2000; // update interval (ms)
	let match = /^https:\/\/www\.youtube\.com\/(?:watch\?|clip\/)/; // url pattern
	let debug = false ? console.debug : (() => {});
	// --------------

	let watcher;
	let hide;
	let url;

	function findCloseButton() {
		try {
			let el = doc.querySelector('iframe#chatframe');
			if (!el) return null;
			el = el.contentWindow.document;
			if (!el.querySelector('yt-live-chat-item-list-renderer #items [id], yt-live-chat-item-list-renderer #empty-state-message')) return null;
			return el.querySelector('yt-live-chat-header-renderer #close-button button');
		} catch (e) {
			return null;
		}
	}

	function unhide() {
		debug('unhide.');
		hide = false;
	}

	function update() {
		if (!window.location.href.match(match)) return; // do nothing

		// detect url change
		if (url != window.location.href) {
			debug('url changed:', window.location.href);
			return init();
		}

		if (!hide) return;

		// find & click the close button
		let btn = findCloseButton();
		if (btn) {
			debug('click:', btn);
			return btn.dispatchEvent(new Event('click'));
		}

		// hook "Show chat" button
		btn = doc.querySelector('#chat-container #show-hide-button button');
		if (btn) {
			btn.removeEventListener('click', unhide);
			btn.addEventListener('click', unhide);
		}
	}

	function init() {
		if (watcher) clearInterval(watcher);
		debug('initializing...');
		hide = true;
		url = window.location.href;
		setTimeout(() => {
			watcher = setInterval(update, interval);
			debug('initialized.');
			update();
		}, wait);
	}

	doc.addEventListener('DOMContentLoaded', init);

})(document);