Greasy Fork

电子科技大学教务助手

电子科技大学教务助手帮你更便捷地使用教务系统

当前为 2021-07-11 提交的版本,查看 最新版本

// ==UserScript==
// @name         电子科技大学教务助手
// @namespace    http://shawroger.gitee.io/
// @version      0.0.2
// @description  电子科技大学教务助手帮你更便捷地使用教务系统
// @author       shawroger
// @match        *://eams.uestc.edu.cn/eams/*
// @require      https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js
// @icon         https://www.uestc.edu.cn/favicon.ico
// ==/UserScript==

function initStorage() {
	let initState = 1;
	if (
		window.localStorage.getItem("UESTC_STUDYSYS_HELPER_SHOWBAR_ALL") === null
	) {
		window.localStorage.setItem("UESTC_STUDYSYS_HELPER_SHOWBAR_ALL", "1");
		return initState;
	} else {
		try {
			initState = JSON.parse(
				window.localStorage.getItem("UESTC_STUDYSYS_HELPER_SHOWBAR_ALL")
			);
		} catch (e) {
			initState = 1;
			window.localStorage.setItem("UESTC_STUDYSYS_HELPER_SHOWBAR_ALL", "1");
		} finally {
			return initState;
		}
	}
}

let loopVars = {
	renderPlan: true,
	renderScore: true,
};

const itemConfig = [
	{
		text: "查询平时成绩",
		img: "https://pic.imgdb.cn/item/60ead7fd5132923bf8f5b136.png",
		href: "http://eams.uestc.edu.cn/eams/teach/grade/usual/usual-grade-std.action",
	},
	{
		text: "快速查询成绩",
		img: "https://pic.imgdb.cn/item/60ead77f5132923bf8f3f56b.png",
		href: "http://eams.uestc.edu.cn/eams/teach/grade/course/person.action",
	},
	{
		text: "审阅我的计划",
		img: "https://pic.imgdb.cn/item/60eadacf5132923bf8ffcca2.png",
		href: "http://eams.uestc.edu.cn/eams/myPlanCompl.action",
	},
];

function hrefContains(url) {
	return window.location.href.includes(url);
}

// 跳转首页
function toHome() {
	window.location.href = `http://eams.uestc.edu.cn/eams`;
}

// 判断是否在选课页面
function isStdElectCourse() {
	return window.location.href.includes("stdElectCourse");
}

// 注入全局 CSS 样式
function injectCSS() {
	const head = $("head");
	const css = `
	<style type="text/css">
		.uestc-helper-box {
			padding: 5px 10px;
			background: whiteSmoke;
			display: flex;
			justify-content: space-between;
			border-bottom: solid grey thin;
		}

		#reset_btn {
			margin-right: 15px;
		}

		#helper-text {
			font-size: 16px;
			padding-left: 10px;
		}

		.list_box_1 ul .li_1 {
			margin-bottom: 20px;
		}

		.btn-group {
			display: flex;
			justify-content: center;
			align-items: center;
			padding: 5px;
		}
	</style>`;
	head.append(css);
}

function bindAction() {
	const resetBtn = $("#reset_btn");
	resetBtn.click(toHome);

	const closeBtn = $("#close_btn");
	closeBtn.click(() => {
		$(".uestc-helper-box").hide();
		window.localStorage.setItem("UESTC_STUDYSYS_HELPER_SHOWBAR_ALL", "0");
		toHome();
	});

	const showBtn = $("#show_btn");
	showBtn.click(() => {
		$(".uestc-helper-box").show();
		window.localStorage.setItem("UESTC_STUDYSYS_HELPER_SHOWBAR_ALL", "1");
		toHome();
	});
}

// 注入 HTML 代码
function injectHTML() {
	if (initStorage()) {
		const div = $("body");
		const html = `
		<div class="uestc-helper-box">
			<p id="helper-text"><a
			target="_blank"
			href="https://greasyfork.org/zh-CN/scripts/429207-%E7%94%B5%E5%AD%90%E7%A7%91%E6%8A%80%E5%A4%A7%E5%AD%A6%E6%95%99%E5%8A%A1%E5%8A%A9%E6%89%8B">
			电子科技大学教务助手
		</a></p>
			<div class="btn-group">
				<button id="reset_btn">重置教务系统</button>
				<button id="close_btn">隐藏</button>
			</div>
		</div>`;

		div.prepend(html);
	} else {
		const div = $("form[action='/eams/home.action']");
		const html = `<input type="button" id="show_btn" value="显示助手">`;
		div.append(html);
	}
}

