Fix Skip YouTube Ads Bug in Brave

For personal use only.

As of 26/12/2024. See the latest version.

// ==UserScript==
// @name        Fix Skip YouTube Ads Bug in Brave
// @namespace   Violentmonkey Scripts
// @match       https://www.youtube.com/*
// @version     0.0.001
// @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_] = [setInterval, clearInterval];

  let md1 = '';
  let md2 = 0;
  let qt = 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 < 0.0001 && md.duration > 5 && md.duration < qt + 5) {
      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;
    }

  };

  document.addEventListener('yt-page-data-fetched', (evt) => {
    const pageFetchedDataLocal = evt.detail;
    const m = pageFetchedDataLocal?.pageData?.playerResponse?.videoDetails?.lengthSeconds;
    if (m > 0) {
      cid && clearInterval_(cid);
      qt = m;
      cid = setInterval_(timerFn, 250);
    }
  }, false);

})();