dudueasy / video-control-by-apolo

// ==UserScript==
// @name video-control-by-apolo
// @namespace Violentmonkey Scripts
// @match *://xiedaimala.com/courses/*
// @grant none
// @license MIT
// @version  1.0.1
// ==/UserScript==
// 

(function () {
  //set video variable
  let video = function () {
    // method1  getVideo through selector
    //// please customize selectorPattern before using
    // let selectorPattern = 'div.el-collapse-item.is-active video'
    //
    // let currentVideo = document.querySelector(selectorPattern)

    // method2 getVideo through filtering video.offsetHeight
    let videos = document.querySelectorAll('video')
    if (videos[0]) {
      videos = Array.from(videos)
      let currentVideo = videos.filter((currentElement) => currentElement.offsetHeight !== 0)[0]

      // force video preload
      currentVideo.preload = 'auto'
      return currentVideo
    }
    else {
      // console.log('找不到video标签')
      return undefined
    }
  }

  //specify functions
  function vidPlayPause() {
    if (video().paused) {
      video().play();
    }
    else {
      video().pause();
    }
  }

  function vidForward(step) {
    video().currentTime += step;
  }

  function vidBackward(step) {
    video().currentTime -= step;
  }

  function volumeDown() {
    if (video().volume >= 0.1) {
      video().volume -= 0.1;
    }
  }

  function volumeUp() {
    if (video().volume <= 0.9) {
      video().volume += 0.1;
    }
  }

  function speedUp() {
    video().playbackRate += 0.1
    console.log(`current playback speed: ${video().playbackRate}`)
  }

  function speedDown() {
    if (video().playbackRate >= 0.5)
      video().playbackRate -= 0.1
    console.log(`current playback speed: ${video().playbackRate}`)
  }

  function fullScreen() {
    if (document.webkitIsFullScreen) {
      video().webkitExitFullScreen();
    }
    else {
      video().webkitRequestFullScreen();
    }
  }

  //global eventListener
  window.addEventListener('keydown', function (e) {
    // e.preventDefault()
    let key = e.keyCode;

    // 检验 event.keyCode 的类型
    //// console.log(key)
    //// console.log(typeof  key)

    // 初始化步进的秒数
    let defaultStep = 5
    let currentStep;

    if (video()) {
      // 如果 shift 键触发, 那么步进/布退秒数翻倍
      if (e.shiftKey === true) {
        currentStep = 2 * defaultStep
      }
      else {
        currentStep = defaultStep
      }

      //arrowLeft
      if (key === 37) {
        e.preventDefault()
        vidBackward(currentStep);
      }
      //arrowRight

      if (key === 39) {
        e.preventDefault()
        vidForward(currentStep);
      }

      //arrowUp
      if (key === 38) {
        e.preventDefault()
        volumeUp();
      }

      if (key === 40) {
        e.preventDefault()
        volumeDown();
      }

      //Space key
      if (key === 32) {
        e.preventDefault()
        vidPlayPause();
      }
      //F key
      if (key === 70) {
        e.preventDefault()
        fullScreen();
      }

      // . key
      if (key === 190) {
        e.preventDefault()
        speedUp()
      }

      // , key
      if (key === 188) {
        speedDown()
      }
    }
  });
})()