YouTube: Remove Videos Based on Duration

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         YouTube: Remove Videos Based on Duration
// @namespace    http://tampermonkey.net/
// @version      0.1
// @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() {
    'use strict';

   // You can enable both max and min duration filtering by uncommenting
   // minDuration and maxDuration. Or you can uncomment only one of this lines
   // to filter videos by only one variable.
   //
   // For now only minDuration filtering is enabled.

    var minDuration = "1:00"; // Minimum duration

    // Uncomment the next line to enable Max duration filtering
    // var maxDuration = "1:00:00"; // Maximum duration

    var timeToSeconds = function(time) {
        var parts = time.split(':').map(Number);
        if (parts.length === 3) {
            return parts[0] * 3600 + parts[1] * 60 + parts[2];
        } else {
            return parts[0] * 60 + parts[1];
        }
    }


    var getVideoRows = function() {
        var contents = document.querySelector(
            'div#contents[class="style-scope ytd-rich-grid-renderer"]'
        );
        return contents.childNodes;
    }

    var processVideoRow = function(row) {
        var videos = row.querySelectorAll('ytd-rich-item-renderer');
        for (var i=0; i<videos.length; i++){
            var video = videos[i];
            var durationMatch = video.querySelector('span#text.style-scope.ytd-thumbnail-overlay-time-status-renderer');
            if (durationMatch) {
                var durationText = durationMatch.textContent.trim();
                var durationSeconds = timeToSeconds(durationText);
                if ((typeof minDuration !== 'undefined' && durationSeconds < timeToSeconds(minDuration)) ||
                    (typeof maxDuration !== 'undefined' && durationSeconds > timeToSeconds(maxDuration))) {
                    video.remove();
                }
            }
        }
    }

    var run = function() {
        var videoRows = getVideoRows();
        for (var i=0; i<videoRows.length; i++){
            processVideoRow(videoRows[i]);
        }
    }

    // without this line of code script may not work in firefox.
    setTimeout(run, 500);

    setInterval(run, 500);
})();