YouTube: Return Grid Layout

유튜브 홈/구독에서 보여지는 영상 갯수 제한을 원래대로 돌립니다

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

  1. // ==UserScript==
  2. // @name YouTube: Return Grid Layout
  3. // @namespace Return YouTube Grid Layout
  4. // @version 1.3.3
  5. // @description 유튜브 홈/구독에서 보여지는 영상 갯수 제한을 원래대로 돌립니다
  6. // @description:en Force YouTube grid layout to show 1~6 videos per row responsively, and scale up thumbnails on wide screens
  7. // @author DOGJIP
  8. // @match https://www.youtube.com/*
  9. // @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
  10. // @grant none
  11. // @run-at document-start
  12. // ==/UserScript==
  13.  
  14. (function () {
  15. 'use strict';
  16.  
  17. const STYLE_ID = 'custom-grid-style';
  18.  
  19. function getItemsPerRow(width) {
  20. if (width >= 2300) return 6;
  21. if (width >= 1900) return 5;
  22. if (width >= 1500) return 4;
  23. if (width >= 1050) return 3;
  24. if (width >= 650) return 2;
  25. return 1;
  26. }
  27.  
  28. function getItemWidth(width, itemsPerRow) {
  29. const containerWidth = width - 96;
  30. const totalMargin = 16 * (itemsPerRow - 1);
  31. const itemWidth = Math.floor((containerWidth - totalMargin) / itemsPerRow);
  32.  
  33. const maxDefault = 300;
  34. const minDefault = 220;
  35.  
  36. const maxW = Math.min(Math.max(itemWidth, maxDefault), 400);
  37. const minW = Math.min(Math.max(itemWidth - 20, minDefault), maxW - 10);
  38.  
  39. return { maxW, minW };
  40. }
  41.  
  42. function generateStyle(width) {
  43. const n = getItemsPerRow(width);
  44. const { maxW, minW } = getItemWidth(width, n);
  45.  
  46. return `
  47. ytd-rich-grid-renderer {
  48. --ytd-rich-grid-item-max-width: ${maxW}px !important;
  49. --ytd-rich-grid-item-min-width: ${minW}px !important;
  50. --ytd-rich-grid-row-margin: 32px !important;
  51. --ytd-rich-grid-items-per-row: ${n} !important;
  52. --ytd-rich-grid-item-margin: 16px !important;
  53. --ytd-rich-grid-posts-per-row: ${n} !important;
  54. --ytd-rich-grid-slim-items-per-row: ${n} !important;
  55. --ytd-rich-grid-game-cards-per-row: ${n} !important;
  56. --ytd-rich-grid-mini-game-cards-per-row: ${n} !important;
  57. --ytd-rich-grid-content-offset-top: 56px !important;
  58. }
  59. ytd-rich-item-renderer[hidden],
  60. ytd-rich-item-renderer[hidden][is-responsive-grid],
  61. ytd-rich-shelf-renderer[hidden] {
  62. display: block !important;
  63. }
  64. `;
  65. }
  66.  
  67. function isExcludedPage(path) {
  68. return /^\/@[^\/]+\/(?:videos|streams)\/?$/.test(path);
  69. }
  70.  
  71. function applyStyle() {
  72. if (isExcludedPage(location.pathname)) {
  73. const old = document.getElementById(STYLE_ID);
  74. if (old) old.remove();
  75. return;
  76. }
  77.  
  78. const old = document.getElementById(STYLE_ID);
  79. if (old) old.remove();
  80.  
  81. const styleEl = document.createElement('style');
  82. styleEl.id = STYLE_ID;
  83. styleEl.textContent = generateStyle(window.innerWidth);
  84. document.head.appendChild(styleEl);
  85. }
  86.  
  87. document.addEventListener('DOMContentLoaded', applyStyle);
  88. window.addEventListener('resize', applyStyle);
  89. const observer = new MutationObserver(applyStyle);
  90. observer.observe(document.body, { childList: true, subtree: true });
  91. })();