// ==UserScript== // @name Youtube Focus // @match *://*.youtube.com/* // @match *://*.youtube-nocookie.com/* // @version 0.2 // @description Less distractions on Youtube // @run-at document-start // @license MIT // @namespace https://greasyfork.org/en/users/6316-itsuki // @downloadURL none // ==/UserScript== ("use strict"); const SELECTORS = { whole: ["ytd-notification-topbar-button-renderer"], homePage: ["#app > .page-container", "#contents"], watchPage: ["#secondary", "#related", "ytd-comments"], guideSelection: ["ytd-guide-section-renderer.style-scope.ytd-guide-renderer"], }; function forceMiniguide() { const guideSections = document.querySelectorAll(SELECTORS.guideSelection); guideSections.forEach((section) => { if (section.hasAttribute("guide-persistent-and-visible")) { section.removeAttribute("guide-persistent-and-visible"); } if (!section.hasAttribute("mini-guide-visible")) { section.setAttribute("mini-guide-visible", ""); } }); } function removeElement(selectors) { selectors.forEach((selector) => { const elements = document.querySelectorAll(selector); elements.forEach((elemento) => elemento.remove()); }); } (function aggresiveRemoval() { removeDistractions(); requestAnimationFrame(aggresiveRemoval); })(); function isHomePage() { return ( window.location.pathname === "/" || window.location.pathname === "/feed/subscriptions" ); } function isWatchPage() { return window.location.pathname.startsWith("/watch"); } function removeDistractions() { removeElement(SELECTORS.whole); if (isHomePage()) { removeElement(SELECTORS.homePage); } else if (isWatchPage()) { removeElement(SELECTORS.watchPage); } } const guideSelection = document.querySelector(SELECTORS.guideSelection); const observer = new MutationObserver((mutations) => { for (let mutation of mutations) { if (mutation.type === "childList" && mutation.addedNodes.length > 0) { removeDistractions(); break; } else if (guideSelection) { forceMiniguide(); break; } } }); observer.observe(document.body, { childList: true, subtree: true }); window.addEventListener("unload", () => observer.disconnect());