YouTube Performance Booster

Optimizes YouTube for maximum performance with faster loading and instant navigation

当前为 2025-06-04 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name YouTube Performance Booster
  3. // @version 1.2
  4. // @description Optimizes YouTube for maximum performance with faster loading and instant navigation
  5. // @author HYPERR.
  6. // @match *://*.youtube.com/*
  7. // @grant GM_addStyle
  8. // @grant GM_setValue
  9. // @grant GM_getValue
  10. // @run-at document-start
  11. // @license MIT
  12. // @namespace https://greasyfork.org/users/1476487
  13. // ==/UserScript==
  14.  
  15. (function() {
  16. 'use strict';
  17.  
  18. // Performance optimization settings
  19. const settings = {
  20. instantNavigation: true,
  21. aggressivePrefetch: true,
  22. disableComments: false,
  23. disableRelatedVideos: false,
  24. disableNotifications: true,
  25. disableAnimations: true,
  26. lightTheme: false,
  27. qualityPreset: 'auto' // auto, 720p, 480p, 360p
  28. };
  29.  
  30. // Apply CSS optimizations immediately
  31. GM_addStyle(`
  32. /* Disable animations and transitions */
  33. ytd-app[disable-animations] * {
  34. transition: none !important;
  35. animation: none !important;
  36. scroll-behavior: auto !important;
  37. }
  38. /* Hide non-essential elements */
  39. ytd-app[disable-comments] #comments,
  40. ytd-app[disable-related] #related,
  41. ytd-app[disable-notifications] #notification-preference-button,
  42. ytd-app[disable-notifications] .notification-topbar-button {
  43. display: none !important;
  44. }
  45. /* Light theme optimization */
  46. ytd-app[light-theme] {
  47. --yt-spec-general-background-a: #ffffff !important;
  48. --yt-spec-base-background: #f8f9fa !important;
  49. --yt-spec-menu-background: #ffffff !important;
  50. }
  51. /* Performance-focused layout */
  52. #secondary.ytd-watch-flexy {
  53. display: none !important;
  54. }
  55. #primary.ytd-watch-flexy {
  56. max-width: 100% !important;
  57. }
  58. /* Remove visual fluff */
  59. ytd-masthead[is-focused] #container.ytd-masthead,
  60. ytd-thumbnail-overlay-time-status-renderer,
  61. .ytp-ce-element {
  62. opacity: 0.8 !important;
  63. }
  64. `);
  65.  
  66. // Feature toggles from settings
  67. const app = document.querySelector('ytd-app') || document.documentElement;
  68. if (settings.disableAnimations) app.setAttribute('disable-animations', '');
  69. if (settings.disableComments) app.setAttribute('disable-comments', '');
  70. if (settings.disableRelatedVideos) app.setAttribute('disable-related', '');
  71. if (settings.disableNotifications) app.setAttribute('disable-notifications', '');
  72. if (settings.lightTheme) app.setAttribute('light-theme', '');
  73.  
  74. // Instant Navigation System
  75. if (settings.instantNavigation) {
  76. let lastHoveredLink = null;
  77. let hoverTimer = null;
  78. const NAV_DELAY = 100; // ms
  79.  
  80. document.addEventListener('mouseover', function(e) {
  81. const link = e.target.closest('a');
  82. if (!link || !link.href || link.href === lastHoveredLink) return;
  83.  
  84. clearTimeout(hoverTimer);
  85. lastHoveredLink = link.href;
  86.  
  87. hoverTimer = setTimeout(() => {
  88. if (settings.aggressivePrefetch) {
  89. // Prefetch resources for instant loading
  90. fetch(link.href, {
  91. method: 'HEAD',
  92. mode: 'cors',
  93. cache: 'force-cache'
  94. });
  95. }
  96.  
  97. // Preload for instant navigation
  98. const preload = document.createElement('link');
  99. preload.rel = 'preload';
  100. preload.as = 'document';
  101. preload.href = link.href;
  102. document.head.appendChild(preload);
  103. }, NAV_DELAY);
  104. }, {capture: true, passive: true});
  105.  
  106. document.addEventListener('click', function(e) {
  107. const link = e.target.closest('a');
  108. if (link && link.href) {
  109. e.preventDefault();
  110. window.location.href = link.href;
  111. }
  112. }, {capture: true});
  113. }
  114.  
  115. // Performance optimization functions
  116. function optimizePlayer() {
  117. const player = document.querySelector('video');
  118. if (!player) return;
  119.  
  120. // Set quality preferences
  121. const qualityMap = {
  122. 'auto': 'default',
  123. '720p': 'hd720',
  124. '480p': 'large',
  125. '360p': 'medium'
  126. };
  127. const preferredQuality = qualityMap[settings.qualityPreset] || 'default';
  128. try {
  129. player.setPlaybackQualityRange(preferredQuality);
  130. } catch(e) {
  131. console.log('YT Performance: Quality setting unavailable');
  132. }
  133.  
  134. // Player performance tweaks
  135. player.preload = 'auto';
  136. player.playsInline = true;
  137. player.disablePictureInPicture = true;
  138. }
  139.  
  140. function optimizePage() {
  141. // Remove non-critical elements
  142. const elementsToRemove = [
  143. 'ytd-comments',
  144. 'ytd-watch-next-secondary-results-renderer',
  145. 'ytd-rich-item-renderer:has([overlay-style="SHORTS"])',
  146. '#secondary',
  147. '#masthead-container',
  148. '#player-ads'
  149. ];
  150.  
  151. elementsToRemove.forEach(selector => {
  152. document.querySelectorAll(selector).forEach(el => {
  153. if (el && el.parentNode) {
  154. el.parentNode.removeChild(el);
  155. }
  156. });
  157. });
  158.  
  159. // Disable resource-heavy features
  160. if ('requestIdleCallback' in window) {
  161. requestIdleCallback(() => {
  162. // Disable chat if present
  163. const liveChat = document.querySelector('ytd-live-chat-frame');
  164. if (liveChat) liveChat.remove();
  165. // Disable JS animations
  166. document.body.classList.add('no-animations');
  167. });
  168. }
  169. }
  170.  
  171. // MutationObserver to apply optimizations as page changes
  172. const observer = new MutationObserver(mutations => {
  173. optimizePlayer();
  174. optimizePage();
  175. });
  176.  
  177. observer.observe(document, {
  178. childList: true,
  179. subtree: true,
  180. attributes: false
  181. });
  182.  
  183. // Initialize optimizations
  184. window.addEventListener('load', () => {
  185. optimizePlayer();
  186. optimizePage();
  187. // Set up service worker for caching (if supported)
  188. if ('serviceWorker' in navigator) {
  189. navigator.serviceWorker.register('sw.js')
  190. .then(reg => console.log('YT Performance: Service worker registered'))
  191. .catch(err => console.log('YT Performance: Service worker failed', err));
  192. }
  193. });
  194.  
  195. // Console info
  196. console.log('YouTube Performance Booster is active!');
  197. console.log('Settings:', settings);
  198. })();