Greasy Fork is available in English.
修改YouTube首页每行推荐视频数量
当前为
// ==UserScript==
// @name 修改YouTube首页布局
// @name:zh 修改YouTube首页布局
// @name:en Modify YouTube Homepage Layout
// @name:ja YouTubeホームレイアウトカスタマイズ
// @description 修改YouTube首页每行推荐视频数量
// @description:zh 修改YouTube首页每行推荐视频数量
// @description:en Change the number of recommended videos per row on the YouTube homepage
// @description:ja YouTubeのホームページで1行に表示されるおすすめ動画の数を変更します
// @version 1.3
// @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==
(() => {
'use strict';
const CONFIG = {
columns: 'ytd-items-per-row',
shorts: 'ytd-shorts-blocked',
portraitColumns: 'ytd-portrait-columns'
};
const state = {
columns: Math.min(10, Math.max(1,
parseInt(localStorage.getItem(CONFIG.columns)) || 5)),
shorts: localStorage.getItem(CONFIG.shorts) === 'true',
portraitColumns: parseInt(localStorage.getItem(CONFIG.portraitColumns)) || null
};
const [layoutStyle, shortsStyle] = ['columns', 'shorts'].map(() =>
document.head.appendChild(document.createElement('style'))
);
const handleResize = () => {
if (state.portraitColumns !== null) {
const { innerWidth: width, innerHeight: height } = window;
const originalColumns = Math.min(10, Math.max(1,
parseInt(localStorage.getItem(CONFIG.columns)) || 5));
state.columns = width > height ? originalColumns : state.portraitColumns;
updateStyle();
}
};
const updateStyle = () => {
layoutStyle.textContent = `
.style-scope.ytd-two-column-browse-results-renderer {
--ytd-rich-grid-items-per-row: ${state.columns} !important;
--ytd-rich-grid-gutter-margin: 0px !important;
}
`;
shortsStyle.textContent = state.shorts ? `
ytd-rich-section-renderer,
ytd-reel-shelf-renderer {
display: none !important;
}
` : '';
};
GM_registerMenuCommand('🖥️ 设置每行显示数量', () => {
const input = prompt('请输入每行显示的视频数量(1-10):', state.columns);
if (input === null) return;
const value = Math.min(10, Math.max(1, parseInt(input) || state.columns));
localStorage.setItem(CONFIG.columns, value);
state.columns = value;
handleResize();
updateStyle();
alert(`当前设置:每行显示 ${value} 个视频`);
});
GM_registerMenuCommand('📱 竖屏显示数量', () => {
const input = prompt(`纵向模式列数(1-10,当前:${state.portraitColumns || "未设置"}):`, state.portraitColumns || 6);
if (input === null) return;
const value = Math.min(10, Math.max(1, parseInt(input) || 6));
localStorage.setItem(CONFIG.portraitColumns, value);
state.portraitColumns = value;
handleResize();
alert(`竖屏当前设置:每行显示 ${value} 个视频`);
});
if (state.portraitColumns !== null) {
GM_registerMenuCommand('清除竖屏设置', () => {
if (!confirm('确定要清除竖屏设置吗?')) return;
localStorage.removeItem(CONFIG.portraitColumns);
state.portraitColumns = null;
state.columns = Math.min(10, Math.max(1,
parseInt(localStorage.getItem(CONFIG.columns)) || 5));
updateStyle();
alert('已清除竖屏设置');
})};
GM_registerMenuCommand(state.shorts ? '显示Shorts' : '隐藏Shorts', () => {
state.shorts = !state.shorts;
localStorage.setItem(CONFIG.shorts, state.shorts);
updateStyle();
window.location.reload();
});
window.addEventListener('resize', handleResize);
handleResize();
updateStyle();
})();