This code is a **Tampermonkey user script** that manipulates YouTube's...

September 15, 2025 at 02:16 AM

// ==UserScript== // @name YouTube Feed Manipulator (8× Repeat) // @namespace http://tampermonkey.net/ // @version 1.1 // @description Shuffles your YouTube feed items on reload, and marks the last two videos watched 8× automatically. // @author chatgpt // @match https://www.youtube.com/* // @match https://m.youtube.com/* // @grant none // ==/UserScript== (function() { 'use strict'; // --- 1) Your base configuration --- const baseWatched = [ '65k8VIyJlXQ', 'q5-b7v6EIzc', 'gaBBZshLhMY', '70-SQST-ivM', 'Q6f3t-JGAio', 'W_jr0Y0N0u8', 'KElR4TN6UNo', 'k_q9y5vSIrw' ]; // --- 2) Automatically repeat the last two IDs 8× total --- // (they already appear once in baseWatched, so add 7 more of each) const watchedVideos = baseWatched.slice(); // clone const lastTwo = baseWatched.slice(-2); // get the final 2 IDs for (let i = 0; i < 7; i++) { watchedVideos.push(...lastTwo); } // --- 3) Utility: extract video ID from a YouTube URL --- function extractVideoIdFromUrl(url) { const m = url.match(/[?&]v=([^&#]+)/); return m ? m,[object Object], : null; } // --- 4) Shuffle feed items in place (Fisher–Yates) --- function shuffleFeed() { const grid = document.querySelector('ytd-rich-grid-renderer .rich-grid-renderer') || document.querySelector('ytd-item-section-renderer ytd-section-list-renderer'); if (!grid) return; const items = Array.from(grid.children).filter(el => el.matches('ytd-rich-grid-media, ytd-video-renderer, ytd-grid-video-renderer') ); if (items.length < 2) return; for (let i = items.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); [items[i], items[j]] = [items[j], items[i]]; } items.forEach(item => grid.appendChild(item)); console.log(`Shuffled ${items.length} feed items`); } // --- 5) Mark specified videos as watched (by ID match) --- function markSpecifiedVideosAsWatched() { const videos = document.querySelectorAll( 'ytd-rich-grid-media, ytd-video-renderer, ytd-grid-video-renderer' ); videos.forEach(videoEl => { const a = videoEl.querySelector('a#video-title, a.yt-simple-endpoint'); if (!a) return; const id = extractVideoIdFromUrl(a.href); if (watchedVideos.includes(id)) { videoEl.classList.add('watched-video'); } }); } // --- 6) Inject CSS for .watched-video --- const css = ` .watched-video { opacity: 0.5 !important; border: 2px solid #ccc; }`; const style = document.createElement('style'); style.textContent = css; document.head.appendChild(style); // --- 7) On page load: shuffle then mark watched --- window.addEventListener('load', () => { setTimeout(() => { shuffleFeed(); markSpecifiedVideosAsWatched(); }, 1500); }); })();

This code is a Tampermonkey user script that manipulates YouTube's interface by performing the following actions:

  1. Title and Metadata:

    • The script is named "YouTube Feed Manipulator (8× Repeat)".
    • It works on YouTube desktop (https://www.youtube.com/*) and YouTube mobile (https://m.youtube.com/*).
    • The stated purpose is to shuffle YouTube feed items and mark specific videos as 'watched', with the last two video IDs repeated 8 times (total).

  1. Code Behavior:

    Here's a breakdown of what the script does:

    a) Base Video IDs:

    • An array called baseWatched contains several video IDs, which represent videos to be marked as "watched".
    const baseWatched = [
        '65k8VIyJlXQ',
        'q5-b7v6EIzc',
        'gaBBZshLhMY',
        ...
    ];
    

    b) Repetition of the Last Two Video IDs:

    • The last two IDs in the baseWatched array are extracted and repeated 8 times (including their original occurrences). This creates a new array watchedVideos containing all video IDs.
    const lastTwo = baseWatched.slice(-2); // Last two IDs
    for (let i = 0; i < 7; i++) {
        watchedVideos.push(...lastTwo);  // Add last two IDs 7 more times
    }
    

    c) Extract Video ID from a YouTube URL:

    • The extractVideoIdFromUrl function extracts the v parameter (video ID) from a YouTube URL if present.
    function extractVideoIdFromUrl(url) {
        const m = url.match(/[?&]v=([^&#]+)/);
        return m ? m[1] : null;
    }
    

    d) Shuffle YouTube Feed:

    • The function shuffleFeed shuffles the video feed displayed on YouTube using the Fisher–Yates shuffle algorithm.
    • It identifies YouTube grid elements (ytd-rich-grid-media, ytd-video-renderer, etc.), shuffles their order, and appends shuffled elements back to the grid.
    function shuffleFeed() {
        const grid = document.querySelector('ytd-rich-grid-renderer .rich-grid-renderer');
        const items = Array.from(grid.children).filter(/* filter video elements */);
        for (let i = items.length - 1; i > 0; i--) {
            const j = Math.floor(Math.random() * (i + 1));
            [items[i], items[j]] = [items[j], items[i]];  // Swap items
        }
        items.forEach(item => grid.appendChild(item));  // Re-append shuffled
    }
    

    e) Mark Videos as 'Watched':

    • The markSpecifiedVideosAsWatched function scans the videos on the page and checks their IDs against the watchedVideos array.
    • If a video ID matches one in the array, it adds a CSS class (.watched-video) to the corresponding element, visually marking it as watched.
    function markSpecifiedVideosAsWatched() {
        const videos = document.querySelectorAll(
            'ytd-rich-grid-media, ytd-video-renderer, ytd-grid-video-renderer'
        );
        videos.forEach(videoEl => {
            const id = extractVideoIdFromUrl(a.href);
            if (watchedVideos.includes(id)) {
                videoEl.classList.add('watched-video');
            }
        });
    }
    

    f) Styling Watched Videos:

    • The script dynamically adds CSS rules to the page to style .watched-video elements. These videos are made semi-transparent (opacity: 0.5) and outlined with a border.
    const css = `
    .watched-video {
        opacity: 0.5 !important;
        border: 2px solid #ccc;
    }`;
    

    g) Combine and Run on Page Load:

    • When the page loads, the script waits 1.5 seconds before executing:
      1. shuffleFeed() shuffles the grid of displayed videos.
      2. markSpecifiedVideosAsWatched() marks specific videos in the grid as "watched".
    window.addEventListener('load', () => {
        setTimeout(() => {
            shuffleFeed();
            markSpecifiedVideosAsWatched();
        }, 1500);
    });
    

  1. Summary of Functionality:

  • Shuffles YouTube feed videos: Randomly rearranges the videos in the main feed using the Fisher–Yates shuffle.
  • Marks videos as 'watched': Adds visual indicators (semi-transparent effect and border) to videos based on their video IDs.
  • Repetition of video IDs: Ensures that the last two videos in the original list are processed 8 times in total when marking them as watched.
Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node