Greasy Fork

Greasy Fork is available in English.

GaoXiaoKaoShi高校考试网自动刷视频

自动刷高校考试网视频

当前为 2023-04-05 提交的版本,查看 最新版本

// ==UserScript==
// @name         GaoXiaoKaoShi高校考试网自动刷视频
// @namespace    https://github.com/AliubYiero/TemperScripts/GaoXiaoKaoShiVideoSkip
// @version      beta0.3
// @description  自动刷高校考试网视频
// @author       Yiero

// @match        http://www.gaoxiaokaoshi.com/*
// @license      MIT
// ==/UserScript==

// 因为每次点击/关闭视频高校考试网都会刷新网页,所以控制台会有一堆报错,但是勉强能用

/* 设置当前局部变量 */
/* 当前学习视频信息表
* [0]课程名称 [1]所属分类 [2]学时要求 [3]已完成学时 [4]状态 [5]开始学习 [6]课后练习
*  */
let videoInfoTable = null	// 当前学习视频信息表

let video = null			// 视频对象
let mainWindow = null		// 课程列表iframeWindow对象
let examViewWindow = null	// 视频对象iframeWindow对象

let timer = null			// 视频播放定时器
let checkVideoCounter = 0	// 视频播放检查次数容器

localStudyingObj = {}	// 当前对话储存

// 获取视频状态
function getStudyState() {
	// 重定向window到内层iframe
	const mainIframe = window.document.getElementById("mainIframe")
	mainWindow = mainIframe.contentWindow

	// 绑定视频信息总表单
	const videoInfoList = mainWindow.document.querySelectorAll('tr')

	// 判断当前学习列表
	let localStudyingObj
	if (!sessionStorage.studying) {
		localStudyingObj = {}
	} else {
		localStudyingObj = JSON.parse(sessionStorage.studying)
	}

	for (let i = 1; i < videoInfoList.length; i++) {
		const state = videoInfoList[i].children[4]

		// 跳过`已完成`视频
		if (state.innerText === "已完成") {
			continue
		}

		// 跳过当前正在学习视频
		if (localStudyingObj[videoInfoList[i].children[0].innerText]) {
			continue
		}

		// 返回未学习完的视频列
		videoInfoTable = videoInfoList[i].children
		return
	}

	console.log("进入下一页")
	/* 当前页所有视频均学习完,模拟点击进入下一页 */
	// 判断当前页是否为最后一页(结束递归)
	const page = mainWindow.document.querySelectorAll('.page > .fright > ul > li')[0].innerText
	const pages = page.split('/')
	if (pages[0] === pages[1]) {
		console.log("已经是最后一页,全部视频观看完成")
		return;
	}
	// 点击进入下一页
	mainWindow.document.getElementById('PageSplit1_BtnNext').click()

	// 重新加载新一页的视频状态
	setTimeout(() => {
		// 重定向window到内层iframe
		const mainIframe = document.getElementById("mainIframe")
		mainWindow = mainIframe.contentWindow

		getStudyState()
	}, 3000)
}

// 点击进入视频
function enterVideo() {
	if (!videoInfoTable) getStudyState()

	/* 通过`setVideoStartEvent`函数绑定事件打开视频 */
	// 绑定用户手动点击视频事件
	setVideoStartEvent()

	// 模拟点击开始播放视频
	videoInfoTable[5].children[0].click()

}

function getVideoObj() {
	// 重定向window到视频对象iframeWindow对象
	const examViewIframe = document.getElementById("ExamView")
	examViewWindow = examViewIframe.contentWindow

	video = examViewWindow.document.querySelector('video')
	setVideoEndedEvent()
	timer = setInterval(checkVideoPlaying, 1000)
}

// 检查视频是否正在播放
function checkVideoPlaying() {
	if (checkVideoCounter++ >= 8) {
		clearInterval(timer)
		timer = null
		return
	}

	if (video.paused) {
		video.play()	// 播放视频
		// 设置当前视频应该开始的时间(已学习学时(min)-1)
		const hasReadTime = videoInfoTable[3].innerText.split("分钟")[0]
		setTimeout(() => video.currentTime = (hasReadTime - 1) * 60, 1000)
		video.volume = 0	// 将音量设置为0

		examViewWindow.document.querySelector('#J_prismPlayer').click()	// 模拟点击聚焦视频,防止视频卡住
		console.log(`开始自动播放视频 「${videoInfoTable[0].innerText}」`)
		clearInterval(timer)
		timer = null
		return
	}
}

// 绑定视频播放结束事件
function setVideoEndedEvent() {
	if (!video) enterVideo()
	function reloadVideo() {
		// 关闭视频
		window.document.querySelector('a.fright').click()
		// 刷新网页更新缓存
		location.reload();

		// 清除视频信息
		checkVideoCounter = 0
		examViewWindow = null
		video = null

		// 播放下一个视频
		setTimeout(startVideo, 1000)
	}

	// 视频暂停时,再次播放
	video.addEventListener('pause', () => {
		// 重新刷新视频
		reloadVideo()
	})

	// 视频结束时,播放下一个视频
	video.addEventListener('ended', () => {
		reloadVideo()
		console.log(`「${videoInfoTable[0].innerText}」视频结束`)
	})
}

// 绑定点击事件
function setVideoStartEvent() {
	mainWindow.document.addEventListener('click', e => {
		if (!e.target.classList.contains('btn_4')) return

		setTimeout(() => {
			// 获取视频对象
			getVideoObj()
			// 重新绑定视频事件
			setVideoEndedEvent()
		}, 3000)

	})
}

// 开始获取视频信息
function startVideo() {
	// 获取需要学习的视频信息表
	getStudyState()
	// 开始播放视频
	enterVideo()
	// 写入`sessionStorage`
	localStudyingObj[`${videoInfoTable[0].innerText}`] = 1
	sessionStorage.studying = JSON.stringify(localStudyingObj)
	// 绑定视频事件
	setVideoEndedEvent()

	console.log(JSON.parse(sessionStorage.studying))
	if (Object.keys(JSON.parse(sessionStorage.studying)).length <= 5) {
		// 打开新页面开始播放新视频
		window.open(window.document.URL, "_blank")
	}
}

// 主函数
;(function () {
	setTimeout(() => {
		// 退出错误URL地址
		if (window.document.URL.includes('Study')) return

		// 开始播放视频
		startVideo()
	}, 2000)
})()