YouTube 3 Video Grid Fix

Fixes Youtubes stupid update idea to allow only 3 videos to load in each grid.

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

You will need to install an extension such as Tampermonkey to install this script.

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         YouTube 3 Video Grid Fix
// @namespace    http://tampermonkey.net/
// @version      1.3
// @description  Fixes Youtubes stupid update idea to allow only 3 videos to load in each grid.
// @match        *://www.youtube.com/*
// @grant        none
// @run-at       document-start
// @license MIT
// ==/UserScript==

(function(){
    'use strict';
    const ID = 'yt5col-home',
          CSS = `
            ytd-rich-grid-renderer{--ytd-rich-grid-items-per-row:5!important}
            ytd-rich-grid-renderer>#contents>ytd-rich-grid-row,
            ytd-item-section-renderer ytd-rich-grid-row{
              display:grid!important;
              grid-template-columns:repeat(5,minmax(0,1fr))!important;
              gap:var(--ytd-rich-grid-gutter-margin,16px)!important;
              width:100%!important
            }
            ytd-thumbnail{max-height:none!important;height:auto!important;width:100%!important}
            ytd-thumbnail img{width:100%!important;height:100%!important;object-fit:cover!important}
          `;
    function update(){
        if(location.pathname==='/' && !document.getElementById(ID)){
            let s=document.createElement('style');
            s.id=ID; s.textContent=CSS;
            document.head.appendChild(s);
        }
        else if(location.pathname!=='/' && document.getElementById(ID)){
            document.getElementById(ID).remove();
        }
    }
    history.pushState     = (f=>function(){ f.apply(this,arguments); update(); })(history.pushState);
    history.replaceState  = (f=>function(){ f.apply(this,arguments); update(); })(history.replaceState);
    window.addEventListener('popstate', update);
    window.addEventListener('yt-navigate-finish', update);
    update();
})();