Skip to content

Instantly share code, notes, and snippets.

@dusekdan
Last active March 18, 2024 13:59
Show Gist options
  • Save dusekdan/d049b817d1865a1d98a86e7d97f08e08 to your computer and use it in GitHub Desktop.
Save dusekdan/d049b817d1865a1d98a86e7d97f08e08 to your computer and use it in GitHub Desktop.

Revisions

  1. dusekdan revised this gist Mar 18, 2024. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions user.script.js
    Original file line number Diff line number Diff line change
    @@ -14,16 +14,16 @@

    // Known limitations:
    // (a) If various elements become available for hiding at different times, the interval of checking for elements will stop after first item successfully found and hidden
    // Rationale: I don't want to be firing an infinite intervals of setting the style in the browser.
    // Possible solution: Have a re-try logic for finding all the elements with a maximum amount of attempts before giving up.
    // Rationale: I don't want to be firing an infinite intervals of setting the style in the browser.
    // Possible solution: Have a re-try logic for finding all the elements with a maximum amount of attempts before giving up.

    function hideElementsWhenTheyLoad() {

    // It is likely that across various versions, shorts are gonna come up with different selectors
    const shortsSideBarMarch2024Selector = "ytd-guide-section-renderer.style-scope:nth-child(1) > div:nth-child(2) > ytd-guide-entry-renderer:nth-child(2)"
    const shortsSideBarMarch2024Selector = "ytd-guide-section-renderer.style-scope:nth-child(1) > div:nth-child(2) > ytd-guide-entry-renderer:nth-child(2)"


    // Put all the selectors to hide into this field
    // Put all the selectors to hide into this field
    const selectorsToHide = [shortsSideBarMarch2024Selector]

    let hidingComplete = false;
  2. dusekdan created this gist Mar 18, 2024.
    52 changes: 52 additions & 0 deletions user.script.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    // ==UserScript==
    // @name YoutubeShortsRemover
    // @match https://youtube.com/*
    // @match https://www.youtube.com/*
    // @match https://*.youtube.com/*
    // @version 0.1
    // @author Daniel Dusek (github: dusekdan)
    // @grant none
    // ==/UserScript==

    (function() {
    'use strict';


    // Known limitations:
    // (a) If various elements become available for hiding at different times, the interval of checking for elements will stop after first item successfully found and hidden
    // Rationale: I don't want to be firing an infinite intervals of setting the style in the browser.
    // Possible solution: Have a re-try logic for finding all the elements with a maximum amount of attempts before giving up.

    function hideElementsWhenTheyLoad() {

    // It is likely that across various versions, shorts are gonna come up with different selectors
    const shortsSideBarMarch2024Selector = "ytd-guide-section-renderer.style-scope:nth-child(1) > div:nth-child(2) > ytd-guide-entry-renderer:nth-child(2)"


    // Put all the selectors to hide into this field
    const selectorsToHide = [shortsSideBarMarch2024Selector]

    let hidingComplete = false;
    let intervalId = setInterval( () => {
    console.log("Running hideElements() function");
    selectorsToHide.forEach(selector => {
    const elementToHide = document.querySelector(selector);
    if (elementToHide) {
    elementToHide.style.display = 'none';
    console.log("YoutubeShortsRemover, set display=none for " + selector);
    hidingComplete = true;
    }
    });


    if (hidingComplete) {
    clearInterval(intervalId);
    }

    }, 2000);

    }

    // Run the function when the page loads
    window.addEventListener('load', hideElementsWhenTheyLoad);
    })();