This script is a **Tampermonkey UserScript** titled **"YouTube Homepage Randomizer...
This script is a Tampermonkey UserScript titled "YouTube Homepage Randomizer with Related Videos (Mobile & Desktop)". It contains functionality to randomize the YouTube homepage feed based on words extracted from the currently watched video title and its related video titles, among other custom behaviors. Here's a detailed breakdown of what the code does:
Core Features:
-
Time Manipulation (Lock to 5:14 AM):
- The script hijacks the
Date
functionality in the browser to lock the system time to 5:14 AM. - It uses a proxy mechanism to intercept and override any instantiation of
Date
, includingDate.now()
. - Multiple fail-safes are implemented to ensure the time manipulation cannot be bypassed or circumvented:
- Ensures
new Date()
always returns 5:14 AM. - Overrides
Date.now()
to return the timestamp corresponding to 5:14 AM. - Locks the
window.Date
object by making it immutable usingObject.defineProperty
.
- Ensures
- The script hijacks the
-
Video Title Randomization:
- Tracks the title of the currently watched YouTube video.
- Extracts titles of the first 16 related videos using DOM selectors.
- Implements a retry mechanism:
- Attempts to fetch related video titles a maximum of 3 times per "round."
- If not enough titles are fetched in 3 attempts, it starts a new "round" (out of a maximum of 2000 rounds).
- Logs progress and failure messages at each stage.
-
Homepage Feed Randomization:
- After collecting video titles, it splits them into words (ignoring words shorter than 3 characters).
- Randomizes (shuffles) the collected words using the Fisher-Yates shuffle algorithm.
- These shuffled words are then used as keywords to generate randomized YouTube homepage feeds (the part of this logic is cut off in the code snippet but indicated by the
searchVideos()
function).
-
Clears Local Storage During Scrolling:
- Ensures that as the user scrolls and new videos appear, the local storage is automatically cleared every 556 milliseconds to possibly refresh or fetch updated randomization data.
-
Supports Both Mobile and Desktop YouTube:
- The script is designed to work on both
m.youtube.com
(mobile) andwww.youtube.com
(desktop), as indicated by the@match
directives.
- The script is designed to work on both
-
Resource Usage:
- Utilizes
GM_xmlhttpRequest
(a Tampermonkey API) for performing external HTTP requests. - Requires jQuery 3.6.0, which it loads externally.
- Utilizes
Miscellaneous:
-
Error Handling:
- Logs errors if the title fetching or time manipulation routines fail, with colorful messages.
- Includes a
resolve()
fallback for related video title fetching even if the required 16 titles cannot be fetched.
-
Randomization Probability:
- Based on the description (
...shown 52% of the time...
), some logic not visible in this snippet may determine whether to randomize the homepage feed or not, based on probability.
- Based on the description (
Purpose:
This script customizes the YouTube experience by dynamically changing the homepage recommendations. With its reliance on currently watched and related video titles, it likely aims to create a serendipitous, fresh set of recommendations each time, with strong guarantees for uptime (e.g., retries and fail-safes).