YCS Auto Minimize

Minimizes the YCS menu to save screen real estate; auto-toggle between min/maximized states based on preference.

  1. // ==UserScript==
  2. // @name YCS Auto Minimize
  3. // @namespace http://tampermonkey.net/
  4. // @version 2.27
  5. // @description Minimizes the YCS menu to save screen real estate; auto-toggle between min/maximized states based on preference.
  6. // @author eedo31
  7. // @match https://www.youtube.com/watch?*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
  9. // @grant GM_addStyle
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. // v2.27 Added aria-label for better accessibility
  14.  
  15. (function () {
  16. "use strict";
  17.  
  18. // Configuration constants
  19. const RETRY_DELAY = 1000; // Delay for retrying to find elements
  20. const MAX_RETRIES = 10; // Maximum number of retries to find elements
  21. const LOCAL_STORAGE_KEY = "ycs_minimized"; // LocalStorage key for state persistence
  22.  
  23. // Utility functions
  24. function applyStyles() {
  25. GM_addStyle(`
  26. .ycs-minimize-bar {
  27. width: calc(100% + 30px);
  28. display: flex;
  29. justify-content: flex-start;
  30. align-items: center;
  31. cursor: pointer;
  32. padding: 10px 15px;
  33. background-color: transparent;
  34. border-radius: 10px;
  35. box-sizing: border-box;
  36. position: relative;
  37. left: -15px;
  38. z-index: 1000;
  39. }
  40.  
  41. .ycs-minimize-bar:hover {
  42. background-color: rgba(255, 255, 255, 0.1);
  43. }
  44.  
  45. .ycs-minimize-bar span {
  46. font-size: 16px;
  47. font-weight: bold;
  48. color: #fff;
  49. margin: 0;
  50. padding: 0;
  51. text-align: left;
  52. width: 100%;
  53. }
  54.  
  55. .ycs-app-main {
  56. display: block;
  57. }
  58.  
  59. .ycs-app {
  60. border-radius: 10px;
  61. overflow: hidden;
  62. box-shadow: 0px 2px 10px rgba(0, 0, 0, 0.2);
  63. border: 1px solid #ccc;
  64. padding-top: 0;
  65. }
  66. `);
  67. }
  68.  
  69. // DOM Manipulation functions
  70. function createMinimizeBar() {
  71. const minimizeBar = document.createElement("div");
  72. minimizeBar.classList.add("ycs-minimize-bar");
  73.  
  74. // Add aria-label for accessibility
  75. minimizeBar.setAttribute("aria-label", "Toggle YCS menu visibility");
  76.  
  77. const text = document.createElement("span");
  78. text.textContent = "Maximize YCS"; // Default text when minimized
  79. minimizeBar.appendChild(text);
  80.  
  81. return minimizeBar;
  82. }
  83.  
  84. function addMinimizeBarToPage(minimizeBar) {
  85. const ycsApp = document.querySelector(".ycs-app");
  86.  
  87. if (!ycsApp) {
  88. console.warn("Cannot find .ycs-app, retrying...");
  89. return false; // Failed to find the YCS app container
  90. }
  91.  
  92. setDynamicPadding(ycsApp, minimizeBar);
  93. ycsApp.insertBefore(minimizeBar, ycsApp.firstChild);
  94. return true; // Successfully added the minimize bar
  95. }
  96.  
  97. function setDynamicPadding(ycsApp, minimizeBar) {
  98. const appPadding = parseInt(window.getComputedStyle(ycsApp).paddingLeft, 10);
  99. minimizeBar.style.paddingLeft = `${appPadding}px`;
  100. }
  101.  
  102. // State management functions
  103. function getInitialState() {
  104. const savedState = localStorage.getItem(LOCAL_STORAGE_KEY);
  105. return savedState === "true"; // Return true if saved state is minimized
  106. }
  107.  
  108. function setMinimizedState(isMinimized) {
  109. localStorage.setItem(LOCAL_STORAGE_KEY, isMinimized.toString());
  110. }
  111.  
  112. // Bar interaction logic
  113. function toggleMinimizeBar(minimizeBar, ycsApp) {
  114. const main = ycsApp.querySelector(".ycs-app-main");
  115. const text = minimizeBar.querySelector("span");
  116.  
  117. if (!main) return;
  118.  
  119. if (main.style.display === "none") {
  120. main.style.display = "block";
  121. text.textContent = "Minimize YCS";
  122. ycsApp.style.height = "";
  123. setMinimizedState(false);
  124. } else {
  125. main.style.display = "none";
  126. text.textContent = "Maximize YCS";
  127. ycsApp.style.height = "40px";
  128. setMinimizedState(true);
  129. }
  130. }
  131.  
  132. function setupBarEventListener(minimizeBar, ycsApp) {
  133. minimizeBar.addEventListener("click", () => toggleMinimizeBar(minimizeBar, ycsApp));
  134. }
  135.  
  136. // Initialize the state and setup the bar
  137. function initializeMenuState(ycsApp) {
  138. const isMinimized = getInitialState();
  139. const main = ycsApp.querySelector(".ycs-app-main");
  140. const text = ycsApp.querySelector(".ycs-minimize-bar span");
  141.  
  142. if (!main) return;
  143.  
  144. if (isMinimized) {
  145. main.style.display = "none";
  146. ycsApp.style.height = "40px";
  147. text.textContent = "Maximize YCS";
  148. } else {
  149. main.style.display = "block";
  150. ycsApp.style.height = "";
  151. text.textContent = "Minimize YCS";
  152. }
  153. }
  154.  
  155. // Core function to add the minimize bar
  156. function addMinimizeBar() {
  157. const minimizeBar = createMinimizeBar();
  158. const ycsApp = document.querySelector(".ycs-app");
  159.  
  160. if (addMinimizeBarToPage(minimizeBar)) {
  161. setupBarEventListener(minimizeBar, ycsApp);
  162. initializeMenuState(ycsApp);
  163. } else {
  164. retryCount++;
  165. if (retryCount > MAX_RETRIES) {
  166. console.error("Max retries reached, couldn't find .ycs-app.");
  167. return;
  168. }
  169. setTimeout(addMinimizeBar, RETRY_DELAY);
  170. }
  171. }
  172.  
  173. let retryCount = 0;
  174.  
  175. // Initialize the script
  176. window.onload = function () {
  177. applyStyles();
  178. addMinimizeBar();
  179. };
  180. })();