YouTube: Return Grid Layout

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

  1. // ==UserScript==
  2. // @name YouTube: Return Grid Layout
  3. // @namespace Return YouTube Grid Layout
  4. // @version 1.3.5
  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 >= 2400) return 6;
  21. if (width >= 1950) 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 - 25, minDefault), maxW - 15);
  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. /* —— 기존 유튜브 CSS 변수 유지 —— */
  48. ytd-rich-grid-renderer {
  49. --ytd-rich-grid-item-max-width: ${maxW}px !important;
  50. --ytd-rich-grid-item-min-width: ${minW}px !important;
  51. --ytd-rich-grid-row-margin: 32px !important;
  52. --ytd-rich-grid-items-per-row: ${n} !important;
  53. --ytd-rich-grid-item-margin: 16px !important;
  54. --ytd-rich-grid-posts-per-row: ${n} !important;
  55. --ytd-rich-grid-slim-items-per-row: ${n} !important;
  56. --ytd-rich-grid-game-cards-per-row: ${n} !important;
  57. --ytd-rich-grid-mini-game-cards-per-row: ${n} !important;
  58. --ytd-rich-grid-content-offset-top: 56px !important;
  59.  
  60. /* 시작 오프셋 제거 */
  61. padding-inline-start: 0 !important;
  62. --ytd-rich-grid-content-offset-left: 0 !important;
  63. }
  64.  
  65. /* —— CSS Grid: n개 컬럼 고정 + 밀도 채우기 —— */
  66. ytd-rich-grid-renderer ytd-rich-grid-row {
  67. display: grid !important;
  68. grid-auto-flow: row !important;
  69. grid-template-columns: repeat(${n}, minmax(${minW}px, 1fr)) !important;
  70. column-gap: 16px !important;
  71. row-gap: 16px !important;
  72. margin: 0 !important;
  73. justify-items: stretch !important;
  74. }
  75.  
  76. /* 각 아이템이 셀 크기에 꽉 차도록 */
  77. ytd-rich-grid-renderer ytd-rich-grid-row ytd-rich-item-renderer {
  78. width: 100% !important;
  79. margin: 0 !important;
  80. }
  81.  
  82. /* 숨겨진 아이템(placeholder)만 다시 보이게 */
  83. ytd-rich-item-renderer[hidden],
  84. ytd-rich-item-renderer[hidden][is-responsive-grid] {
  85. display: block !important;
  86. }
  87.  
  88. /* 재생목록/믹스 섹션 제거 */
  89. ytd-rich-section-renderer,
  90. ytd-rich-section-renderer ytd-rich-shelf-renderer {
  91. display: none !important;
  92. }
  93. `;
  94. }
  95.  
  96. function isExcludedPage(path) {
  97. return /^\/@[^\/]+\/(?:videos|streams)(?:[\/?].*)?$/.test(path);
  98. }
  99.  
  100. function applyStyle() {
  101. if (isExcludedPage(location.pathname + location.search)) {
  102. const old = document.getElementById(STYLE_ID);
  103. if (old) old.remove();
  104. return;
  105. }
  106.  
  107. const old = document.getElementById(STYLE_ID);
  108. if (old) old.remove();
  109.  
  110. const styleEl = document.createElement('style');
  111. styleEl.id = STYLE_ID;
  112. styleEl.textContent = generateStyle(window.innerWidth);
  113. document.head.appendChild(styleEl);
  114. }
  115.  
  116. function registerNavigationListener() {
  117. window.addEventListener('yt-navigate-finish', applyStyle);
  118. const pushState = history.pushState;
  119. history.pushState = function () {
  120. pushState.apply(this, arguments);
  121. applyStyle();
  122. };
  123. window.addEventListener('popstate', applyStyle);
  124. }
  125.  
  126. registerNavigationListener();
  127. document.addEventListener('DOMContentLoaded', applyStyle);
  128. window.addEventListener('resize', applyStyle);
  129. let debounceTimer = null;
  130. const observer = new MutationObserver(() => {
  131. clearTimeout(debounceTimer);
  132. debounceTimer = setTimeout(applyStyle, 100); // 디바운싱
  133. });
  134. })();