Fix Skip YouTube Ads Bug in Brave

For personal use only.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

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.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==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);

})();