Greasy Fork

Greasy Fork is available in English.

修改YouTube首页布局

修改YouTube首页每行推荐视频数量

当前为 2025-04-16 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name            修改YouTube首页布局
// @name:en         Modify YouTube Homepage Layout
// @description     修改YouTube首页每行推荐视频数量
// @description:en  Change the number of recommended videos per row on the YouTube homepage
// @version         1.0
// @author          爆菊大师
// @match           https://www.youtube.com/
// @icon            https://www.youtube.com/favicon.ico
// @grant           GM_registerMenuCommand
// @license         MIT
// @namespace http://greasyfork.icu/users/929164
// ==/UserScript==

(function() {
    'use strict';
    let itemsPerRow = Math.min(10, Math.max(1,
        parseInt(localStorage.getItem('ytd-items-per-row'), 10) || 5
    ));
    const style = document.createElement('style');
    style.id = 'custom-ytd-style';
    document.head.append(style);
    const updateLayout = value => {
        style.textContent = `
            .style-scope.ytd-two-column-browse-results-renderer {
                --ytd-rich-grid-items-per-row: ${value} !important;
            }
        `;
    };
    const handleUserInput = () => {
        const input = prompt('请输入每行显示的视频数量(1-10):', itemsPerRow);
        if (input === null) return;
        const parsed = parseInt(input, 10);
        let value = Math.min(10, Math.max(1, isNaN(parsed) ? 0 : parsed));

        if (value >= 1 && value <= 10) {
            if (value !== parsed) {
                const msg = parsed < 1 ? '输入值过小,已自动调整为1'
                          : parsed > 10 ? '输入值过大,已自动调整为10'
                          : '输入值无效,已重置为默认值';
                alert(msg);
            }
            itemsPerRow = value;
            localStorage.setItem('ytd-items-per-row', value);
            updateLayout(value);
            alert(`✅ 当前设置:每行显示 ${value} 个视频`);
        } else {
            alert('⚠️ 请输入1-10之间的有效数字!');
        }
    };
    typeof GM_registerMenuCommand === 'function' &&
        GM_registerMenuCommand('📺 设置每行显示数量', handleUserInput);
    updateLayout(itemsPerRow);
})();