Shift Space Toggle Youtube CE Element

This is to toggle annoying Youtube CE Element near the end of the video

目前為 2022-08-14 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Shift Space Toggle Youtube CE Element
// @namespace    http://tampermonkey.net/
// @version      0.5
// @description  This is to toggle annoying Youtube CE Element near the end of the video
// @author       CY Fung
// @match        https://www.youtube.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @grant        none
// @license      MIT
// ==/UserScript==

/* jshint esversion:6 */
(function() {
  'use strict';

  let videoElm = null;
  const ceElems = [];
  let isPassiveAvailable = null

  let earliestShown = -1;
  let endTime = -1;

  const WeakRef = window?.WeakRef;

  let videoReady = false

  let lastPress = -1
  const allowList = [
        'DIV', 'SPAN', 'BODY', 'HTML', 'VIDEO', 'A',
        'YTD-PLAYER', 'YTD-WATCH-FLEXY', 'YTD-PAGE-MANAGER', 'YTD-MINIPLAYER'
    ];

  function cacheCEElems() {

    const m = document.querySelectorAll('ytd-player#ytd-player .ytp-ce-element');
    if (m.length === 0) return false;

    ceElems.length = 0;
    for (const elm of m) {
      ceElems.push(new WeakRef(elm))
    }

    return true;

  }

  function videoTimeUpdate(evt) {

    //passive = true
    //capture = false

    if (!videoReady) {
      if (evt?.target?.matches('ytd-player#ytd-player video')) {
        videoReady = true
        if (cacheCEElems() === false) setTimeout(cacheCEElems, 180);
      }
    }

    if (ceElems.length === 0) return;

    const video = evt.target;
    const anyShown = ceElems.some((elmRef) => elmRef.deref()?.classList?.contains('ytp-ce-element-show') === true)
    //console.log(135, anyShown, video.currentTime)
    if (anyShown) {
      if (earliestShown > 0 && -earliestShown < -video.currentTime) {
        earliestShown = video.currentTime
        endTime = video.duration
      } else if (earliestShown < 0) {
        videoElm = new WeakRef(video);
        earliestShown = video.currentTime
        endTime = video.duration
      }
    }


  }

  function initialForVideoDetection(evt) {

    //passive = true
    //capture = true

    const video = evt?.target;
    if (video?.nodeName === 'VIDEO' && typeof video.src == 'string') {} else return;

    videoReady = false;
    ceElems.length = 0;

    videoElm = null;
    earliestShown = -1;
    endTime = -1;


    new Promise(function() {


      if (video.hasAttribute('usr-video-cem')) return;
      video.setAttribute('usr-video-cem', '');

      if (isPassiveAvailable === null) isPassiveAvailable = checkPassive();
      video.addEventListener('timeupdate', videoTimeUpdate, optCapture(true, false));

    })

  }

  function pageKeyDownfunction(evt) {
    //passive = false
    //capture = true

    if (evt.code === 'Space' && evt.shiftKey) {

      if (!allowList.includes(evt.target.nodeName)) return;

      if (endTime > earliestShown && earliestShown > 0) {

        let video = videoElm?.deref();

        if (!video) return;

        let p = (video.currentTime - earliestShown) / (endTime - earliestShown);
        if (p >= 0 && p <= 1) {
          evt.preventDefault();
          evt.stopPropagation();
          evt.stopImmediatePropagation();

          for (const ceElem of ceElems) {
            ceElem?.deref()?.classList.toggle('ytp-ce-element-show');
          }
        }

      }

    }
  }

  function checkPassive() {

    let passiveSupported = false;

    try {
      const options = {
        get passive() {
          passiveSupported = true;
          return false;
        }
      };

      window.addEventListener("test", null, options);
      window.removeEventListener("test", null, options);
    } catch (err) {
      passiveSupported = false;
    }

    return passiveSupported

  }

  const optCapture = (passive, capture) => isPassiveAvailable ? { passive, capture } : capture;

  new Promise(function() {

    if (isPassiveAvailable === null) isPassiveAvailable = checkPassive();
    document.addEventListener('canplay', initialForVideoDetection, optCapture(true, true));

  })

  document.addEventListener('keydown', pageKeyDownfunction, true)

  //ytp-ce-video ytp-ce-top-left-quad ytp-ce-size-853 ytp-ce-element-show

  // Your code here...
})();