Greasy Fork

来自缓存

Greasy Fork is available in English.

XuetangX Keyboard Shortcut

增加学堂在线视频对快捷键的支持。

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         XuetangX Keyboard Shortcut
// @namespace    https://raineggplant.com/
// @version      0.0.2
// @description  增加学堂在线视频对快捷键的支持。
// @author       RainEggplant
// @match        *://next.xuetangx.com/*
// @homepageURL  https://github.com/RainEggplant/xuetangx-keyboard-shortcut
// ==/UserScript==

(function () {
  "use strict";
  const DEBUG = false;

  const videoSelector = "video";
  const wrapperSelector = "#qa-video-wrap";
  const timeStep = 10;

  const observer = new MutationObserver((mutation) => {
    const wrapper = document.querySelector(wrapperSelector);
    if (wrapper) {
      observer.disconnect();
      wrapper.setAttribute("tabindex", "-1");
      wrapper.addEventListener("keydown", keyboard_shortcut);
    }
  });

  // Start waiting for wrapper element
  observer.observe(document.body, {
    childList: true,
    subtree: true,
    attributes: false,
    characterData: false,
  });

  function keyboard_shortcut(e) {
    if (DEBUG) console.log(e.keyCode);
    const video = document.querySelector(videoSelector);
    let time;
    let volume;
    switch (e.keyCode) {
      case 32: // Space
        e.preventDefault();
        if (video.paused) video.play();
        else video.pause();
        break;
      case 37: // Arrow Left
        e.preventDefault();
        time = video.currentTime - timeStep;
        video.currentTime = time > 0 ? time : 0;
        break;
      case 39: // Arrow Right
        e.preventDefault();
        time = video.currentTime + timeStep;
        video.currentTime = time < video.duration ? time : video.duration;
        break;
    }
  }
})();