Greasy Fork 还支持 简体中文。

Disable Youtube PiP Miniplayer

Prevents Youtube from keep playing videos in PiP/miniplayer

您需要先安裝使用者腳本管理器擴展,如 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      1.0
// @author       xFIRKx
// @match        http://*.youtube.com/*
// @match        https://*.youtube.com/*
// @homepageURL  https://greasyfork.org/it/scripts/493793-disable-youtube-pip-miniplayer
// @grant        none
// ==/UserScript==

(function() {
    document.body.addEventListener("yt-navigate-finish", function(event) {
        if (document.getElementsByTagName('ytd-miniplayer').length) {
            document.querySelector('ytd-miniplayer').parentNode.removeChild(document.querySelector('ytd-miniplayer'));
        }
        if (document.getElementsByClassName('ytp-miniplayer-button').length) {
            document.querySelector('.ytp-miniplayer-button').parentNode.removeChild(document.querySelector('.ytp-miniplayer-button'))
        }
        if (window.location.pathname != "/watch") {
            document.querySelector('#movie_player video').parentNode.removeChild(document.querySelector('#movie_player video'));
        }
    });
})();
(() => {
    'use strict'
    // credit: https://stackoverflow.com/a/46428962
    let oldHref = document.location.href
    window.onload = () => {
        let bodyList = document.querySelector('body')
        let observer = new MutationObserver(ms => {
            ms.forEach(_m => {
                if (oldHref != document.location.href) {
                    oldHref = document.location.href
                    // allow some delay for page to load.
                    setTimeout(() => {
                        const jq = $('#movie_player > div.ytp-miniplayer-ui > div > button.ytp-miniplayer-close-button.ytp-button')
                        if (jq.length && !document.location.href.startsWith('https://www.youtube.com/watch?')) {
                            jq.click()
                            console.log('[AutoCloseYoutubeMiniplayer] miniplayer dismissed')
                        }}, 200)
                }
            })
        })
        observer.observe(bodyList, {childList: true, subtree: true})
    }
})()
// Function to disable play/pause keyboard shortcut within the miniplayer
function disableMiniplayerShortcut(event) {
    let miniplayer = document.querySelector('ytd-miniplayer');
    if (miniplayer && miniplayer.contains(event.target)) {
        if (event.code === 'Space' || event.code === 'ArrowRight' || event.code === 'ArrowLeft') {
            event.stopImmediatePropagation(); // Stop event propagation
            event.preventDefault(); // Prevent default keyboard shortcut action
            console.log('Keyboard shortcut disabled for miniplayer.');
        }
    }
}

// Add event listener for keyboard shortcut within the miniplayer
document.addEventListener('keydown', disableMiniplayerShortcut, true); // Use capture phase for early interception


// === PATCH START ===
(function() {
    function handleMiniplayer() {
        const mini = document.querySelector('ytd-miniplayer');
        if (!mini) return;

        if (location.pathname === '/' || location.pathname === '/feed/library') {
            // On homepage or library: hide miniplayer to avoid breaking buttons
            mini.style.display = 'none';
            console.log('[Patch] Miniplayer hidden on homepage/library');
        } else {
            // On other pages, remove it
            mini.remove();
            console.log('[Patch] Miniplayer removed on non-homepage');
        }
    }

    window.addEventListener('load', () => {
        setTimeout(handleMiniplayer, 500);
    });

    document.body.addEventListener('yt-navigate-finish', () => {
        setTimeout(handleMiniplayer, 500);
    });
})();

// === PATCH END ===