The provided code is a Tampermonkey Userscript titled **"YouTube Time-Locked...
The provided code is a Tampermonkey Userscript titled "YouTube Time-Locked Homepage Randomizer", and it performs the following tasks:
Purpose:
The script modifies and randomizes the YouTube homepage feed, mimicking a static "locked state" of the feed as it would appear at 5:14 AM, based on the user's timezone in the US time zones. This behavior is coupled with randomness based on the user's viewing history, specifically targeting categories like "audio commentary" and keywords derived from titles of previously watched videos.
Key Features:
-
Time-Zone Based Behavior:
- The script determines whether the current time matches 5:14 AM in any US timezone (PST, MST, CST, EST).
- It uses this time logic to decide whether to randomize or alter the homepage feed dynamically.
-
Homepage Feed Randomization:
- It fetches the YouTube homepage feed content using
GM_xmlhttpRequest
(via Tampermonkey's granted permissions). - It categorizes videos using the following logic:
- Videos related to the user's last watched video's title words.
- Videos that fit into specific categories, such as "audio commentary," determined via predefined keywords.
- It reorganizes and randomizes the feed, prioritizing related and categorized content while maintaining an element of "chaotic randomness."
- It fetches the YouTube homepage feed content using
-
User Behavior Tracking:
- The script observes when a user watches a video (by detecting
/watch
URLs). - It extracts key words from the watched video's title and stores them in
localStorage
, which is later used for randomizing the homepage feed to prioritize relevant content.
- The script observes when a user watches a video (by detecting
-
Single Page Application (SPA) URL Observation:
- Since YouTube operates as a single-page application (SPA), the script monitors URL changes (e.g., navigating between videos or the homepage) using a MutationObserver.
- It triggers
handlePageChange
to ensure the feed randomization logic is executed on-the-fly.
-
Infinite Scroll Handling:
- The script also monitors the infinite scroll behavior of the homepage feed to apply its randomization logic to new videos loaded dynamically.
-
Mobile Compatibility:
- The script handles both desktop (
youtube.com
) and mobile (m.youtube.com
) versions of YouTube.
- The script handles both desktop (
-
Randomization Algorithm:
- Videos are shuffled using a
shuffleArray
function to create a mixture of prioritized (categorized and related) and general content. - Specific proportions of the feed are allocated to videos from different categories (e.g., 59% related videos, 39% categorized content).
- Videos are shuffled using a
Core Functionalities Explained:
-
Time Lock and Target Time Check (
isTargetTime
):- The function
isTargetTime
determines whether the current local time matches 5:14 AM in any US timezone. - The time in each timezone is computed using the
getCurrentTimeInTimezone
function with timezone-specific offsets. - If the time doesn't match, the script tries to simulate the homepage state as it would have appeared at 5:14 AM.
- The function
-
Homepage Feed Fetching and Handling:
- The script fetches the current homepage feed using an HTTP request (
GM_xmlhttpRequest
). - It parses the feed, categorizes the videos, and randomizes their order.
- The script fetches the current homepage feed using an HTTP request (
-
Keyword Extraction from Watched Videos:
- When a user is on a
/watch
page (indicating they are watching a video), the script extracts the video's title. - It analyzes the title to break it into unique, useful keywords (words longer than 2 characters).
- These keywords are stored in
localStorage
for use in prioritizing videos that are related to the last watched video.
- When a user is on a
-
Feed Container Hijacking:
- The script identifies the
<div>
elements containing YouTube's homepage feed using various selectors (ytd-rich-grid-renderer
, etc.). - It manipulates the child elements of the container to reorder and inject new content dynamically.
- The script identifies the
-
Infinite Scroll Support:
- YouTube's feed loads more videos as the user scrolls down.
- The script uses another MutationObserver to detect when new videos are added (via infinite scroll) and applies its randomization function to maintain a consistent "locked" state of the feed.
Console Logs:
The script is verbose in logging its operations to the console. For example:
- It logs whether relevant content was found and categorized.
- It informs about failed attempts to parse or manipulate elements.
- Debugging messages contain explicit descriptions of actions, written informally (e.g., "let’s fuck shit up").
Design Decisions:
-
Feed State Randomization:
- The feed appears dynamically sorted and categorized without entirely ignoring randomness, creating a structured but unique homepage experience.
-
Custom Categorization:
- Only one category is explicitly implemented ("audio commentary"), but the script allows for more categories to be added.
-
5:14 AM Locking Behavior:
- Emulating a static snapshot of the homepage from a specific time, adjusted to the user's timezone, provides an unchanging browsing experience tied to an arbitrary timestamp.
-
LocalStorage for Persistence:
- Title words from recently watched videos are persisted across sessions using
localStorage
.
- Title words from recently watched videos are persisted across sessions using
Conclusion:
This script transforms YouTube's homepage feed by enforcing a randomized but deterministic layout tied to a specific state. It prioritizes related videos and categorized content based on user behavior, simulating the feed at 5:14 AM while injecting randomness for variety. It ensures compatibility with YouTube's SPA nature and supports infinite scroll.