YouTube Caption Window Resizer

Sets YouTube caption window width to 100%, unsets left property, and centers text.

Du musst eine Erweiterung wie Tampermonkey, Greasemonkey oder Violentmonkey installieren, um dieses Skript zu installieren.

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

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

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

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

Sie müssten eine Skript Manager Erweiterung installieren damit sie dieses Skript installieren können

(Ich habe schon ein Skript Manager, Lass mich es installieren!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         YouTube Caption Window Resizer
// @namespace    http://tampermonkey.net/
// @version      0.1
// @license      MIT
// @description  Sets YouTube caption window width to 100%, unsets left property, and centers text.
// @author       Your Name
// @match        *://www.youtube.com/*
// @grant        none
// ==/UserScript==

(
    function()
    {
        'use strict';

        const applyCaptionStyles = () =>
        {
            const captionWindow = document.querySelector('.caption-window.ytp-caption-window-bottom.ytp-caption-window-rollup');
            if (captionWindow)
            {
                captionWindow.style.width = '100%';
                captionWindow.style.left = ''; // Unsets the left property
                captionWindow.style.textAlign = 'center';
                console.log('Caption window styles applied.');
            }
        };

        // Use a MutationObserver to detect when the caption window is added to the DOM
        const observer = new MutationObserver
        (
            (mutationsList) =>
            {
                for (const mutation of mutationsList)
                {
                    if (mutation.type === 'childList' && mutation.addedNodes.length > 0)
                    {
                        // Check if the added node is the caption window or contains it
                        if( mutation.addedNodes[0].classList && mutation.addedNodes[0].classList.contains('caption-window') || mutation.addedNodes[0].querySelector('.caption-window.ytp-caption-window-bottom.ytp-caption-window-rollup'))
                        {
                            applyCaptionStyles();
                        }
                    }
                    else if (mutation.type === 'attributes' && mutation.target.classList.contains('caption-window'))
                    {
                        // Reapply styles if caption window attributes change (e.g., visibility)
                        applyCaptionStyles();
                    }
                }
            }
        );

        // Start observing the body for changes
        observer.observe
        (
            document.body,
            {
                childList: true,
                subtree: true,
                attributes: true,
                attributeFilter: ['style', 'class'] // Observe changes to style and class attributes
            }
        );

        // Also apply styles on initial load, in case the caption window is already present
        applyCaptionStyles();
    }
)();