Hide Watched YouTube Videos from Recommendations

Hide YouTube videos you've already watched from the recommendations

当前为 2024-10-10 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Hide Watched YouTube Videos from Recommendations
// @namespace    https://tampermonkey.net/
// @version      2.1
// @description  Hide YouTube videos you've already watched from the recommendations
// @author       Nyxia
// @match        *://www.youtube.com/*
// @license      MIT
// @grant        GM_addStyle
// @grant        GM_log
// @grant        GM_openInTab
// ==/UserScript==

(function() {
    'use strict';

    // Function to hide watched videos
    const hideWatchedVideos = () => {
        // Select all video items in the recommendation sections
        const videoItems = document.querySelectorAll('ytd-grid-video-renderer, ytd-compact-video-renderer, ytd-video-renderer');

        videoItems.forEach(video => {
            try {
                // Check if the video has a "watched" label (e.g., 'progress-bar' element indicating it's been watched)
                const progressBar = video.querySelector('.ytd-thumbnail-overlay-resume-playback-renderer');

                // If the video has been partially or fully watched, hide it
                if (progressBar) {
                    video.style.display = 'none';  // Hide the watched video
                    GM_log('Hiding watched video:', video);
                }
            } catch (error) {
                // Log any potential errors for debugging
                console.error('Error hiding watched video:', error);
            }
        });
    };

    // MutationObserver to watch for page updates (e.g., infinite scroll loading more videos)
    const observer = new MutationObserver((mutations) => {
        mutations.forEach((mutation) => {
            hideWatchedVideos();  // Reapply the function every time the page updates
        });
    });

    // Start observing the document for changes in child elements (like new video recommendations)
    observer.observe(document, {
        childList: true,
        subtree: true
    });

    // Initial call to hide videos when the script is first loaded
    hideWatchedVideos();
})();