Youtube Focus

Less distractions on Youtube

目前為 2024-09-22 提交的版本,檢視 最新版本

// ==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://gf.qytechs.cn/en/users/6316-itsuki
// ==/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());

QingJ © 2025

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