YouTube Maximizer

Maximizes the YouTube player to fill the entire browser viewport when in theater mode

  1. // ==UserScript==
  2. // @name YouTube Maximizer
  3. // @namespace http://tampermonkey.net/
  4. // @license MIT
  5. // @version 1.4
  6. // @description Maximizes the YouTube player to fill the entire browser viewport when in theater mode
  7. // @author sharlxeniy <sharlxeniy@gmail.com>
  8. // @match https://www.youtube.com/*
  9. // @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
  10. // @grant none
  11. // ==/UserScript==
  12. (function() {
  13. 'use strict';
  14.  
  15. // 定义样式表
  16. const styleSheet = `
  17. #masthead-container {
  18. transition: opacity 0.3s ease;
  19. opacity: 0; /* 初始隐藏顶部导航栏 */
  20. pointer-events: auto;
  21. }
  22. #page-manager {
  23. margin-top: 0 !important;
  24. }
  25. #full-bleed-container {
  26. height: 100vh !important; /* 填满屏幕 */
  27. max-height: 100vh !important;
  28. }
  29. #movie_player {
  30. width: 100vw !important; /* 视频宽度为全屏 */
  31. height: 100vh !important; /* 视频高度为全屏 */
  32. }
  33. `;
  34.  
  35. function addStyles() {
  36. if (!document.querySelector('#custom-youtube-style')) {
  37. const style = document.createElement('style');
  38. style.id = 'custom-youtube-style';
  39. style.textContent = styleSheet;
  40. document.head.appendChild(style);
  41. }
  42. }
  43.  
  44. function isWatchPage() {
  45. return location.pathname === '/watch';
  46. }
  47.  
  48. function updateStyles() {
  49. if (document.cookie.includes('wide=1')) {
  50. addStyles();
  51. } else {
  52. const style = document.querySelector('#custom-youtube-style');
  53. if (style) style.remove();
  54. }
  55. }
  56.  
  57. function dismissPromo() {
  58. const dismissButton = document.querySelector('#dismiss-button button');
  59. if (dismissButton) {
  60. console.log('發現Premium廣告,已自動關閉');
  61. dismissButton.click();
  62. }
  63. }
  64.  
  65. function handleScroll() {
  66. const navbar = document.querySelector('#masthead-container');
  67. if (!navbar) return;
  68.  
  69. if (window.scrollY > 0) {
  70. navbar.style.opacity = '1';
  71. navbar.style.pointerEvents = 'auto';
  72. } else {
  73. navbar.style.opacity = '0';
  74. navbar.style.pointerEvents = 'none';
  75. }
  76. }
  77.  
  78. updateStyles();
  79. dismissPromo();
  80. const observer = new MutationObserver(() => {
  81. updateStyles();
  82. dismissPromo();
  83. });
  84. observer.observe(document.body, { childList: true, subtree: true });
  85. window.addEventListener('scroll', handleScroll);
  86.  
  87. let lastUrl = location.href;
  88. setInterval(() => {
  89. if (location.href !== lastUrl) {
  90. lastUrl = location.href;
  91. console.log('[YouTube Maximizer] URL changed, reapplying...');
  92. updateStyles();
  93. dismissPromo();
  94. }
  95. }, 500);
  96. })();