YouTube - Limit Subtitle Languages

Restrict YouTube subtitle language menu to only the target language.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         YouTube - Limit Subtitle Languages
// @description  Restrict YouTube subtitle language menu to only the target language.
// @version      0.2
// @author       to
// @namespace    https://github.com/to
// @license      MIT
//
//
// @noframes
// @match        https://www.youtube.com/watch*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// ==/UserScript==

(function () {
	// チェック対象の言語
	const TARGET_LANGUAGE = "日本語";

	// プレーヤーを監視する
	function observePlayer() {
		// 変更を監視する
		const player = document.querySelector('.html5-video-player');
		const observer = new MutationObserver(() => {
			// 翻訳言語メニューか?
			const menu = document.querySelector('.ytp-panel-menu');
			if (!menu || !Array.from(menu.childNodes).some(menuItem =>
				menuItem.textContent.trim() == TARGET_LANGUAGE))
				return;

			// 対象言語以外の言語を削除
			Array.from(menu.childNodes).forEach(menuItem => {
				if (!(menuItem.textContent.trim() == TARGET_LANGUAGE))
					menuItem.remove();
			});
		});
		observer.observe(player, {
			childList: true,
			subtree: true,
		});
	}

	// プレーヤーが見つかるのを待つ
	const intervalId = setInterval(() => {
		const player = document.querySelector('.html5-video-player');
		if (player) {
			clearInterval(intervalId);
			observePlayer();
		}
	}, 500);
})();