Greasy Fork

来自缓存

Greasy Fork is available in English.

超星键盘控制优化

上下控制音量;左右控制视频进度条;空格播放\暂停视频;'+'、'-'控制倍速;'['、']'控制上一节、下一节;'alt'+'[1-9]' 选择快速选择整数倍速;'f'全屏;'ESC'退出全屏;若无效请单击播放窗口一次再尝试,若仍无效请刷新页面;仍有bug请留言反馈

当前为 2021-06-14 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         超星键盘控制优化
// @namespace    http://greasyfork.icu/zh-CN/users/782923-asea
// @version      1.4.1
// @description  上下控制音量;左右控制视频进度条;空格播放\暂停视频;'+'、'-'控制倍速;'['、']'控制上一节、下一节;'alt'+'[1-9]' 选择快速选择整数倍速;'f'全屏;'ESC'退出全屏;若无效请单击播放窗口一次再尝试,若仍无效请刷新页面;仍有bug请留言反馈
// @author       Asea Q:569389750
// @match        https://mooc1-1.chaoxing.com/mycourse/studentstudy*
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        none
// @license      GPL-3.0-only
// ==/UserScript==

(function() {
  var errors = 0;
  mainfn_flag = false // 主定时任务标志
  var old_url = window.location.href; // 获取当前url,方便后面换课时调用
  if(mainfn_flag===false)
  {
    mainfn = setInterval(mianKeybind, 500);
    mainfn_flag = true;
  }
  function changeURLArg(url,arg,arg_val) // 获取url某个属性的value
  {   old_arg_val = getArgVal(url,arg).toString()
      newUrl = url.replace(old_arg_val,arg_val)
      return newUrl
    }
  function getArgVal(url,arg){ // 更改某个属性的value
    patch = arg+'=([^&]*)'
    arg_val = url.match(patch)[1]
    return arg_val
  }
  function mianKeybind(){

      if(document.readyState == 'complete') // 判断网页资源加载完毕
      { 
        urlfn = setInterval(url_listener, 1000)  // 监听url变化
        var vol = 0.1;  //1代表100%音量,每次增减0.1
        var time = 5; //单位秒,每次增减5秒
        var rate = 0.2; // 倍速增长量,倍速最低0.2,最高16
        try
        {
          var iframes = document.getElementById("iframe").contentWindow.document.querySelectorAll("iframe"); // 获取到页面内所有iframe,ppt以及video的iframe是在一个大的iframe里的
          iframes.forEach(iframe=>
            {
              if(iframe.contentWindow.document.getElementsByTagName('video').length > 0) // 判断是否含有video标签
              {
                videoElement = iframe.contentWindow.document.querySelector('video'); // 定位到video标签
                videoElement.onclick = function(event) // 绑定鼠标点击事件
                { 
                  videoElement = event.currentTarget // 定位当前元素
                  videoElement.onkeydown = keybind // 绑定键盘事件
                  function keybind(event)   
                  {//键盘事件
                    var e = event || window.event || arguments.callee.caller.arguments[0];
                    //鼠标上下键控制视频音量
                    if (e && e.key === 'ArrowUp') 
                    {
    
                      // 按 向上键
                      videoElement.volume !== 1 ? videoElement.volume += vol : 1;
                      return false;
      
                    } else if (e && e.key === 'ArrowDown') 
                    {
      
                      // 按 向下键
                      videoElement.volume !== 0 ? videoElement.volume -= vol : 1;
                      return false;
      
                    } else if (e && e.key === 'ArrowLeft') 
                    {
      
                      // 按 向左键
                      videoElement.currentTime !== 0 ? videoElement.currentTime -= time : 1;
                      return false;
      
                    } else if (e && e.key === 'ArrowRight') 
                    {
      
                        // 按 向右键
                        videoElement.volume !== videoElement.duration ? videoElement.currentTime += time : 1;
                        return false;
      
                    } else if (e && e.key === ' ') 
                    {
      
                      // 按空格键 判断当前是否暂停
                      videoElement.paused === true ? videoElement.play() : videoElement.pause();
                      return false;
                    } else if(e &&(e.key === '=' || e.key === '+')) 
                    {
                      // 按加号键 倍速增加
                      videoElement.playbackRate > 0 && videoElement.playbackRate < 16 ? videoElement.playbackRate = (videoElement.playbackRate+rate).toFixed(1) : 1;
                      return false;
                    } else if(e && e.key === '-') 
                    {
                      // 按减号键 倍速减少
                      videoElement.playbackRate > rate ? videoElement.playbackRate = (videoElement.playbackRate-rate).toFixed(1) : 1;
                      return false;
                    } else if(e && e.altKey && '123456789'.search(e.key) != -1) 
                    {
                      // 整数倍速
                      videoElement.playbackRate > 0 && videoElement.playbackRate < 16 ? videoElement.playbackRate = Number(e.key) : 1;
                      return false;

                    } else if(e && e.key === ']') // 下一节
                    {
                      url = window.location.href
                      now_chapterId = getArgVal(url,'chapterId')
                      next_chapterId = (Number(now_chapterId)+1).toString()
                      window.location.href = changeURLArg(url,'chapterId',next_chapterId)
                    } else if(e && e.key === '[') // 上一节
                    {
                      url = window.location.href
                      now_chapterId = getArgVal(url,'chapterId')
                      next_chapterId = (Number(now_chapterId)-1).toString()
                      window.location.href = changeURLArg(url,'chapterId',next_chapterId)
                    } else if(e && e.key === 'f') // 全屏
                    {
                        if (videoElement.requestFullscreen) {
                          videoElement.requestFullscreen();
                      } else if (videoElement.mozRequestFullScreen) {
                          videoElement.mozRequestFullScreen();
                      } else if (videoElement.webkitRequestFullScreen) {
                          videoElement.webkitRequestFullScreen();
                      }
                    }
                  };
                }
              }
              
          });
          // console.log('变量设置完毕')
          document.getElementById("iframe").contentWindow.document.querySelector("iframe").contentWindow.document.querySelector('.vjs-big-play-button').click() // 自动播放
          console.log('Done') // 总鼠标事件绑定完毕
          clearInterval(mainfn); // 关闭加载元素的定时监听
          mainfn_flag = false; // 主定时任务停止的标志 方便开启
        }
        catch
        { 
          errors += 1
          if(errors < 20){
          console.log('Loading') // 元素未加载出来时
          }
          else if (errors > 20){ 
            console.log('未发现video元素,已关闭监听')
            clearInterval(mainfn)
          }
        }
      }
    }
    
    function url_listener()
    { 
      var now_url = window.location.href; // 当前url
      if (now_url !== old_url) // 判断是否换节课
      { 
        errors = 0
        clearInterval(mainfn)
        console.log('发现url切换')
        old_url = now_url;
        mainfn = setInterval(mianKeybind, 500); // 开启主定时任务
        clearInterval(urlfn); // 关闭url监听定时任务
      }
    }
})();