Hide Youtube Player When Paused

Automatically hide the player when video is paused and mouse not hovering, as if the video is playing

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Hide Youtube Player When Paused
// @namespace    PlanetXX2
// @version      2024-11-13.1
// @description  Automatically hide the player when video is paused and mouse not hovering, as if the video is playing
// @license      MIT
// @author       PlanetXX2
// @match        https://*.youtube.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    let player = null;
    let playerParent = null;
    let isListening = false;
    let waitForReadyInterval = null;
    let lastState = "paused"; // paused, playing

    const eventListenerMouseOver = () => {
        setTimeout(() => {
            player.classList.remove("ytp-autohide");
        }, 50);
    }
    const eventListenerMouseLeave = () => {
        setTimeout(() => {
            player.classList.add("ytp-autohide");
        }, 50);
    }

    function playerStateChanged() {
        if (player.classList.contains("paused-mode") && !isListening) {
            isListening = true;
            playerParent.addEventListener("mouseover", eventListenerMouseOver);
            playerParent.addEventListener("mouseleave", eventListenerMouseLeave);
        } else if (isListening) {
            isListening = false;
            playerParent.removeEventListener("mouseover", eventListenerMouseOver);
            playerParent.removeEventListener("mouseleave", eventListenerMouseLeave);
        }
    }

    function playerReady() {
        let ob = new MutationObserver(() => {
            if (player.classList.contains("paused-mode")) {
                if (lastState === "playing") {
                    lastState = "paused";
                    playerStateChanged();
                }
            } else {
                if (lastState === "paused") {
                    lastState = "playing";
                    playerStateChanged();
                }
            }
        });
        ob.observe(player, {
            attributes: true,
            attributeFilter: ["class"]
        });
    }

    // Wait for the player to be ready
    function waitForReady() {
        waitForReadyInterval = setInterval(function () {
            player = document.getElementById("movie_player");
            if (player) {
                console.log(player)
                playerParent = player.parentElement;

                clearInterval(waitForReadyInterval);
                playerReady();
                console.log("Player ready");
            }
        }, 500);
    }

    // reset the state when page navigate
    window.navigation.addEventListener("navigate", (e) => {
        console.log("BBB",e)
        console.log("AAA", window.location.pathname);
        playerParent && playerParent.removeEventListener("mouseover", eventListenerMouseOver);
        playerParent && playerParent.removeEventListener("mouseleave", eventListenerMouseLeave);
        player = null;
        playerParent = null;
        isListening = false;
        lastState = "paused";
        clearInterval(waitForReadyInterval)

        if(e.destination.url.includes("/watch") || e.destination.url.includes("/live")){
            waitForReady();
        }
    });

    if(window.location.pathname.includes("/watch") || window.location.pathname.includes("/live")){
        waitForReady();
    }

})();