Skip to content

Instantly share code, notes, and snippets.

@aleclarson
Last active February 8, 2025 19:56
Show Gist options
  • Select an option

  • Save aleclarson/d9c5afe4d87fd39a5c0fa60cadaeadd5 to your computer and use it in GitHub Desktop.

Select an option

Save aleclarson/d9c5afe4d87fd39a5c0fa60cadaeadd5 to your computer and use it in GitHub Desktop.

Revisions

  1. aleclarson revised this gist Feb 8, 2025. 1 changed file with 33 additions and 11 deletions.
    44 changes: 33 additions & 11 deletions script.js
    Original file line number Diff line number Diff line change
    @@ -23,36 +23,58 @@
    }

    // Check if click target is within or is a ytd-rich-item-renderer
    const richItem = event.target.closest('ytd-rich-item-renderer');
    let richItem = event.target.closest('ytd-rich-item-renderer, ytd-compact-video-renderer');
    if (!richItem) {
    return;
    const enclosingLink = event.target.closest('a[href^="/watch?"]');
    const parsedLink = new URL(enclosingLink.href);
    for (const link of document.querySelectorAll(`a[href^="/watch${parsedLink.search}"]`)) {
    richItem = link.closest('ytd-rich-item-renderer, ytd-compact-video-renderer');
    if (richItem) {
    break;
    }
    }
    if (!richItem) {
    alert('No rich item found');
    return;
    }
    }

    event.preventDefault();
    event.stopPropagation();

    // Find and click the feedback shape
    const feedbackShape = richItem.querySelector('yt-touch-feedback-shape');
    if (feedbackShape) {
    event.preventDefault();
    event.stopPropagation();
    feedbackShape.click();
    const menuButton = richItem.querySelector('yt-touch-feedback-shape, button[aria-label="Action menu"]');
    if (!menuButton) {
    alert('No menu button found');
    return;
    }

    menuButton.click();

    // Wait for popup to appear
    await new Promise(resolve => setTimeout(resolve, 100));
    await new Promise(resolve => setTimeout(resolve, 300));

    // Find popup container
    const popupContainer = document.querySelector('ytd-popup-container yt-contextual-sheet-layout');
    const popupContainer = document.querySelector('ytd-popup-container');
    if (!popupContainer) {
    alert('No popup container found');
    return;
    }

    const menuPopup = popupContainer.querySelector('ytd-menu-popup-renderer, yt-contextual-sheet-layout');
    if (!menuPopup) {
    alert('No menu popup found');
    return;
    }

    // Get all list items
    const listItems = popupContainer.querySelectorAll('yt-list-item-view-model');
    const menuItems = popupContainer.querySelectorAll('yt-list-item-view-model, ytd-menu-service-item-renderer');

    // Determine which text to look for based on key combination
    const targetText = event.shiftKey ? "Don't recommend channel" : "Not interested";

    // Find and click the matching item
    for (const item of listItems) {
    for (const item of menuItems) {
    if (item.textContent.includes(targetText)) {
    item.click();
    break;
  2. aleclarson created this gist Feb 8, 2025.
    62 changes: 62 additions & 0 deletions script.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,62 @@
    // ==UserScript==
    // @name YouTube Enhanced Interaction
    // @namespace http://tampermonkey.net/
    // @version 0.1
    // @description Enhanced click handling for YouTube recommendations
    // @author You
    // @match https://www.youtube.com/*
    // @grant none
    // ==/UserScript==

    (function () {
    'use strict';

    document.addEventListener('click', async (event) => {
    // Skip if ctrl or meta (command) key is pressed
    if (event.ctrlKey || event.metaKey) {
    return;
    }

    // Only proceed if alt key is pressed
    if (!event.altKey) {
    return;
    }

    // Check if click target is within or is a ytd-rich-item-renderer
    const richItem = event.target.closest('ytd-rich-item-renderer');
    if (!richItem) {
    return;
    }

    // Find and click the feedback shape
    const feedbackShape = richItem.querySelector('yt-touch-feedback-shape');
    if (feedbackShape) {
    event.preventDefault();
    event.stopPropagation();
    feedbackShape.click();
    }

    // Wait for popup to appear
    await new Promise(resolve => setTimeout(resolve, 100));

    // Find popup container
    const popupContainer = document.querySelector('ytd-popup-container yt-contextual-sheet-layout');
    if (!popupContainer) {
    return;
    }

    // Get all list items
    const listItems = popupContainer.querySelectorAll('yt-list-item-view-model');

    // Determine which text to look for based on key combination
    const targetText = event.shiftKey ? "Don't recommend channel" : "Not interested";

    // Find and click the matching item
    for (const item of listItems) {
    if (item.textContent.includes(targetText)) {
    item.click();
    break;
    }
    }
    }, /* capture */ true);
    })();