Fix Skip YouTube Ads Bug in Brave

For personal use only.

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

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

})();