YouTube Remove button from Playlists

Adds a button to directly remove videos from the "Watch Later" playlist on YouTube

As of 2024-07-02. See the latest version.

// ==UserScript==
// @name         YouTube Remove button from Playlists
// @namespace    http://tampermonkey.net/
// @version      1.3
// @description  Adds a button to directly remove videos from the "Watch Later" playlist on YouTube
// @author       You
// @match        https://www.youtube.com/playlist?list=WL
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // Добавляем стили для кнопки и её состояния при наведении
    const style = document.createElement('style');
    style.textContent = `
        .remove-button {
            display: flex;
            align-items: center;
            border: none;
            background: transparent;
            color: #909090;
            cursor: pointer;
            margin-top: 5px;
            padding: 0;
            transition: color 0.3s;
        }
        .remove-button:hover {
            color: #ffffff;
        }
        .remove-button svg {
            width: 30px;
            height: 30px;
            fill: currentColor;
        }
    `;
    document.head.append(style);

    function addRemoveButtons() {
        document.querySelectorAll('ytd-playlist-video-renderer').forEach(video => {
            if (!video.querySelector('.remove-button')) {
                const button = document.createElement('button');
                button.classList.add('remove-button');

                // Создание значка корзины
                const trashIcon = document.createElement('div');
                trashIcon.innerHTML = `
                    <svg xmlns="http://www.w3.org/2000/svg" enable-background="new 0 0 24 24" height="24" viewBox="0 0 24 24" width="24" focusable="false">
                        <path d="M11 17H9V8h2v9zm4-9h-2v9h2V8zm4-4v1h-1v16H6V5H5V4h4V3h6v1h4zm-2 1H7v15h10V5z"></path>
                    </svg>`;

                button.appendChild(trashIcon);

                button.addEventListener('click', async () => {
                    const menuButton = video.querySelector('#button');
                    menuButton.click();

                    await new Promise(resolve => setTimeout(resolve, 100)); // Ждем, пока меню откроется

                    const menuItems = document.querySelectorAll('ytd-menu-service-item-renderer');
                    let removed = false;

                    menuItems.forEach(item => {
                        const textElement = item.querySelector('yt-formatted-string');
                        if (textElement && textElement.textContent.includes('Удалить из плейлиста "Смотреть позже"')) {
                            textElement.click();
                            removed = true;
                        }
                    });

                    if (!removed) {
                        alert('Не удалось удалить видео. Пожалуйста, попробуйте снова.');
                    }
                });

                video.querySelector('#meta').appendChild(button);
            }
        });
    }

    setInterval(addRemoveButtons, 1000); // Добавляем кнопки каждые 1000 мс
})();