YouTube - Hide chat by default

Hide chat on YouTube live videos by default.

Stan na 10-09-2024. Zobacz najnowsza wersja.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

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.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name         YouTube - Hide chat by default
// @namespace    amekusa.yt-hide-chat
// @author       amekusa
// @version      1.1.3
// @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 interval = 1500;
	let debug = false ? console.debug : (() => {});
	// --------------

	let hide = true;
	let url = window.location.href;

	let find = () => {
		try {
			let frame = doc.querySelector('iframe#chatframe');
			if (!frame) return null;
			frame = frame.contentWindow.document;
			if (!frame.querySelector('yt-live-chat-item-list-renderer #items')) return null; // wait actual messages show up before close
			return frame.querySelector('yt-live-chat-header-renderer #close-button button');
		} catch (e) {
			return null;
		}
	};

	let unhide = () => {
		debug('unhide');
		hide = false;
	};

	let update = () => {
		// only work with a video page
		if (!window.location.href.startsWith('https://www.youtube.com/watch?')) return;

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

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

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

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

})(document);