YouTube Focus

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

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

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

(function() {
    'use strict';

    const isMobile = location.hostname.startsWith('m.');

    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.addEventListener('click', e => {
                    window.location.href = 'https://www.youtube.com/feed/you';
                });
            }

            const sidebarHome = document.querySelector('a#endpoint[title="Главная"]');
            if (sidebarHome) {
                sidebarHome.href = 'https://www.youtube.com/feed/you';
                sidebarHome.addEventListener('click', e => {
                    window.location.href = 'https://www.youtube.com/feed/you';
                });
            }
        }

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

            const flexy = document.querySelector('ytd-watch-flexy.style-scope');
            if (flexy) {
                const playerContainer = flexy.querySelector('#secondary');
                if (playerContainer) playerContainer.style.display = 'none';

                const primary = flexy.querySelector('#primary');
                if (primary) primary.style.width = '100%';
            }
        }

        updateLinks();
        hideRecommendationsAndAdjustLayout();

        const observerDesktop = new MutationObserver(() => {
            updateLinks();
            hideRecommendationsAndAdjustLayout();
        });
        observerDesktop.observe(document.body, { childList: true, subtree: true });
    }
})();