YouTube Caption Window Resizer

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==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();
    }
)();