YouTube: Remove Videos Based on Duration

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

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

  1. // ==UserScript==
  2. // @name YouTube: Remove Videos Based on Duration
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.2
  5. // @description Remove too short or too long videos from your YouTube's recommendation
  6. // @author Timothy (kuronek0zero)
  7. // @namespace https://github.com/kuronekozero/youtube-remove-short-long-videos/tree/main
  8. // @match https://www.youtube.com/*
  9. // @grant GM_addStyle
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. const userMinVideoDuration = 120; // Minimum duration in seconds (1 minute)
  15. // const userMaxVideoDuration = 600; // Maximum duration in seconds (10 minutes) uncomment this part if you want to also remove too long videos
  16.  
  17. function getDurationInSeconds(durationText) {
  18. const timeParts = durationText.split(":").map(Number);
  19. if (timeParts.length === 2) {
  20. return timeParts[0] * 60 + timeParts[1]; // MM:SS
  21. } else if (timeParts.length === 3) {
  22. return timeParts[0] * 3600 + timeParts[1] * 60 + timeParts[2]; // HH:MM:SS
  23. }
  24. return 0;
  25. }
  26.  
  27. function removeVideos() {
  28. const videos = document.querySelectorAll('ytd-video-renderer, ytd-grid-video-renderer, ytd-rich-item-renderer');
  29.  
  30. videos.forEach(video => {
  31. const durationElement = video.querySelector('.badge-shape-wiz__text');
  32. if (durationElement) {
  33. const durationText = durationElement.textContent.trim();
  34. const durationInSeconds = getDurationInSeconds(durationText);
  35.  
  36. if (durationInSeconds < userMinVideoDuration /* || durationInSeconds > userMaxVideoDuration */ ) {
  37. video.style.display = 'none';
  38. }
  39. }
  40. });
  41. }
  42.  
  43. // Run the script every 3 seconds to remove videos dynamically
  44. setInterval(removeVideos, 3000);
  45. })();