YouTube Comments Unlock After Watch

Hide YouTube comments until at least 50% of the video has been watched (without skipping), to improve focus. Also blocks other distractions like recommendations.

作者
Choudhary
日安装量
0
总安装量
0
评分
0 0 0
版本
0.1
创建于
2025-09-23
更新于
2025-09-23
大小
3.8 KB
许可证
MIT
适用于

This script boosts focus on YouTube by hiding comments until at least 50% of the video is watched continuously, including: 1) blocking sidebar recommendations, 2) preventing skips from unlocking comments prematurely, 3) encouraging deeper engagement amid modern distractions like short-form content.

This userscript is designed to enhance focus and productivity while watching YouTube videos by enforcing a "watch-first" rule for comments. In today's digital environment, where short-form content, endless recommendations, and comment sections can easily distract users from the main video, this script helps promote deeper engagement. Here's a breakdown of what it does, how to use it, and how to customize it.

What the Script Does
- Hides Comments Until 50% Watched: Comments (the section with user discussions below the video) remain invisible until you've progressively watched at least 50% of the video's duration. It tracks actual viewing time in real-time, ignoring pauses and preventing skips from counting toward the threshold (e.g., jumping to the end won't unlock comments prematurely).
- The right-hand sidebar with suggested videos (class: `ytd-watch-next-secondary-results-renderer`) is always hidden to minimize distractions and keep your attention on the current video.
- Uses a MutationObserver to detect and hide elements as they load dynamically on YouTube's page (e.g., when switching videos or lazy-loading occurs). It also attaches event listeners to the video player for accurate time tracking.
- The script runs entirely locally in your browser via Tampermonkey (or similar extensions like Violentmonkey or Greasemonkey). It doesn't send any data to external servers, track your activity, or interact with YouTube's API—everything is handled client-side.

This is particularly useful for students, researchers, or anyone trying to consume educational or long-form content without getting sidetracked. It encourages "mindful viewing" by delaying access to potentially spoiler-filled or off-topic discussions.

Installation and Usage Instructions
1. Install an Extension: If you haven't already, add Tampermonkey (for Chrome/Firefox/Edge) or a compatible userscript manager from your browser's store.
2. Add the Script: Click "Install" on this Greasy Fork page (or copy-paste the code into a new script in your extension's dashboard).
3. Test It: Open any YouTube video (e.g., https://www.youtube.com). Comments should be hidden initially. Play the video continuously—after reaching 50% (e.g., 5 minutes into a 10-minute video), comments will appear automatically.
4. Compatibility: Works on modern browsers with YouTube's current layout (as of script version 0.1). If YouTube changes its HTML structure, the script may need updates—check for newer versions here.
5. Troubleshooting: If comments don't hide/unhide as expected, refresh the page or disable/re-enable the script. It only runs on pages matching `https://www.youtube.com/`.

Pair this with extensions like "Remove YouTube Shorts" (Chrome Web Store: https://chromewebstore.google.com/detail/remove-youtube-shorts/mgngbgbhliflggkamjnpdmegbkidiapm) to eliminate addictive short videos, creating a distraction-free YouTube experience.

How to Customize the Script
The script is easy to modify since it's written in plain JavaScript. Open it in your userscript manager's editor and tweak the code as needed. Here are common customizations with step-by-step instructions (line numbers are approximate based on the original code):

1. Change the Unlock Threshold (e.g., from 50% to 70%):
- Find the line in `handleTimeUpdate()`: `if (watchedTime >= 0.5 duration) {`
- Change `0.5` to your desired fraction (e.g., `0.7` for 70%, or `0.3` for 30%).
- Example: For full video required, use `1.0`. Save and reload the page to test.

2. Allow Skips to Count Toward Progress (Disable Anti-Skip Feature):
- This script only counts continuous play (delta < 2 seconds). To let skips unlock comments (e.g., if you jump ahead but still want credit):
- Comment out or remove the `if (delta > 0 && delta < 2)` check in `handleTimeUpdate()`.
- Replace with `watchedTime += delta;` (or use `watchedTime = video.currentTime;` for total position-based unlocking, ignoring watch order).

3. Disable Hiding of Sidebar Recommendations:
- If you want to keep suggestions visible:
- Remove or comment out these lines:
- `hideElementsByClass('ytd-watch-next-secondary-results-renderer');` (in the observer callback and initial checks).
- This leaves only comment hiding active.

4. Hide Additional Elements (e.g., Video Description or Related Videos):
- To block more distractions, add new calls in the observer's `mutations.forEach` loop and initial checks.
- Example: To hide the video description, add `hideElementsByClass('ytd-video-primary-info-renderer');` (check YouTube's inspect tool for exact classes/IDs).
- For IDs: Use `hideElementById('related');` to hide the entire related section.

5. Add a Manual Unlock Button (for Flexibility):
- Insert this after the observer setup:
```
const unlockButton = document.createElement('button');
unlockButton.textContent = 'Unlock Comments Early';
unlockButton.onclick = () => { unlocked = true; showElementById('comments'); };
document.body.appendChild(unlockButton); // Customize positioning with CSS.
```
- Style it with `unlockButton.style.position = 'fixed'; unlockButton.style.bottom = '10px';` etc.

6. Make It Video-Specific (e.g., Only for Certain Channels):
- Add a check in `setupVideo()`: `if (!window.location.href.includes('channel/UCexample')) return;`
- Replace `'UCexample'` with the channel ID (from URL).

7. Advanced: Add Logging for Debugging:
- Insert `console.log('Watched time:', watchedTime, 'Duration:', duration);` in `handleTimeUpdate()` to monitor progress in the browser console (F12 > Console).

Always test changes on a sample video. If you're new to JS, use tools like JSFiddle or your browser's console to experiment. For major edits, consider forking the script on GitHub and linking it here for updates.