YouTube Focus

Убирает отвлекающие элементы

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         YouTube Focus 
// @namespace    https://greatest.deepsurf.us/ru/scripts/570463-youtube-focus
// @author       4c5688
// @license      CC BY-SA
// @version      1.7
// @description  Убирает отвлекающие элементы
// @match        https://www.youtube.com/*
// @match        https://m.youtube.com/*
// @run-at       document-start
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const STORAGE_KEY = 'yt-focus-enabled';
    let enabled = localStorage.getItem(STORAGE_KEY) !== 'false';
    const isMobile = location.hostname.startsWith('m.');

    const fsStyle = document.createElement('style');
    fsStyle.textContent = `
        .ytp-fullscreen-grid-stills-container,
        .ytp-fullscreen-grid,
        .ytp-modern-videowall-still,
        .ytp-pause-overlay {
            display: none !important;
            visibility: hidden !important;
            opacity: 0 !important;
            pointer-events: none !important;
        }

        ytd-app[fullscreen],
        ytd-app[scrolling],
        html[fullscreen="true"],
        body[fullscreen="true"],
        :-webkit-full-screen {
            overflow: hidden !important;
            height: 100vh !important;
            position: fixed !important;
            width: 100vw !important;
        }

        ytd-watch-flexy[fullscreen] #columns {
            display: none !important;
        }
    `;
    document.documentElement.appendChild(fsStyle);

    const killScroll = (e) => {
        if (!enabled) return;

        if (document.webkitFullscreenElement || document.fullscreenElement) {
            e.stopImmediatePropagation();

            if (e.type === 'wheel') {
                e.preventDefault();
            }
        }
    };

    window.addEventListener('wheel', killScroll, { passive: false, capture: true });
    window.addEventListener('scroll', killScroll, { passive: false, capture: true });

    document.addEventListener('scroll', () => {
        if (!enabled) return;

        if (document.fullscreenElement && window.scrollY !== 0) {
            window.scrollTo(0, 0);
        }
    }, { capture: true });

    function createButtonDesktop() {
        const logo = document.querySelector('a#logo');
        if (!logo || document.getElementById('yt-focus-btn')) return;

        const btn = document.createElement('button');
        btn.id = 'yt-focus-btn';
        btn.textContent = 'Focus';

        btn.style.marginLeft = '14px';
        btn.style.padding = '6px 12px';
        btn.style.borderRadius = '18px';
        btn.style.border = 'none';
        btn.style.cursor = 'pointer';
        btn.style.background = 'transparent';

        btn.style.fontFamily = '"Roboto","Arial",sans-serif';
        btn.style.fontSize = '18px';
        btn.style.fontWeight = '700';
        btn.style.letterSpacing = '0.3px';
        btn.style.lineHeight = '20px';

        const updateStyle = () => {
            btn.style.color = enabled ? '#ff0033' : '#ffffff';
            btn.style.opacity = enabled ? '1' : '0.75';
        };

        btn.addEventListener('mouseenter', () => {
            btn.style.background = 'rgba(255,255,255,0.1)';
        });

        btn.addEventListener('mouseleave', () => {
            btn.style.background = 'transparent';
        });

        btn.addEventListener('click', () => {
            enabled = !enabled;
            localStorage.setItem(STORAGE_KEY, enabled);
            updateStyle();
            location.reload();
        });

        updateStyle();
        logo.parentElement.appendChild(btn);
    }

    function createButtonMobile() {
        const header = document.querySelector('ytm-home-logo');
        if (!header || document.getElementById('yt-focus-btn-mobile')) return;

        const btn = document.createElement('button');
        btn.id = 'yt-focus-btn-mobile';
        btn.textContent = 'Focus';

        btn.style.padding = '4px 8px';
        btn.style.borderRadius = '14px';
        btn.style.border = 'none';
        btn.style.cursor = 'pointer';
        btn.style.background = 'transparent';

        btn.style.fontFamily = '"Roboto","Arial",sans-serif';
        btn.style.fontSize = '14px';
        btn.style.fontWeight = '600';

        btn.style.marginLeft = 'auto';
        btn.style.marginRight = '8px';
        btn.style.alignSelf = 'center';

        const updateStyle = () => {
            btn.style.color = enabled ? '#ff0033' : '#ffffff';
            btn.style.opacity = enabled ? '1' : '0.75';
        };

        btn.addEventListener('click', () => {
            enabled = !enabled;
            localStorage.setItem(STORAGE_KEY, enabled);
            updateStyle();
            location.reload();
        });

        updateStyle();

        header.style.display = 'flex';
        header.style.alignItems = 'center';
        header.appendChild(btn);
    }

    const observerBtn = new MutationObserver(() => {
        if (isMobile) {
            createButtonMobile();
        } else {
            createButtonDesktop();
        }
    });

    observerBtn.observe(document.documentElement, {
        childList: true,
        subtree: true
    });

    const runMainLogic = () => {
        if (!enabled) return;

        if (isMobile) {
            if (location.pathname === '/') {
                window.location.replace('https://m.youtube.com/feed/library');
                return;
            }

            const style = document.createElement('style');
            style.textContent = `
                ytm-pivot-bar-renderer { display: none !important; }
                .related-items-container { display: none !important; }
            `;
            document.head.appendChild(style);

            function changeMobileLogo() {
                const logoBtn = document.querySelector('ytm-home-logo button');

                if (logoBtn && !logoBtn.dataset.fixed) {
                    logoBtn.dataset.fixed = 'true';

                    logoBtn.addEventListener('click', (e) => {
                        e.preventDefault();
                        e.stopPropagation();
                        window.location.href = 'https://m.youtube.com/feed/library';
                    }, true);
                }
            }

            function hideMobileRecs() {
                document.querySelectorAll(
                    '.related-items-container, ytm-item-section-renderer[section-identifier="related-items"]'
                ).forEach(el => el.remove());
            }

            const observerMobile = new MutationObserver(() => {
                changeMobileLogo();
                hideMobileRecs();
            });

            observerMobile.observe(document.body, {
                childList: true,
                subtree: true
            });

            changeMobileLogo();
            hideMobileRecs();

        } else {
            if (location.pathname === '/') {
                window.location.replace('https://www.youtube.com/feed/you');
                return;
            }

            function updateLinks() {
                const logo = document.querySelector('a#logo');

                if (logo) {
                    logo.href = 'https://www.youtube.com/feed/you';
                    logo.onclick = () => location.href = logo.href;
                }

                const sidebarHome = document.querySelector('a#endpoint[title="Главная"]');

                if (sidebarHome) {
                    sidebarHome.href = 'https://www.youtube.com/feed/you';
                    sidebarHome.onclick = () => location.href = sidebarHome.href;
                }
            }

            function hideRecommendationsAndAdjustLayout() {
                document.querySelectorAll('ytd-watch-next-secondary-results-renderer')
                    .forEach(el => el.style.display = 'none');

                const flexy = document.querySelector('ytd-watch-flexy');

                if (flexy) {
                    const secondary = flexy.querySelector('#secondary');

                    if (secondary) secondary.style.display = 'none';

                    const primary = flexy.querySelector('#primary');

                    if (primary) primary.style.width = '100%';
                }
            }

            updateLinks();
            hideRecommendationsAndAdjustLayout();

            const observerDesktop = new MutationObserver(() => {
                if (!enabled) return;

                updateLinks();
                hideRecommendationsAndAdjustLayout();
            });

            observerDesktop.observe(document.body, {
                childList: true,
                subtree: true
            });
        }
    };

    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', runMainLogic);
    } else {
        runMainLogic();
    }

})();