YouTube Video Aspect Ratio Resizer

Resize YouTube player based on selected aspect ratio

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         YouTube Video Aspect Ratio Resizer
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Resize YouTube player based on selected aspect ratio
// @author       Your Name
// @match        https://www.youtube.com/*
// @grant        GM_addStyle
// ==/UserScript==

(function() {
    'use strict';

    // Function to set the player size based on the aspect ratio
    function setPlayerSize(aspectRatio) {
        const player = document.querySelector('.html5-video-player');
        if (player) {
            player.style.width = '100%';
            switch (aspectRatio) {
                case '21:9':
                    player.style.height = 'calc(100vw / (21/9))';
                    break;
                case '16:9':
                    player.style.height = 'calc(100vw / (16/9))';
                    break;
                case '4:3':
                    player.style.height = 'calc(100vw / (4/3))';
                    break;
                case '1:1':
                    player.style.height = '100vw';
                    break;
                case '9:16':
                    player.style.width = '100vh';
                    player.style.height = 'calc(100vh * (9/16))';
                    break;
                default:
                    break;
            }
        }
    }

    // Create a button to select aspect ratio
    function createAspectRatioButton(aspectRatio) {
        const button = document.createElement('button');
        button.innerText = aspectRatio;
        button.style.margin = '5px';
        button.style.padding = '10px';
        button.style.backgroundColor = '#ff0000';
        button.style.color = 'white';
        button.style.border = 'none';
        button.style.borderRadius = '5px';
        button.style.cursor = 'pointer';

        button.addEventListener('click', () => {
            setPlayerSize(aspectRatio);
        });

        return button;
    }

    // Create a controls container
    function createControls() {
        const controls = document.createElement('div');
        controls.style.position = 'absolute';
        controls.style.top = '10px';
        controls.style.right = '10px';
        controls.style.zIndex = '1000';
        controls.style.backgroundColor = 'rgba(0, 0, 0, 0.7)';
        controls.style.padding = '10px';
        controls.style.borderRadius = '5px';

        const aspectRatios = ['21:9', '16:9', '4:3', '1:1', '9:16'];
        aspectRatios.forEach(ratio => {
            controls.appendChild(createAspectRatioButton(ratio));
        });

        document.body.appendChild(controls);
    }

    // Create controls when the page is fully loaded
    window.addEventListener('load', () => {
        createControls();
    });
})();