YouTube Maximizer

Maximizes the YouTube player to fill the entire browser viewport when in theater mode

// ==UserScript==
// @name         YouTube Maximizer
// @namespace    http://tampermonkey.net/
// @license     MIT
// @version      1.3
// @description  Maximizes the YouTube player to fill the entire browser viewport when in theater mode
// @author       sharlxeniy <[email protected]>
// @match        https://www.youtube.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @grant        none
// ==/UserScript==
(function() {
    'use strict';

    // 定义样式表
    const styleSheet = `
        #masthead-container {
            transition: opacity 0.3s ease;
            opacity: 0; /* 初始隐藏顶部导航栏 */
            pointer-events: none;
        }
        #page-manager {
            margin-top: 0 !important;
        }
        #full-bleed-container {
            height: 100vh !important; /* 填满屏幕 */
            max-height: 100vh !important;
        }
        #movie_player {
            width: 100vw !important; /* 视频宽度为全屏 */
            height: 100vh !important; /* 视频高度为全屏 */
        }
    `;

    function addStyles() {
        if (!document.querySelector('#custom-youtube-style')) {
            const style = document.createElement('style');
            style.id = 'custom-youtube-style';
            style.textContent = styleSheet;
            document.head.appendChild(style);
        }
    }

    function isWatchPage() {
        return location.pathname === '/watch';
    }

    function updateStyles() {
        if (document.cookie.includes('wide=1')) {
            addStyles();
        } else {
            const style = document.querySelector('#custom-youtube-style');
            if (style) style.remove();
        }
    }

    function dismissPromo() {
        const dismissButton = document.querySelector('#dismiss-button button');
        if (dismissButton) {
            console.log('发现广告,自动关闭');
            dismissButton.click();
        }
    }

    function handleScroll() {
        const navbar = document.querySelector('#masthead-container');
        if (!navbar) return;

        if (window.scrollY > 0) {
            navbar.style.opacity = '1';
            navbar.style.pointerEvents = 'auto';
        } else {
            navbar.style.opacity = '0';
            navbar.style.pointerEvents = 'none';
        }
    }

    if (isWatchPage()) {
        updateStyles();
        dismissPromo();
        const observer = new MutationObserver(() => {
            updateStyles();
            dismissPromo();
        });
        observer.observe(document.body, { childList: true, subtree: true });
        window.addEventListener('scroll', handleScroll);
    }
})();