This code is a **Tampermonkey UserScript** designed to modify the...
This code is a Tampermonkey UserScript designed to modify the behavior of YouTube's "Related Videos" section when watching a video. Here's a detailed breakdown of what it does:
Purpose
The script creates a system to randomize and reorder the related videos on YouTube using different tiers of randomness methods, with a fallback mechanism if an earlier tier doesn't work. It also collects statistics on the performance of each tier to refine its decision-making (adaptive tier selection).
Features
-
Randomizes "Related Videos":
- Implements 4 tiers of behavior to reorder the recommendations:
- Tier 1 (Relevance Ranking): Reorders videos based on how closely their titles relate (word overlap) to the current video’s title.
- Tier 2 (Title-Based Shuffle): Shuffles videos deterministically using a title-based seed for the randomness (ensures predictable but randomized order).
- Tier 3 (Pure Random Shuffle): Fully randomizes the order of the videos.
- Tier 4 (Fallback): Does nothing; leaves the videos in their original order as a safety fallback.
- Implements 4 tiers of behavior to reorder the recommendations:
-
Tier-Based Error Recovery:
- If one tier (e.g., relevance ranking) fails or does not function properly, the script tries the next tier, ensuring it falls back reliably.
-
Statistics Tracking:
- Tracks how often each tier is attempted and succeeds.
- Uses the stats to determine the best-performing tier dynamically.
- Prioritizes tiers with high success and relevance rates.
How It Works
-
Core Workflow:
- Captures the "Related Videos" container (
#related #items
) on YouTube. - Tries to reorder the related videos according to the selected tier method.
- If the current tier completes successfully and changes the order, it updates the statistics for that tier.
- Adapts its tier preferences based on the recorded success rates.
- Captures the "Related Videos" container (
-
Helper Functions:
- Functions like
getRelatedVideosContainer
,getCurrentVideoTitle
, andreplaceVideos
help identify and manage the related videos. - Functions like
hashString
andseededRandom
are used for deterministic shuffling.
- Functions like
-
Dynamic Tier Selection:
- Analyzes the performance stats (
tierStats
) to determine the best tier to use. - Prefers tiers with high success rates (>91%), or the one with the best overall success rate in fallback cases.
- Analyzes the performance stats (
-
Mutation Observing:
- Uses a
MutationObserver
to detect changes in the YouTube DOM (e.g., when new related videos are loaded after switching videos). - Automatically triggers the randomization logic whenever related videos are loaded or updated.
- Uses a
Detailed Explanation of Tiers
-
Tier 1 (Relevance Ranking):
- Compares the title of the current video to the titles of the related videos.
- Scores related videos based on word overlap with the current video’s title.
- Re-sorts the videos based on these scores to display videos with higher relevance near the top.
-
Tier 2 (Title-Based Shuffle):
- Uses a seeded random generator based on the current video's title to reorder the videos.
- Ensures consistent shuffling (i.e., same title leads to the same reordered list every time).
-
Tier 3 (Pure Random Shuffle):
- Fully randomizes the order of the related videos.
-
Tier 4 (No Change):
- Does nothing and leaves the videos unchanged, serving as a fallback in case previous tiers fail.
Statistics Tracking
- Records:
- Number of attempts (
attempts
) for each tier. - Number of successful reordering operations (
successes
).
- Number of attempts (
- Dynamically adapts tier selection based on the success rate (successes/attempts).
- Prefers tiers with a high success rate and relevance.
Tampermonkey-Specific Features
-
GM_getValue
andGM_setValue
:- Used to persist
tierStats
across sessions (local storage for statistical data).
- Used to persist
-
@match
Metadata:- Restricts the script's execution to YouTube video pages (
*://*.youtube.com/watch*
).
- Restricts the script's execution to YouTube video pages (
-
MutationObserver
:- Observes changes in the YouTube page’s structure to trigger randomization every time the "Related Videos" section is updated.
Use Case
This script is primarily for users who want to:
- Add variety to their YouTube recommendations by reordering the "Related Videos."
- Experiment with different randomization techniques (relevance-based vs random).
- Avoid seeing the same recommendations repeatedly in their feed.
- Have a fallback system in place if something goes wrong with reordering.
Caution: By modifying YouTube's DOM, this script could unintentionally break or become outdated if YouTube changes its structure or class names. Updates to the script might be required to keep it functional.