Skip to content

Instantly share code, notes, and snippets.

@lachlansleight
Last active April 20, 2023 00:38
Show Gist options
  • Save lachlansleight/0a4bc926e71a10d470d79755f60a217f to your computer and use it in GitHub Desktop.
Save lachlansleight/0a4bc926e71a10d470d79755f60a217f to your computer and use it in GitHub Desktop.
YouTube Shorts Hider (TamperMonkey Script)
// ==UserScript==
// @name YouTube Shorts Hider
// @namespace lachlansleight
// @version 0.1
// @description Hides all evidence of Shorts from the YouTube desktop site
// @author You
// @match https://www.youtube.com*/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @grant none
// ==/UserScript==
(function() {
'use strict';
const querySelectorAsArray = (selector, parent = null) => {
const items = parent ? parent.querySelectorAll(selector) : document.querySelectorAll(selector);
const asArray = [];
for(let i = 0; i < items.length; i++) asArray.push(items.item(i));
return asArray;
}
const filterByAttribute = (item, attributeName, attributeValue = undefined) => {
if(!item.getAttributeNames().includes(attributeName)) return false;
if(attributeValue) return item.getAttribute(attributeName) === attributeValue;
return true;
}
const hideShortsFromSidebar = () => {
const sidebarItems = querySelectorAsArray(".yt-simple-endpoint").filter(n => filterByAttribute(n, "title", "Shorts"));
if(sidebarItems.length === 0) return;
sidebarItems[0].parentElement.style.display = "none";
}
const hideShortsSectionFromHomePage = () => {
const shorts = querySelectorAsArray(".ytd-rich-section-renderer").filter(n => filterByAttribute(n, "is-shorts"));
if(shorts.length === 0) return;
shorts[0].parentElement.style.display = "none";
}
const hideShortsTilesFromSubscriptionsPage = () => {
const getRenderer = () => {
const renderers = querySelectorAsArray(".ytd-two-column-browse-results-renderer");
return renderers.filter(n => filterByAttribute(n, "page-subtype", "subscriptions"));
}
const getTiles = (parentNode) => {
const tiles = querySelectorAsArray(".ytd-thumbnail", parentNode);
return tiles.filter(n => filterByAttribute(n, "overlay-style", "SHORTS"));
}
let subscriptions = getRenderer();
if(subscriptions.length === 0) return;
const tiles = getTiles(subscriptions[0]);
tiles.forEach(n => {
const parent = n.parentElement?.parentElement?.parentElement?.parentElement?.parentElement;
if(parent) parent.style.display = "none";
});
}
const doHide = async () => {
while(true) {
hideShortsFromSidebar();
hideShortsSectionFromHomePage();
if(window.location.href.includes("/subscriptions")) hideShortsTilesFromSubscriptionsPage();
await new Promise(resolve => setTimeout(resolve, 500));
}
}
doHide();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment