Disable Youtube PiP Miniplayer

Prevents Youtube from keep playing videos in PiP/miniplayer

目前為 2024-04-30 提交的版本,檢視 最新版本

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

You will need to install an extension such as Tampermonkey to install this script.

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Disable Youtube PiP Miniplayer
// @namespace    disableMPYTxFIRKx
// @description  Prevents Youtube from keep playing videos in PiP/miniplayer
// @version      0.7
// @author       xFIRKx
// @match        http://*.youtube.com/*
// @match        https://*.youtube.com/*
// @grant        none
// ==/UserScript==

(function() {
    // Remove miniplayer and its button when page navigation finishes
    document.body.addEventListener("yt-navigate-finish", function(event) {
        removeMiniplayer();
    });

    function removeMiniplayer() {
        let miniplayer = document.querySelector('ytd-miniplayer');
        if (miniplayer) {
            miniplayer.parentNode.removeChild(miniplayer);
        }

        let miniplayerButton = document.querySelector('.ytp-miniplayer-button');
        if (miniplayerButton) {
            miniplayerButton.parentNode.removeChild(miniplayerButton);
        }
    }

    // Pause and reset video/audio element when navigating to the miniplayer
    window.onload = () => {
        let bodyList = document.querySelector('body');
        let observer = new MutationObserver(mutationsList => {
            mutationsList.forEach(mutation => {
                if (mutation.target.id === 'player-theater-container' && mutation.type === 'childList') {
                    // Pause and reset video/audio element
                    let videoElement = document.querySelector('#movie_player video');
                    if (videoElement) {
                        videoElement.pause();
                        videoElement.currentTime = 0;
                    }

                    let audioElement = document.querySelector('audio');
                    if (audioElement) {
                        audioElement.pause();
                        audioElement.currentTime = 0;
                    }
                }
            });
        });
        observer.observe(bodyList, { childList: true, subtree: true });
    };

    // Disable miniplayer play/pause button click functionality
    document.addEventListener('click', function(event) {
        if (event.target.closest('.ytp-miniplayer-button')) {
            event.preventDefault(); // Prevent default click action
            event.stopPropagation(); // Stop event propagation
            console.log('Play/Pause functionality disabled on miniplayer');
        }
    }, true);

    // Disable miniplayer play/pause keyboard shortcut functionality
    document.addEventListener('keydown', function(event) {
        let miniplayer = document.querySelector('ytd-miniplayer');
        if (miniplayer && (event.code === 'Space' || event.code === 'ArrowRight' || event.code === 'ArrowLeft')) {
            event.preventDefault(); // Prevent default keyboard shortcut action
            event.stopPropagation(); // Stop event propagation
            console.log('Keyboard shortcut disabled for miniplayer');
        }
    });

})();