Hide low viewcount vids

Hides low view count recommended videos on YouTube

Bu betiği kurabilmeniz için Tampermonkey, Greasemonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği yüklemek için Tampermonkey gibi bir uzantı yüklemeniz gerekir.

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Userscripts gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği indirebilmeniz için ayrıca Tampermonkey gibi bir eklenti kurmanız gerekmektedir.

Bu komut dosyasını yüklemek için bir kullanıcı komut dosyası yöneticisi uzantısı yüklemeniz gerekecek.

(Zaten bir kullanıcı komut dosyası yöneticim var, kurmama izin verin!)

Bu stili yüklemek için Stylus gibi bir uzantı yüklemeniz gerekir.

Bu stili yüklemek için Stylus gibi bir uzantı kurmanız gerekir.

Bu stili yükleyebilmek için Stylus gibi bir uzantı yüklemeniz gerekir.

Bu stili yüklemek için bir kullanıcı stili yöneticisi uzantısı yüklemeniz gerekir.

Bu stili yüklemek için bir kullanıcı stili yöneticisi uzantısı kurmanız gerekir.

Bu stili yükleyebilmek için bir kullanıcı stili yöneticisi uzantısı yüklemeniz gerekir.

(Zateb bir user-style yöneticim var, yükleyeyim!)

// ==UserScript==
// @name        Hide low viewcount vids
// @description Hides low view count recommended videos on YouTube
// @match       https://www.youtube.com/*
// @version     1.1.4
// @author      Laku
// @namespace   https://greasyfork.org/users/1387751
// ==/UserScript==

const threshold = 100;

function hide(v) {
  const e = v.querySelector('span.inline-metadata-item.style-scope.ytd-video-meta-block');
  if(!e) return;
  const cap = /(?:No|(\d+\.?\d{0,2})([KMB])?) views?/.exec(e.textContent);
  if(!cap) return;
  const count = parseFloat(cap[1]??0) * (({K:1e3,M:1e6,B:1e9})[cap[2]]??1);
  if(count < threshold) {
    v.style.display = "none";
    //console.log(count);
  }
}

function hide_endscreen(v) {
  const e = v.querySelector(".ytp-videowall-still-info-author");
  const split = e.textContent.split("•");
  if(split.length < 2) return;
  const cap = /(?:No|(\d+\.?\d{0,2})([KMB])?) views?/.exec(split[1].trim());
  const count = parseFloat(cap[1]??0) * (({K:1e3,M:1e6,B:1e9})[cap[2]]??1);
  if(count < threshold) {
    v.style.display = "none";
    //console.log(count);
  }
}

function hide_vids() {
  if(window.location.pathname.includes("watch")) document.querySelectorAll("#secondary > ytd-compact-video-renderer").forEach((v) => hide(v));
  if(window.location.pathname == "/") document.querySelectorAll("#contents > ytd-rich-item-renderer").forEach((v) => hide(v));
}

let old_path = window.location.pathname;
function check(changes, observer) {
  changes.forEach((c)=>{
    const path = window.location.pathname;
    if(path != old_path) {
      //console.log("path changed");
      old_path = path;
      setTimeout(hide_vids, 1000);
    }
    const id = c.target.id;
    if(path == "/" && id == "contents") {
      c.addedNodes.forEach((v) => hide(v));
    } else if(path.includes("watch") && (id == "items" || id == "contents")) {
      c.addedNodes.forEach((v) => {if(v.nodeName == "YTD-COMPACT-VIDEO-RENDERER") hide(v);});
    } else if(path.includes("watch") && c.target.className == "ytp-endscreen-content") {
      c.addedNodes.forEach((v) => {hide_endscreen(v);});
    }
  });
}

(new MutationObserver(check)).observe(document, {childList: true, subtree: true});

window.addEventListener("yt-page-data-updated", () => {
  // I can't figure out a good way to detect when YT has loaded new recommended vids on page transition, observer doesnt pick up the changes for whatever reason
  setTimeout(hide_vids, 1000);
});