YouTube Maximizer

Maximizes the YouTube player in theater mode to fill the entire viewport.

// ==UserScript==
// @name         YouTube Maximizer
// @name:zh-TW   YouTube 網頁全螢幕
// @namespace    http://tampermonkey.net/
// @homepageURL  https://github.com/kevinboy666/YouTube-Maximizer
// @license     MIT
// @version      1.8
// @description  Maximizes the YouTube player in theater mode to fill the entire viewport.
// @description:zh-TW 自動將 YouTube 劇院模式最大化,並關閉 Premium 廣告。
// @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 navbarStyle = `
        transition: opacity 0.3s ease;
        opacity: 0; /* 隱藏頂部導覽 */
        pointer-events: none;
    `;


    // 定义样式表
    const videoStyleSheet = `
        #masthead-container {
            ${navbarStyle}
        }
        #page-manager {
            margin-top: 0 !important;
        }
        #full-bleed-container {
            height: 100vh !important; /* 填滿螢幕 */
            max-height: 100vh !important;
        }
        #movie_player {
            width: 100vw !important; /* 影片寬度全螢幕 */
            height: 100vh !important; /* 影片高度全螢幕 */
        }
    `;

    const liveStyleSheet = `
        #masthead-container {
            ${navbarStyle}
        }
        #page-manager {
            margin-top: 0 !important;
        }
        #full-bleed-container {
            height: 100vh !important; /* 填滿螢幕 */
            max-height: 100vh !important;
        }
        #movie_player {
            width: 70vw !important; /* 影片寬度全螢幕 */
            height: 100vh !important; /* 影片高度全螢幕 */
        }
    `;

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

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

    function removeStyles() {
        const style = document.querySelector('#custom-youtube-style');
        if (style) style.remove();
    }

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


    function updateStyles() {

        // 先處理直播聊天室情況
        if (isLive) {
            if(isChatButton){
                const player = document.querySelector('#movie_player');
                const chat = document.querySelector('#chat-container');

                if (document.querySelector('#show-hide-button[hidden]')) {
                    // 聊天室可見 → 縮小影片避免擋住
                    console.log("聊天室可見");
                    removeStyles();
                    addLiveStyles();
                    window.addEventListener('scroll', handleScroll);

                } else {
                    // 聊天室隱藏 → 恢復全寬
                    console.log("聊天室隱藏");
                    removeStyles();
                    addVideoStyles();
                    console.log("放大撥放器");
                    window.addEventListener('scroll', handleScroll);
                }
            }

        }
        else if (isWatchPage() && document.cookie.includes('wide=1')) {
            addVideoStyles();
            window.addEventListener('scroll', handleScroll);
            console.log("放大撥放器");
        } else {
            removeStyles();
            window.removeEventListener('scroll', handleScroll);
        }
    }

    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';
        }
    }

    function dismissPromo() {
        const dismissButton = document.querySelector('#dismiss-button button');
        if (dismissButton) {
            console.log('發現Premium廣告,自動關閉');
            dismissButton.click();
        }
    }

    function isLiveStream() {
        if (window.ytInitialPlayerResponse &&
            window.ytInitialPlayerResponse.videoDetails) {
            return !!window.ytInitialPlayerResponse.videoDetails.isLiveContent;
        }
        return false;
    }

    let isLive = false;
    function checkIsLiveStream() {
        if (window.ytInitialPlayerResponse) {
            isLive = isLiveStream();
            console.log("是否為直播:", isLive);
        } else {
            // 如果還沒有載入,稍後再試
            setTimeout(checkIsLiveStream, 500);
        }
    }
    checkIsLiveStream();


    let isChatButton = false;
    function checkIsChatButton() {
        if(isLive){
            if (document.querySelector('#show-hide-button')) {
                isChatButton = true;
                console.log("聊天室按鈕:", isChatButton);
            } else {
                // 如果還沒有載入,稍後再試
                setTimeout(checkIsChatButton, 500);
            }
        }
    }
    checkIsChatButton();


    const observer = new MutationObserver(() => {
        updateStyles();
        dismissPromo();
    });


    observer.observe(document.body, {
        childList: true,
        subtree: true,
        attributes: true,
        attributeFilter: ['hidden']
        //attributeFilter: ['style', 'class', 'hidden', 'display', 'visibility']
    });
    
})();