YouTube Focus Enhancer

Hides comments and recommendations until you watch a configurable percentage of the video without skipping; automatically pauses when the player moves out of view, switches between tabs, or clicks away, and automatically resumes the video when you return.

作者
Choudhary
今日安裝
1
安裝總數
3
評價
0 0 0
版本
1.5.2
建立日期
2025-09-24
更新日期
2025-09-25
尺寸
26.8 KB
授權條款
未知
腳本執行於

This script was made to improve focus while watching videos on YouTube: it hides comments and the related videos bar until you actually watch the video 'organically', automatically pauses the video when it is no longer visible or when you click outside the player, and only tries to unlock comments when a percentage of the video has been watched without skipping ahead. The goal is to make the interface less distracting and for comments/sidebar presentations to only appear when the user has paid enough attention to the content.

In practice, as soon as the page loads, the script injects a small CSS and applies classes that keep the comments and sidebar hidden by opacity/max-height/pointer-events. It keeps observing the DOM (MutationObserver) and will look for the video element until it finds it. When it finds it, it starts listening to player events (timeupdate, seeking, seeked, pause, play, ended) to know how much time you watched 'for real'. If you skip the video forward for more than a certain number of seconds, it marks that as 'skip' and resets the organically watched time — this prevents someone from just skipping ahead and immediately unlocking comments.

The unlocking criteria is percentage-based: by default, requiredPercentToUnlockComments is set to 50, meaning that when you watch 50% of the video without skipping ahead, the script removes the class that was hiding the comments and performs a smooth fade-in. If the video belongs to a playlist and you set requiredPercentToUnlockComments to 100, the script also tries to automatically pause the video a few tenths of a second before the end (configurable by pauseAtEndPlaylistTolerance) and releases the comments at that point — useful for preventing auto-play of playlists and forcing a pause at the end.

Additionally, it uses an IntersectionObserver to check how much of the player is visible in the window. If the visible fraction falls below intersectionVisibilityThreshold (a value between 0 and 1), the script automatically pauses the video; if it becomes visible above the threshold again, the script tries to play it again, but only if the user hasn't paused manually. There is also window focus detection and document visibility: when switching tabs or minimizing, it pauses, and when returning, it tries to resume (as long as the previous pause wasn't caused by the user).

For quick adjustments: change the constants at the top of the file. Changing requiredPercentToUnlockComments to 100 makes it necessary to watch the entire video before unlocking comments; reducing it to 10 unlocks too early. seekToleranceSeconds controls how many seconds of advancement count as 'skipping' — if someone skips more than this value, the organic time is discarded. intersectionVisibilityThreshold defines how much of the player needs to be visible to not pause; a lower value requires less visible area (less sensitive), whereas a higher value requires almost the entire player to be on screen. smoothRevealMs is just the duration in milliseconds of the fade that shows the comments; increase it for a slower effect, decrease it for something instantaneous.

If you want comments to always start visible, remove or comment out the call that adds the class tm-hide-comments in tryAttachHideClasses, or modify this function not to touch that element. If you prefer that the sidebar never appears, keep tm-hide-related permanently applied or modify the injected CSS to include other selectors you want to hide. To disable the pause when clicking outside the player, comment out the line that calls pauseByScript in onDocumentClick; if you want clicking outside to do nothing, you can make onDocumentClick return early when it detects clicks outside the player.

To make the unlocking criteria based on absolute seconds instead of percentage, edit tryUnlockCommentsIfEligible to compare this.watchingState.watchedOrganicSeconds with a fixed number (for example, 30 seconds) instead of calculating it based on duration. If YouTube changes selectors and the script stops finding the video, update findVideoElement and the selectors used to search for #movie_player, .html5-video-player, or ytd-comments; swapping just these selectors usually suffices to restore functionality.

If you want more logs for debugging, enable config.verbose = true and you will see console.debug with the instance ID and state messages. If the script becomes too aggressive when trying to reconnect on pages with a lot of changes, increase initRetryIntervalMs to reduce the polling frequency. Beware of automatic resume: browsers sometimes block play() due to autoplay policies; that's why resumeIfAllowed involves try/catch and only tries to play if the previous pause was made by the script itself and not by the user.

Finally, the script creates a single instance per tab and cleans the previous instance when necessary, observes URL changes (history.pushState/replaceState), and handles YouTube's internal navigation. If you want to modify advanced behavior, the central functions to modify are initForVideo (turn on/off listeners), tryUnlockCommentsIfEligible (unlock logic), tryPauseAtEndIfPlaylist (behavior in playlists), and setupIntersectionObserver (what is considered 'visible'). By changing these parts, you can, for example, make unlocking happen only after a minimum number of minutes watched, pause whenever the mouse leaves the player, or release comments only after a custom event that you define.