MatrixDevFunnyVideosMusicBooksProjectsAncapsTechEconomicsPrivacyGIFSCringeAnarchyFilmPicsThemesIdeas4MatrixAskMatrixHelpTop Subs
4

This script makes it so only videos under 20 minutes in length apear. This increases the odds the creator wrote a tight script. It also reduces the number of videos that are just watch time baits.

TBH, 20 minutes was the magic number. But I've been seeing more more low content to time videos get created under 20 minutes. So this isn't the silver bullet it once was. But it helps.

You might notice in the code that there are two arrays at the top. TIME_NODES and CARD_NODES, with a few selectors in them. This is because youtube changes their markup a couple times a year. And they also AB test so the markup can even change back and forth. So if it breaks, you just have to find a selector to add to one of those arrays. Then it will stay backwards compatable if they change it back.

// ==UserScript==
// @name        YouTube: Hide 20+ Minute Videos
// @version     1.0.0
// @description Auto-hides YouTube videos longer than 20 minutes.  This reduces distraction from long winded clickbait, fixes pacing, and saves time.
// @match       https://www.youtube.com/*
// @run-at      document-start
// @grant       none
// ==/UserScript==
 var exports = {};
 window.youtubehide = exports;
 var LIMIT = 20*60;
 var TIME_NODES = [
  '.yt-badge-shape__text',
  'ytd-thumbnail-overlay-time-status-renderer span',
  'span.ytd-thumbnail-overlay-time-status-renderer',
  '.badge-shape-wiz__text',
  '.ytBadgeShapeText',
 ].join(',');
 var CARD_NODES = [
  '.ytd-item-section-renderer',
  'yt-lockup-view-model',
  'ytd-rich-item-renderer',
  'ytd-video-renderer',
  'ytd-compact-video-renderer',
  'ytd-grid-video-renderer',
  'ytd-playlist-video-renderer',
  'ytd-playlist-panel-video-renderer'
 ].join(',');
 exports.addStyle = addStyle;
 function addStyle(){
  if (document.getElementById('yt-hide-long-style')) return;
  var s = document.createElement('style');
  s.id = 'yt-hide-long-style';
  s.textContent = '.yt-hide-long{display:none!important}';
  document.documentElement.appendChild(s);
 }
 exports.parseSeconds = parseSeconds;
 function parseSeconds(txt){
  if (!txt || txt.indexOf(':')<0) return 0;
  txt = txt.trim().replace(/[^\d:]/g,'');
  var parts = txt.split(':');
  var sec = 0;
  for (var i=0;i<parts.length;i++) sec = sec*60 + (parseInt(parts[i],10)||0);
  return sec;
 }
 exports.scan = scan;
 function scan(root=document){
  var out = [];
  var els = root.querySelectorAll(TIME_NODES);
  for (var i=0;i<els.length;i++){
   var t = (els[i].textContent||'').trim();
   if (t.indexOf(':')<0) continue;
   var s = parseSeconds(t);
   if (s >= LIMIT){
    console.log('Bad',els[i]);
    var card = els[i].closest(CARD_NODES);
    if (card) {
     card.classList.add('yt-hide-long');
     out.push(card);
    }
   }
  }
  return out;
 }
 exports.start = start;
 function start(){
  if (!document.body) return setTimeout(start,50);
  addStyle();
  scan(document);
  var mo = new MutationObserver(function(muts){
   for (var i=0;i<muts.length;i++){
    var a = muts[i].addedNodes;
    for (var j=0;j<a.length;j++){
     var n = a[j];
     if (n && n.nodeType===1) scan(n);
    }
   }
  });
  mo.observe(document.body,{subtree:true,childList:true});
 }

 start();

function toadd__edit__wasadded() {
    document.querySelectorAll('.ytd-promoted-video-renderer').forEach(i=>i.closest('#contents').remove())
    document.querySelectorAll('ytd-in-feed-ad-layout-renderer').forEach(i=>i.remove())
}

(function(){
 var KILL = '.ytd-promoted-video-renderer, ytd-in-feed-ad-layout-renderer';
 function nuke(root){
  var els = root.querySelectorAll(KILL);
  for (var i=0;i<els.length;i++){
   var e = els[i];
   if (e.classList && e.classList.contains('ytd-promoted-video-renderer')){
    var p = e.closest('#contents');
    if (p) p.remove(); else e.remove();
   } else {
    e.remove();
   }
  }
 }
 function begin(){
  if (!document.body) return setTimeout(begin,50);
  nuke(document);
  var mo = new MutationObserver(function(m){
   for (var i=0;i<m.length;i++){
    var a = m[i].addedNodes;
    for (var j=0;j<a.length;j++){
     var n = a[j];
     if (n && n.nodeType===1) nuke(n);
    }
   }
  });
  mo.observe(document.body,{subtree:true,childList:true});
 }
 begin();
})();

Comment preview

[-]JasonCarswell3(+3|0)

Hiding long videos for why?
Hiding them on YouTube?
Or hiding them on Matrix?

I listen to a lot of long videos. Most are simply not good. I only share those that are great with rich discussions.

On the other end, I find a lot of 20-30 minute videos could be half the length. And now more than ever there are long-winded AI slop generators timesucking with what could have been good content.

[-]x0x71(+1|0)

Yep. I'm mostly trying to dodge those 30-minute videos that should be 15. It's all YouTube recommends to me. It's not worth getting grabbed by a title only to realize half a second after I've gotten interested, I'm going to regret watching that.

I also want to keep moving on things. I'm looking for a short break once or twice a day, not a real time commitment.

[-]newJiminy0(0|0)

You can already change the search filters on YouTube for things like video length

[-]x0x70(0|0)

Interesting. I'm mostly trying to get this to apply to sidebar suggestions.

[-]newJiminy0(0|0)

What sidebar suggestions? Does this make it so people can't share 20+ min youtube videos on this site?