Greasy Fork 支持简体中文。

YouTube播放器网页全屏

YouTube 视频播放器网页全屏,仅针对剧场模式有效,请先按 t 进入剧场模式,按 n 快捷键触发(再次按 n 可取消全屏)

// ==UserScript==
// @name         YouTube播放器网页全屏
// @namespace    http://tampermonkey.net/
// @version      1.0.2
// @description  YouTube 视频播放器网页全屏,仅针对剧场模式有效,请先按 t 进入剧场模式,按 n 快捷键触发(再次按 n 可取消全屏)
// @author       meteora
// @match        https://www.youtube.com/*
// @match        https://www.youtube.com
// @icon         https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @grant        none
// @license MIT
// ==/UserScript==

;(() => {
	// 创建全屏按钮样式
	const style = document.createElement("style")
	style.textContent = `
        .webpage-fullscreen {
            position: fixed !important;
            top: 0 !important;
            left: 0 !important;
            width: 100vw !important;
            height: 100vh !important;
            max-width: none !important;
            max-height: none !important;
            z-index: 9999 !important;
            background: black;
        }
    `
	document.head.appendChild(style)

	let isWebpageFullscreen = false
	document.addEventListener("keydown", function (event) {
		// 忽略在输入框中的按键事件
		if (
			event.target.tagName === "INPUT" ||
			event.target.tagName === "TEXTAREA"
		) {
			return
		}
		// 按键盘 n 触发
		if (event.key === "n") {
			const videoContainer = document.getElementById("full-bleed-container")
			if (!videoContainer) return
			isWebpageFullscreen = !isWebpageFullscreen
			if (isWebpageFullscreen) {
				videoContainer.classList.add("webpage-fullscreen")
			} else {
				videoContainer.classList.remove("webpage-fullscreen")
			}
			// 模拟屏幕尺寸变化,让播放器大小自适应
			window.dispatchEvent(new Event("resize"))
			// 阻止默认行为
			event.preventDefault()
			// 按键盘 esc 触发
		} else if (event.key === "Escape") {
			const videoContainer = document.getElementById("full-bleed-container")
			if (!videoContainer) return
			if (isWebpageFullscreen) {
				videoContainer.classList.remove("webpage-fullscreen")
				isWebpageFullscreen = false
				// 模拟屏幕尺寸变化,让播放器大小自适应
				window.dispatchEvent(new Event("resize"))
				// 阻止默认行为
				event.preventDefault()
			}
		}
	})
})()