Keep Scroll Position After Clicking Tabs on YouTube Channel Page

To Keep Scroll Position After Clicking Tabs on YouTube Channel Page

  1. // ==UserScript==
  2. // @name Keep Scroll Position After Clicking Tabs on YouTube Channel Page
  3. // @namespace UserScript
  4. // @match https://www.youtube.com/*
  5. // @grant none
  6. // @version 1.1
  7. // @license MIT
  8. // @author CY Fung
  9. // @description To Keep Scroll Position After Clicking Tabs on YouTube Channel Page
  10. // ==/UserScript==
  11.  
  12.  
  13. let lastScrollTop = 0;
  14. let lastScrollTopUpdatedAt = 0;
  15. const _requestAnimationFrame = requestAnimationFrame;
  16. async function checker(tab, endAt) {
  17.  
  18.  
  19. while (Date.now() - lastScrollTopUpdatedAt < endAt) {
  20. if (lastScrollTop !== document.documentElement.scrollTop) {
  21. lastScrollTopUpdatedAt = 0;
  22. if(tab.classList.contains('iron-selected')){
  23.  
  24. document.documentElement.scrollTop = lastScrollTop;
  25. Promise.resolve().then(() => {
  26.  
  27. document.documentElement.scrollTop = lastScrollTop;
  28. })
  29. setTimeout(() => {
  30. document.documentElement.scrollTop = lastScrollTop;
  31. }, 1)
  32. }
  33.  
  34. return;
  35. }
  36. await new Promise(_requestAnimationFrame)
  37. }
  38. lastScrollTopUpdatedAt = 0;
  39.  
  40.  
  41. }
  42. document.addEventListener('click', (evt) => {
  43. if (!evt || !evt.isTrusted) return;
  44. const target = evt.target;
  45.  
  46. Promise.resolve().then(() => {
  47.  
  48. let tab;
  49. if (target instanceof HTMLElement) {
  50. tab = HTMLElement.prototype.closest.call(target, 'tp-yt-paper-tab.style-scope.ytd-c4-tabbed-header-renderer');
  51. }
  52.  
  53. if (!tab) return;
  54.  
  55. if (document.documentElement.scrollTop > 10) {
  56. lastScrollTop = document.documentElement.scrollTop;
  57. lastScrollTopUpdatedAt = Date.now();
  58. checker(tab, 830)
  59. }
  60. })
  61.  
  62. }, true)

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址