YouTube - Hide chat by default

Hide chat on YouTube live videos by default.

目前為 2024-09-09 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

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

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

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

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

	let update = () => {
		// 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);