YouTube 4-Column Home Page Layout

Force YouTube to use a 4-column grid, fix layout, and remove section renderers like Shorts etc.

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         YouTube 4-Column Home Page Layout
// @namespace    http://tampermonkey.net/
// @version      1.4
// @description  Force YouTube to use a 4-column grid, fix layout, and remove section renderers like Shorts etc.
// @author       violettt
// @match        https://www.youtube.com/*
// @grant        none
// @license      GPLv3
// ==/UserScript==

(function () {
    'use strict';

    function injectCustomCSS() {
        const style = document.createElement('style');
        style.textContent = `
            ytd-rich-item-renderer {
                width: calc(92% / 4) !important;
            }

            /* Optional: reduce spacing if needed */
            ytd-rich-grid-renderer #contents {
                column-gap: 0 !important;
                row-gap: 0 !important;
            }
        `;
        document.head.appendChild(style);
    }

    function observeAndFixGrid() {
        const observer = new MutationObserver(() => {
            updateGrid();
        });

        const container = document.querySelector('#contents');
        if (container) {
            observer.observe(container, { childList: true, subtree: true });
            updateGrid(); // initial run
        }
    }

    function updateGrid() {
        // Remove sections like Shorts or "Recommended topics"
        document.querySelectorAll('ytd-rich-section-renderer').forEach(el => el.remove());

        const items = Array.from(document.querySelectorAll('ytd-rich-item-renderer'));

        items.forEach((item, index) => {
            // Change attribute to 4
            item.setAttribute('items-per-row', '4');

            // Update "is-in-first-column" class
            item.classList.remove('is-in-first-column');
            if (index % 4 === 0) {
                item.classList.add('is-in-first-column');
            }
        });
    }

    function waitForGrid() {
        const check = setInterval(() => {
            if (document.querySelector('ytd-rich-item-renderer')) {
                clearInterval(check);
                injectCustomCSS();
                observeAndFixGrid();
            }
        }, 500);
    }

    waitForGrid();
})();