YouTube Live: Manual Fine Tune Video Time

7/5/2024

目前為 2024-07-05 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        YouTube Live: Manual Fine Tune Video Time
// @namespace   UserScripts
// @match       https://www.youtube.com/*
// @grant       none
// @version     0.1.0
// @author      CY Fung
// @description 7/5/2024
// @run-at      document-start
// @license     MIT
//
// ==/UserScript==

(() => {


  let byPassPlaybackRate = false;
  let tmpPlaybackRate = null;
  let byPassPlaybackRates = null; // [0.25, 0.5, 0.75, 1, 1.25, 1.5, 1.75, 2]

  let lastNavigateFinishTime = 0;

  if (typeof AbortSignal === 'undefined') return;

  let __requestAnimationFrame__ = typeof webkitRequestAnimationFrame === 'function' ? window.webkitRequestAnimationFrame.bind(window) : window.requestAnimationFrame.bind(window);

  let instance = null;

  /** @type {globalThis.PromiseConstructor} */
  const Promise = (async () => { })().constructor; // YouTube hacks Promise in WaterFox Classic and "Promise.resolve(0)" nevers resolve.


  const PromiseExternal = ((resolve_, reject_) => {
    const h = (resolve, reject) => { resolve_ = resolve; reject_ = reject };
    return class PromiseExternal extends Promise {
      constructor(cb = h) {
        super(cb);
        if (cb === h) {
          /** @type {(value: any) => void} */
          this.resolve = resolve_;
          /** @type {(reason?: any) => void} */
          this.reject = reject_;
        }
      }
    };
  })();


  const observablePromise = (proc, timeoutPromise) => {
    let promise = null;
    return {
      obtain() {
        if (!promise) {
          promise = new Promise(resolve => {
            let mo = null;
            const f = () => {
              let t = proc();
              if (t) {
                mo.disconnect();
                mo.takeRecords();
                mo = null;
                resolve(t);
              }
            }
            mo = new MutationObserver(f);
            mo.observe(document, { subtree: true, childList: true })
            f();
            timeoutPromise && timeoutPromise.then(() => {
              resolve(null)
            });
          });
        }
        return promise
      }
    }
  }

  let fc = 0;

  const pd = Object.getOwnPropertyDescriptor(HTMLMediaElement.prototype, 'playbackRate');

  const isPassiveArgSupport = (typeof IntersectionObserver === 'function');
  const bubblePassive = isPassiveArgSupport ? { capture: false, passive: true } : false;
  const capturePassive = isPassiveArgSupport ? { capture: true, passive: true } : true;

  const insp = o => o ? (o.polymerController || o.inst || o || 0) : (o || 0);

  let pageFetchedDataLocal = null;
  document.addEventListener('yt-page-data-fetched', (evt) => {
    pageFetchedDataLocal = evt.detail;

  }, bubblePassive);


  function getFormatDates() {

    if (!pageFetchedDataLocal) return null;

    const formatDates = {}
    try {
      formatDates.publishDate = pageFetchedDataLocal.pageData.playerResponse.microformat.playerMicroformatRenderer.publishDate
    } catch (e) { }
    // 2022-12-30

    try {
      formatDates.uploadDate = pageFetchedDataLocal.pageData.playerResponse.microformat.playerMicroformatRenderer.uploadDate
    } catch (e) { }
    // 2022-12-30

    try {
      formatDates.publishDate2 = pageFetchedDataLocal.pageData.response.contents.twoColumnWatchNextResults.results.results.contents[0].videoPrimaryInfoRenderer.dateText.simpleText
    } catch (e) { }
    // 2022/12/31

    if (typeof formatDates.publishDate2 === 'string' && formatDates.publishDate2 !== formatDates.publishDate) {
      formatDates.publishDate = formatDates.publishDate2
      formatDates.uploadDate = null
    }

    try {
      formatDates.broadcastBeginAt = pageFetchedDataLocal.pageData.playerResponse.microformat.playerMicroformatRenderer.liveBroadcastDetails.startTimestamp
    } catch (e) { }
    try {
      formatDates.broadcastEndAt = pageFetchedDataLocal.pageData.playerResponse.microformat.playerMicroformatRenderer.liveBroadcastDetails.endTimestamp
    } catch (e) { }
    try {
      formatDates.isLiveNow = pageFetchedDataLocal.pageData.playerResponse.microformat.playerMicroformatRenderer.liveBroadcastDetails.isLiveNow
    } catch (e) { }


    return formatDates;
  }

  const promiseVideoNextFn = async (video, v) => {

    if (typeof video.requestVideoFrameCallback === 'function' && v !== false) {

      return true === await Promise.race([
        new Promise(resolve => video.requestVideoFrameCallback(() => {
          resolve(true);
        })),
        new Promise(resolve => setTimeout(resolve, 1000))
      ]);

    } else {

      return true === await Promise.race([
        new Promise(resolve => video.addEventListener('timeupdate', () => {
          resolve(true);
        }, { once: true, passive: true, capture: false })),
        new Promise(resolve => setTimeout(resolve, 1000))
      ]);

    }



  }

  let promisePR1;

  let videoTarget = null;


  let pr01 = new PromiseExternal();
  let pr02 = new PromiseExternal();

  document.addEventListener('durationchange', function (evt) {

    const target = (evt || 0).target;
    if (!(target instanceof HTMLVideoElement)) return;

    if (target.classList.contains('video-stream') && target.classList.contains('html5-main-video')) {

      videoTarget = target;

      if (target.duration !== 3600 && target.duration > 120) {

        pr01.resolve();
        pr01 = new PromiseExternal();

      }

    }


  }, true);

  let liveVideo = null;


  async function run(videoTarget) {

    try {

      if (fc > 1e9) fc = 9;
      let tc = ++fc;

      const timeout = new Promise(r => setTimeout(r, 1000));
      const video = videoTarget;
      // const video = watchPage.querySelector('video.video-stream.html5-main-video');
      if (!video) return false;


      const fn = () => {
        if (video.paused || !video.isConnected || video.networkState !== 2 || video.readyState !== 4) return false;
        return video.currentTime > 0.1 && video.duration > 0.1 && instance;
      }
      if (!fn()) {
        await observablePromise(fn, timeout).obtain();
      }

      if (tc !== fc) return false;

      await new Promise(resolve => video.addEventListener('timeupdate', () => {
        resolve();
      }, { once: true, passive: true, capture: false }));

      if (tc !== fc) return false;


      if (!instance || instance.getPlayerState() !== 1) return false;

      liveVideo = video;


    } catch (e) {
      console.log(e)
    }


  }

  const speedmasterUserEduWM = new WeakMap();
  Object.defineProperty(Object.prototype, 'speedmasterUserEdu', {

    get() {
      return speedmasterUserEduWM.get(this)
    },

    set(nv) {
      speedmasterUserEduWM.set(this, nv);
    },

    enumerable: false,
    configurable: true

  })

  let nnId = 0;
  document.addEventListener('yt-navigate-finish', () => {
    const t = ++nnId;
    setTimeout(() => {
      if (t !== nnId) return;
      if ([...document.querySelectorAll('ytd-page-manager#page-manager #movie_player .html5-main-video[src]')].filter(e => !e.closest('[hidden]')).length === 1) {
        pr02.resolve();
        pr02 = new PromiseExternal();
        lastNavigateFinishTime = Date.now();
      }
    }, 800);
  }, false);


  (async () => {
    try {
      let lastSrc = null;
      while (1) {

        await Promise.all([pr01, pr02]);
        const tr01 = pr01 = new PromiseExternal();
        const tr02 = pr02 = new PromiseExternal();

        await new Promise(resolve => __requestAnimationFrame__(resolve));

        if (!videoTarget || !videoTarget.isConnected) continue;

        if (pr01 !== tr01 || pr02 !== tr02) continue;

        if (Date.now() - lastNavigateFinishTime > 4000) continue;

        if (!pageFetchedDataLocal) continue;
        const dates = getFormatDates();
        if (!dates) continue;
        if (dates.broadcastBeginAt && !dates.broadcastEndAt && dates.isLiveNow === true) {
        } else {
          continue;
        }

        let src = videoTarget.src;

        if (lastSrc !== src) {
          lastSrc = src;

          run(videoTarget).then(res => {
            if (res === false) lastSrc = null;
          });
        }

      }
    } catch (e) {
      console.log(e);
    }
  })();


  const _yt_player_observable = observablePromise(() => {
    return (((window || 0)._yt_player || 0) || 0);
  });

  (async () => {

    const _yt_player = await _yt_player_observable.obtain();

    if (!_yt_player || typeof _yt_player !== 'object') return;

    const addProtoToArr = (parent, key, arr) => {

      let isChildProto = false;
      for (const sr of arr) {
        if (parent[key].prototype instanceof parent[sr]) {
          isChildProto = true;
          break;
        }
      }

      if (isChildProto) return;

      arr = arr.filter(sr => {
        if (parent[sr].prototype instanceof parent[key]) {
          return false;
        }
        return true;
      });

      arr.push(key);

      return arr;


    }

    const getGU = (_yt_player) => {

      const w = 'GU';

      let arr = [];

      for (const [k, v] of Object.entries(_yt_player)) {


        const p = typeof v === 'function' ? v.prototype : 0;

        if (p.getPlaybackRate && p.setPlaybackRate) {
          console.log(p.setPlaybackRate?.length, p.getPlaybackRate?.length, p.getVideoUrl?.length, p.getCurrentTime?.length, p.getDuration?.length)
        }
        if (p
          && typeof p.setPlaybackRate === 'function' && p.setPlaybackRate.length === 2
          && typeof p.getPlaybackRate === 'function' && p.getPlaybackRate.length === 0

          // && typeof p.isAtLiveHead === 'function'

          && typeof p.getVideoUrl === 'function' && p.getVideoUrl.length === 4
          && typeof p.getCurrentTime === 'function' && p.getCurrentTime.length >= 2
          && typeof p.getDuration === 'function' && p.getDuration.length == 2


        ) {
          arr = addProtoToArr(_yt_player, k, arr) || arr;


        }

      }

      /**
       *
    g.k.getCurrentTime = function(a, b, c) {
        var d = this.getPlayerState(a);
        if (this.app.getAppState() === 2 && d === 5) {
            var e;
            return ((e = this.app.getVideoData()) == null ? void 0 : e.startSeconds) || 0
        }
        return this.L("web_player_max_seekable_on_ended") && d === 0 ? zhb(this.app, a) : a ? this.app.getCurrentTime(a, b, c) : this.app.getCurrentTime(a)
    }
       *
       */


      // console.log(1222, arr.map(k=> Object.keys(_yt_player[k].prototype).sort()))

      if (arr.length === 0) {

        console.warn(`Key does not exist. [${w}]`);
      } else {

        console.log(`[${w}]`, arr);
        return arr[0];
      }




    }

    const key = getGU(_yt_player);

    // console.log(1233, key)
    const g = _yt_player;
    const k = key;
    const gk = g[k];
    const gkp = g[k].prototype;


    gkp.getPlaybackRate322 = gkp.getPlaybackRate;
    gkp.getPlaybackRate = function () {

      // console.log(5556, this.getPlaybackRate322)
      instance = this;
      return this.getPlaybackRate322();
    }

    gkp.setPlaybackRate322 = gkp.setPlaybackRate;
    gkp.setPlaybackRate = function (a, b) {
      instance = this;
      if (byPassPlaybackRate) {
        // console.log(5888, 12333,a, b)
        // return;
      }
      // console.log(5388, arguments)
      return this.setPlaybackRate322(a, b);
    }

    if (typeof gkp.getAvailablePlaybackRates === 'function' && typeof gkp.getAvailablePlaybackRates322 !== 'function') {


      gkp.getAvailablePlaybackRates322 = gkp.getAvailablePlaybackRates;
      gkp.getAvailablePlaybackRates = function () {
        instance = this;
        if (byPassPlaybackRates) {
          // console.log(5888, 12333,a, b)
          return byPassPlaybackRates;
        }
        // console.log(5388, arguments)
        return this.getAvailablePlaybackRates322();
      }
      //[0.25, 0.5, 0.75, 1, 1.25, 1.5, 1.75, 2]



    }


  })();

  Storage.prototype.setItem322 = Storage.prototype.setItem;
  Storage.prototype.setItem = function (a, b) {

    if (a === 'yt-player-playback-rate') {
      tmpPlaybackRate = b;
      // if(!byPassPlaybackRate) debugger
      if (promisePR1 && b && typeof b === 'string' && b.indexOf('{"data":"1"') >= 0) {

        promisePR1.resolve();
        promisePR1 = null;
      }
      // console.log(5883, a, b,byPassPlaybackRate, 'XX_'+tmpPlaybackRate+'_', location.pathname)
      if (window.location.pathname === '/live_chat') return;
    }

    if (byPassPlaybackRate && a === 'yt-player-playback-rate') return;
    this.setItem322(a, b);

  }


  Storage.prototype.getItem322 = Storage.prototype.getItem;
  Storage.prototype.getItem = function (a) {

    if (a === 'yt-player-playback-rate') {
      if (window.location.pathname === '/live_chat') return null;
      if (typeof tmpPlaybackRate === 'string') return tmpPlaybackRate;
    }

    if (a === 'yt-player-playback-rate' && typeof tmpPlaybackRate === 'string') return tmpPlaybackRate;
    return this.getItem322(a);

  }

  Object.defineProperty(Storage.prototype, 'yt-player-playback-rate', {
    get() {
      return this.getItem('yt-player-playback-rate');
    },
    set(nv) {
      this.setItem('yt-player-playback-rate', nv);
      return true;
    },
    enumerable: true,
    configurable: true
  });

  let onKey = false;

  let lrRates = null;

  const lrRatesSetup = () => {
    if (lrRates || !instance) return;
    const rates = instance.getAvailablePlaybackRates();
    lrRates = [Math.max(...rates.filter(r => r < 1)), Math.min(...rates.filter(r => r > 1))];
  }

  const setRate = (r) => {

    instance && instance.setPlaybackRate(r, r)
  }

  const filterSet = (b) => {
    if (!liveVideo) return;
    b ? (liveVideo.style.filter = 'contrast(0.5)') : (liveVideo.style.filter = '')
  }

  const targetOK = (target) => {
    if (!target || target instanceof Document) return true;
    if (target instanceof HTMLInputElement || target instanceof HTMLTextAreaElement) return false;
    if (target instanceof Element && target.closest('[contenteditable]') instanceof Node) return false;
    return true;
  }

  document.addEventListener('keydown', evt => {
    if (evt.code === 'ArrowLeft' && instance && !onKey && liveVideo && !liveVideo.paused && targetOK(evt.target)) {
      if (!onKey) {

        onKey = 'left'
        console.log('left')
        lrRatesSetup();
        setRate(lrRates[0]);
        filterSet(1)
      }
      // evt.preventDefault();
      evt.stopImmediatePropagation();
      evt.stopPropagation();
    } else if (evt.code === 'ArrowRight' && instance && liveVideo && !liveVideo.paused && targetOK(evt.target)) {
      if (!onKey) {

        onKey = 'right'
        console.log('right')
        lrRatesSetup();
        setRate(lrRates[1]);
        filterSet(1)
      }
      // evt.preventDefault();
      evt.stopImmediatePropagation();
      evt.stopPropagation();
    }
  }, true);


  document.addEventListener('keyup', evt => {
    if (evt.code === 'ArrowLeft' && instance && onKey === 'left') {
      console.log('left end')
      // evt.preventDefault();
      evt.stopImmediatePropagation();
      evt.stopPropagation();
      setRate(1);
      filterSet(0)
    } else if (evt.code === 'ArrowRight' && instance && onKey === 'right') {
      console.log('right end')
      // evt.preventDefault();
      evt.stopImmediatePropagation();
      evt.stopPropagation();
      setRate(1);
      filterSet(0)
    }
    onKey = false;
  }, true);




})();