YouTube: Remove Videos Based on Duration

Remove too short or too long videos from your YouTube's recommendation

目前為 2024-10-21 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         YouTube: Remove Videos Based on Duration
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  Remove too short or too long videos from your YouTube's recommendation
// @author       Timothy (kuronek0zero)
// @namespace    https://github.com/kuronekozero/youtube-remove-short-long-videos/tree/main
// @match        https://www.youtube.com/*
// @grant        GM_addStyle
// @license      MIT
// ==/UserScript==

(function() {
    const userMinVideoDuration = 120; // Minimum duration in seconds (1 minute)
    // const userMaxVideoDuration = 600; // Maximum duration in seconds (10 minutes) uncomment this part if you want to also remove too long videos

    function getDurationInSeconds(durationText) {
        const timeParts = durationText.split(":").map(Number);
        if (timeParts.length === 2) {
            return timeParts[0] * 60 + timeParts[1]; // MM:SS
        } else if (timeParts.length === 3) {
            return timeParts[0] * 3600 + timeParts[1] * 60 + timeParts[2]; // HH:MM:SS
        }
        return 0;
    }

    function removeVideos() {
        const videos = document.querySelectorAll('ytd-video-renderer, ytd-grid-video-renderer, ytd-rich-item-renderer');

        videos.forEach(video => {
            const durationElement = video.querySelector('.badge-shape-wiz__text');
            if (durationElement) {
                const durationText = durationElement.textContent.trim();
                const durationInSeconds = getDurationInSeconds(durationText);

                if (durationInSeconds < userMinVideoDuration /* || durationInSeconds > userMaxVideoDuration */ ) {
                    video.style.display = 'none';
                }
            }
        });
    }

    // Run the script every 3 seconds to remove videos dynamically
    setInterval(removeVideos, 3000);
})();