// 监听教务系统是否显示不正常
function listenBrokenFrame() {
	const bar = $("#position_bar");
	if (bar.length === 0 && !isStdElectCourse()) {
		$("#helper-text").text("教务系统页面显示异常,即将自动重置");
		$("#helper-text").css("color", "red");
		// 等待1.5秒后跳回
		setTimeout(toHome, 1500);
	}
}

// 重整布局
function resetLayout() {
	$("img[src = '/eams/avatar/my.action']").hide();
	if (hrefContains("home!submenus.action?")) {
		const node = $("div.list_box_1 li.li_1").last().clone(true);

		itemConfig.forEach(({ text, img, href }) => {
			node.find("h3").text(text);
			node.find("a").attr("href", href);
			node.find("div").css("background", `url("${img}")  no-repeat 50% 50%`);

			$("div.list_box_1 li.li_1")
				.last()
				.parent()
				.append(`<li class="li_1">` + node.html() + `</li>`);
		});
	}
}

function addBottomText() {
	const footer = $("#BottomBg");
	footer.html(
		footer.text() +
			`<br/><br/>
			页面启用了
			<span style="color: orange">
				<a
					target="_blank"
					style="color: orange"
					href="https://greasyfork.org/zh-CN/scripts/429207-%E7%94%B5%E5%AD%90%E7%A7%91%E6%8A%80%E5%A4%A7%E5%AD%A6%E6%95%99%E5%8A%A1%E5%8A%A9%E6%89%8B">
					电子科技大学教务助手
				</a></span>
				made by shawroger`
	);
}

const scoreColors = [
	{
		range: [80, 101],
		background: "LightSeaGreen",
		color: "",
	},
	{
		range: [70, 80],
		background: "LawnGreen",
		color: "",
	},
	{
		range: [60, 70],
		background: "NavajoWhite",
		color: "",
	},
	{
		range: [-1, 60],
		background: "Maroon",
		color: "white",
	},
];

const keywordsColor = [
	{
		words: ["P", "A", "通过"],
		background: "LightSeaGreen",
		color: "",
		index: -3,
	},
	{
		words: ["否"],
		background: "Maroon",
		color: "white",
		index: -2,
	},
];

// 通过分数区间来染色
function paintColorByScore(tr, text) {
	const score = parseInt(text);

	for (const { color, range, background } of scoreColors) {
		if (range[0] <= score && score < range[1]) {
			tr.css("background", background);

			if (color.length > 1) {
				tr.find("td").each((_, e) => {
					$(e).css("color", color);
				});
			}

			return true;
		}
	}

	return false;
}

// 通过关键字来染色
function paintColorByKeyword(tr, text) {
	function textContainAny(words, text) {
		for (const word of words) {
			if (text.includes(word)) {
				return true;
			}
		}
		return false;
	}
	for (const { color, words, background, index } of keywordsColor) {
		const safeText = text || tr.find("td").eq(index).text().trim();
		if (textContainAny(words, safeText)) {
			tr.css("background", background);

			if (color.length > 1) {
				tr.find("td").each((_, e) => {
					$(e).css("color", color);
				});
			}

			return true;
		}
	}

	return false;
}

// 渲染 计划完成情况 页面
function renderPlan() {
	if (!hrefContains("myPlanCompl.action")) {
		return;
	} else {
		loopVars.renderPlan = false;
		$(".toolbar-item-ge0 toolbar-item").click(() => {
			loopVars.renderPlan = true;
		});
	}

	const table = $("table.formTable");

	table.find("tr").each((_, e) => {
		const tr = $(e);
		const text = tr.find("td").eq(-3).text().trim();
		if (!paintColorByScore(tr, text)) {
			paintColorByKeyword(tr);
		}
	});
}

// 渲染 我的成绩 页面
function renderScore() {
	if (
		!hrefContains("teach/grade/course/person") &&
		!hrefContains("teach/grade/usual")
	) {
		return;
	}

	const table = $("table.gridtable").last();

	table.find("tr").each((_, e) => {
		const tr = $(e);

		const text = hrefContains("historyCourseGrade")
			? tr.find("td").last().text().trim()
			: tr.find("td").eq(-2).text().trim();
		if (!paintColorByScore(tr, text)) {
			paintColorByKeyword(tr, text);
		}
	});
}

(function () {
	"use strict";
	initStorage();
	injectCSS();
	injectHTML();
	resetLayout();
	addBottomText();
	listenBrokenFrame();
	bindAction();
	renderPlan();
	renderScore();

	setInterval(() => {
		listenBrokenFrame();

		if (loopVars.renderPlan) {
			renderPlan();
		}

		if (loopVars.renderScore) {
			renderScore();
		}

	}, 200);
})();