Greasy Fork is available in English.
上下控制音量;左右控制视频进度条;空格播放\暂停视频;'+'、'-'控制倍速;'['、']'控制上一节、下一节;'alt'+'[1-9]' 选择快速选择整数倍速;'f'全屏;'ESC'退出全屏;若无效请单击播放窗口一次再尝试,若仍无效请刷新页面;仍有bug请留言反馈
当前为
// ==UserScript==
// @name 超星键盘控制优化
// @namespace http://greasyfork.icu/zh-CN/users/782923-asea
// @version 1.4
// @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
// ==/UserScript==
(function() {
mainfn_flag = false // 主定时任务标志
window.onload =()=>
{
if(mainfn_flag===false)
{
mainfn = setInterval(mianKeybind, 1000); // 开启主定时任务,监听元素是否加载,若加载出来则绑定事件
mainfn_flag = true;
} // 用户单击本节课程时,url不会改变,但是iframe会重新加载,无法重新绑定事件,是目前的一个bug
}
var old_url = window.location.href; // 获取当前url,方便后面换课时调用
if(mainfn_flag===false)
{
mainfn = setInterval(mianKeybind, 1000);
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, 2000) // 监听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
{
console.log('Loading') // 元素未加载出来时
}
}
}
function url_listener()
{
var now_url = window.location.href; // 当前url
if (now_url !== old_url) // 判断是否换节课
{
old_url = now_url;
mainfn = setInterval(mianKeybind, 500); // 开启主定时任务
clearInterval(urlfn); // 关闭url监听定时任务
}
}
})();