Keep Scroll Position After Clicking Tabs on YouTube Channel Page

To Keep Scroll Position After Clicking Tabs on YouTube Channel Page

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name        Keep Scroll Position After Clicking Tabs on YouTube Channel Page
// @namespace   UserScript
// @match       https://www.youtube.com/*
// @grant       none
// @version     1.1
// @license MIT
// @author      CY Fung
// @description To Keep Scroll Position After Clicking Tabs on YouTube Channel Page
// ==/UserScript==


let lastScrollTop = 0;
let lastScrollTopUpdatedAt = 0;
const _requestAnimationFrame = requestAnimationFrame;
async function checker(tab, endAt) {


  while (Date.now() - lastScrollTopUpdatedAt < endAt) {
    if (lastScrollTop !== document.documentElement.scrollTop) {
      lastScrollTopUpdatedAt = 0;
      if(tab.classList.contains('iron-selected')){

        document.documentElement.scrollTop = lastScrollTop;
        Promise.resolve().then(() => {

          document.documentElement.scrollTop = lastScrollTop;
        })
        setTimeout(() => {
          document.documentElement.scrollTop = lastScrollTop;
        }, 1)
      }

      return;
    }
    await new Promise(_requestAnimationFrame)
  }
  lastScrollTopUpdatedAt = 0;


}
document.addEventListener('click', (evt) => {
  if (!evt || !evt.isTrusted) return;
  const target = evt.target;

  Promise.resolve().then(() => {

    let tab;
    if (target instanceof HTMLElement) {
      tab = HTMLElement.prototype.closest.call(target, 'tp-yt-paper-tab.style-scope.ytd-c4-tabbed-header-renderer');
    }

    if (!tab) return;

    if (document.documentElement.scrollTop > 10) {
      lastScrollTop = document.documentElement.scrollTop;
      lastScrollTopUpdatedAt = Date.now();
      checker(tab, 830)
    }
  })

}, true)