Fix Skip YouTube Ads Bug in Brave

For personal use only.

Bu betiği kurabilmeniz için Tampermonkey, Greasemonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Userscripts gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

You will need to install an extension such as Tampermonkey to install this script.

Bu komut dosyasını yüklemek için bir kullanıcı komut dosyası yöneticisi uzantısı yüklemeniz gerekecek.

(Zaten bir kullanıcı komut dosyası yöneticim var, kurmama izin verin!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Zateb bir user-style yöneticim var, yükleyeyim!)

// ==UserScript==
// @name        Fix Skip YouTube Ads Bug in Brave
// @namespace   Violentmonkey Scripts
// @match       https://www.youtube.com/*
// @version     0.0.002
// @author      -
// @license MIT
// @run-at              document-start
// @grant               none
// @unwrap
// @allFrames           true
// @inject-into         page
// @description For personal use only.
// ==/UserScript==


(() => {

  let cid = 0;

  const [setInterval_, clearInterval_, setTimeout_, clearTimeout_] = [setInterval, clearInterval, setTimeout, clearTimeout];

  const Promise = (async () => { })().constructor;

  let md1 = '';
  let md2 = 0;
  let qt = 0;

  let wd1 = 0;
  let wd2 = 0;

  const timerFn = () => {

    const mps = [...document.querySelectorAll('#movie_player')].filter(e => !e.closest('[hidden]'));
    if (mps.length !== 1) return;

    const media = mps[0].querySelectorAll('audio, video');
    if (media.length !== 1) return;
    const md = media[0];

    if (Number.isFinite(md.currentTime) && md.currentTime > wd1 && md.currentTime > 0.1) {
      wd1 = md.currentTime;
      wd2++;
    } else if (Number.isFinite(md.currentTime) && md.currentTime > 0.1) {

    } else {
      wd1 = 0;
      wd2 = 0;
    }

    if (wd2 >= 3) {
      clearInterval_(cid);
      cid = 0;
    }

    if (Number.isFinite(md.currentTime) && md.currentTime < 0.0001 && md.duration > 5 && (qt ? md.duration < qt + 5 : true)) {
      const t = `${md.currentTime}${md.duration}${md.src}`;
      if (md1 === t) md2++;
      else {
        md1 = t;
        md2 = 0;
      }

    } else if (md2 > 0) {
      md1 = '';
      md2 = 0;
    }

    if (md2 >= 3) {
      md.currentTime = md.duration;
    }

  };

  const triggerFn = async (m) => {

    cid && clearInterval_(cid);
    cid = 0;
    if (m !== null) qt = +m;
    md1 = '';
    md2 = 0;
    wd1 = 0;
    wd2 = 0;

    const conditionCheck = () => {

      const mps = [...document.querySelectorAll('#movie_player')].filter(e => !e.closest('[hidden]'));
      if (mps.length !== 1) return false;

      const media = mps[0].querySelectorAll('audio, video');
      if (media.length !== 1) return false;

      return true;
    }

    let tryCount = 8;
    while (r = !conditionCheck()) {
      await new Promise(r => setTimeout_(r), 80);
      if (--tryCount <= 0) break;
    }

    if (!cid && !r) cid = setInterval_(timerFn, 250);

  }

  document.addEventListener('yt-page-data-fetched', (evt) => {
    const pageFetchedDataLocal = evt.detail;
    const m = pageFetchedDataLocal?.pageData?.playerResponse?.videoDetails?.lengthSeconds;
    triggerFn(m);
  }, false);

  document.addEventListener('durationchange', (evt) => {
    if (evt.target instanceof HTMLMediaElement) {
      triggerFn();
    }
  }, true);

})();