Youtube button to delete a video from a playlist

Добавляет кнопку для непосредственного удаления видеороликов из плейлиста "Смотреть позже" на YouTube

目前為 2024-07-05 提交的版本,檢視 最新版本

// ==UserScript==
// @name      Youtube button to delete a video from a playlist
// @name:en   Youtube button to delete a video from a playlist
// @namespace    http://tampermonkey.net/
// @version      1.53
// @description:en  Adds a button to directly remove videos from the "Watch Later" playlist on YouTube
// @description  Добавляет кнопку для непосредственного удаления видеороликов из плейлиста "Смотреть позже" на YouTube
// @author       You
// @match        https://www.youtube.com/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    const playlistUrlPattern = /\/playlist\?list=WL/;

    // Добавляем стили для кнопки и её состояния при наведении
    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 addRemoveButton(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, 500)); // Увеличиваем время ожидания до 500 мс

                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);
        }
    }

    function init() {
        if (!playlistUrlPattern.test(window.location.href)) {
            return;
        }

        const videoContainers = document.querySelectorAll('ytd-playlist-video-renderer');
        videoContainers.forEach(addRemoveButton);

        const observer = new MutationObserver(mutations => {
            mutations.forEach(mutation => {
                mutation.addedNodes.forEach(node => {
                    if (node.nodeType === 1 && node.matches('ytd-playlist-video-renderer')) {
                        addRemoveButton(node);
                    }
                });
            });
        });

        observer.observe(document.body, {
            childList: true,
            subtree: true
        });
    }

    // Инициализация при изменении данных на YouTube
    window.addEventListener('yt-page-data-updated', () => {
        setTimeout(init, 1000); // Увеличиваем время ожидания после обновления страницы до 1000 мс
    });

    // Инициализация при изменении URL (для Single Page Application)
    let lastUrl = location.href;
    new MutationObserver(() => {
        const url = location.href;
        if (url !== lastUrl) {
            lastUrl = url;
            setTimeout(init, 1000); // Увеличиваем время ожидания после изменения URL до 1000 мс
        }
    }).observe(document, { subtree: true, childList: true });

    init(); // начальная инициализация
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址