Greasy Fork

电子科技大学教务助手

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

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

// ==UserScript==
// @name         电子科技大学教务助手
// @namespace    http://shawroger.gitee.io/
// @version      0.11
// @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==

var loopRunCheckPlan = true;

// 跳转首页
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: 6px;
		}

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

		.list_box_1 ul .li_1 {
			margin-bottom: 20px;
		}
	</style>`;
	head.append(css);
}

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

// 注入 HTML 代码
function injectHTML() {
	const div = $("body");
	const html = `
	<div class="uestc-helper-box">
		<p id="helper-text">欢迎使用教务系统小助手</p>
		<button id="reset_btn">重置教务系统</button>
	</div>`;

	div.prepend(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 (window.location.href.includes("home!submenus.action?")) {
		const node = $("div.list_box_1 li.li_1").last().clone(true);

		const itemConfig = [
			{
				text: "快速查询成绩",
				img: "https://pic.imgdb.cn/item/60ead77f5132923bf8f3f56b.png",
				href: "http://eams.uestc.edu.cn/eams/teach/grade/course/person!search.action?semesterId=303&projectType=",
			},
			{
				text: "审阅我的计划",
				img: "https://pic.imgdb.cn/item/60eadacf5132923bf8ffcca2.png",
				href: "http://eams.uestc.edu.cn/eams/myPlanCompl.action",
			},
		];

		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">电子科技大学教务助手</span> Made by Shawroger`
	);
}

// 计划完成情况
function checkPlan() {
	if (!window.location.href.includes("myPlanCompl.action")) {
		return;
	} else {
		loopRunCheckPlan = false;
	}

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

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

			if (text === "否") {
				tr.css("background", "Maroon");
				tr.find("td").each((_, e) => {
					$(e).css("color", "white");
				});
			} else if (["P", "A", "通过"].includes(text) || parseInt(text) >= 80) {
				tr.css("background", "LightSeaGreen");
			} else if (70 <= parseInt(text) && parseInt(text) < 80) {
				tr.css("background", "LawnGreen");
			} else if (60 <= parseInt(text) && parseInt(text) < 70) {
				tr.css("background", "NavajoWhite");
			}
		});
	});
}

(function () {
	"use strict";
	injectCSS();
	injectHTML();
	resetLayout();
	addBottomText();
	listenBrokenFrame();
	setInterval(() => {
		listenBrokenFrame();

		if (loopRunCheckPlan) {
			checkPlan();
		}
	}, 200);

	bindAction();
	checkPlan();
})();