This code is a **Tampermonkey user script** that manipulates YouTube's...
September 15, 2025 at 02:16 AM
This code is a Tampermonkey user script that manipulates YouTube's interface by performing the following actions:
-
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).
-
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 arraywatchedVideos
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 thev
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 thewatchedVideos
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:
shuffleFeed()
shuffles the grid of displayed videos.markSpecifiedVideosAsWatched()
marks specific videos in the grid as "watched".
window.addEventListener('load', () => { setTimeout(() => { shuffleFeed(); markSpecifiedVideosAsWatched(); }, 1500); });
- An array called
-
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