YouTube Performance CPU Optimized

H.264, event throttling, DOM hafifletme, Ambient kapatma, lazy-load yorumlar (CPU optimized)

2025/08/30のページです。最新版はこちら

このスクリプトの質問や評価の投稿はこちら通報はこちらへお寄せください
// ==UserScript==
// @name         YouTube Performance CPU Optimized
// @namespace    http://tampermonkey.net/
// @version      2.2.0
// @description  H.264, event throttling, DOM hafifletme, Ambient kapatma, lazy-load yorumlar (CPU optimized)
// @author       Sefa AVAN
// @license      MIT
// @match        http://*.youtube.com/*
// @match        http://youtube.com/*
// @match        https://*.youtube.com/*
// @match        https://youtube.com/*
// @run-at       document-start
// ==/UserScript==

(function () {
  'use strict';

  // ---------- 1) Codec override (AV1/VP9 KAPALI → H.264 kalır) ----------
  const origCanPlayType = HTMLMediaElement.prototype.canPlayType;
  HTMLMediaElement.prototype.canPlayType = function (type) {
    if (type && /codecs="?av01/i.test(type)) return '';
    if (type && /codecs="?vp0?9/i.test(type)) return '';
    return origCanPlayType.call(this, type);
  };

  if (navigator.mediaCapabilities && navigator.mediaCapabilities.decodingInfo) {
    const origDecodingInfo = navigator.mediaCapabilities.decodingInfo.bind(navigator.mediaCapabilities);
    navigator.mediaCapabilities.decodingInfo = async (cfg) => {
      const ct = cfg?.video?.contentType || '';
      if (/codecs="?av01/i.test(ct)) return { supported: false, powerEfficient: false, smooth: false };
      if (/codecs="?vp0?9/i.test(ct)) return { supported: false, powerEfficient: false, smooth: false };
      return origDecodingInfo(cfg);
    };
  }

  // ---------- 2) Event throttling ----------
  function throttleEvents(root = document) {
    const throttleMs = 50; // 20Hz
    const last = {};
    const types = ['mousemove', 'pointermove', 'scroll', 'resize'];

    types.forEach(type => {
      root.addEventListener(type, (e) => {
        const now = performance.now();
        if (last[type] && now - last[type] < throttleMs) {
          e.stopImmediatePropagation();
          e.stopPropagation();
          return;
        }
        last[type] = now;
      }, true);
    });
  }

  // ---------- 3) DOM hafifletme ----------
  function slimDOM() {
    // Sonsuz öneriler → sadece ilk öğeleri gözlemle
    document.querySelectorAll('ytd-rich-item-renderer,ytd-compact-video-renderer')
      .forEach(el => {
        if (!el.dataset.cpuOptimized) {
          el.dataset.cpuOptimized = '1';
          el.style.display = "none";
          const observer = new IntersectionObserver((entries, obs) => {
            if (entries[0].isIntersecting) {
              el.style.display = "";
              obs.disconnect();
            }
          }, { rootMargin: '2000px' });
          observer.observe(el);
        }
      });
  }

  // ---------- 4) Ambient Mode kapatma ----------
  function killAmbient() {
    const style = document.createElement('style');
    style.textContent = `
      .ytp-ambient-light,
      .ytp-ambient-mode-enabled,
      ytd-watch-flexy[ambient-mode-enabled] .ytp-ambient-light,
      ytd-watch-flexy[ambient-mode-enabled] .html5-video-player::before {
        display: none !important;
        opacity: 0 !important;
        visibility: hidden !important;
      }
    `;
    document.documentElement.appendChild(style);

    const mo = new MutationObserver(() => {
      document.querySelectorAll('ytd-watch-flexy[ambient-mode-enabled]')
        .forEach(el => el.removeAttribute('ambient-mode-enabled'));
    });
    const startAmbientObs = () => {
      const flexy = document.querySelector('ytd-watch-flexy');
      if (flexy) mo.observe(flexy, { attributes: true, attributeFilter: ['ambient-mode-enabled'] });
    };
    document.addEventListener('DOMContentLoaded', startAmbientObs);
    window.addEventListener('yt-navigate-finish', startAmbientObs);
  }

  // ---------- 5) Lazy-load yorumlar (tek seferlik IntersectionObserver) ----------
  function lazyLoadComments() {
    const comments = document.querySelector("ytd-comments#comments");
    if (!comments) return;
    comments.style.contentVisibility = "hidden";
    const obs = new IntersectionObserver((entries) => {
      if (entries[0].isIntersecting) {
        comments.style.contentVisibility = "visible";
        obs.disconnect();
      }
    });
    obs.observe(comments);
  }

  // ---------- 6) Apply ----------
  const reapply = () => {
    throttleEvents();
    slimDOM();
    killAmbient();
    lazyLoadComments();
  };

  document.addEventListener('DOMContentLoaded', reapply);
  window.addEventListener('yt-navigate-finish', reapply);
})();