| 1 | ||
| 1 | ||
| 1 | ||
| 1 | ||
| 1 |
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