修改YouTube首页布局

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

目前为 2025-04-16 提交的版本。查看 最新版本

// ==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 https://gf.qytechs.cn/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);
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址