// ==UserScript==
// @name YSU教室课表标色
// @version 1.0.5
// @author Haomin Kong
// @description 燕山大学教室课表标色
// @match */zjdxgc/mycjcx/*
// @match http://202.206.243.9/zjdxgc/mycjcx/wjskbcx.asp
// @match http://jwc.ysu.edu.cn/*
// @icon http://ysu.edu.cn/images/favicon.png
// @require https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js
// @grant GM_registerMenuCommand
// @grant GM_unregisterMenuCommand
// @grant GM_openInTab
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_notification
// @grant GM_registerMenuCommand
// @grant GM_addStyle
// @license GPL-3.0 License
// @run-at document-end
// @namespace https://gitee.com/a645162/ysu-web-browser-scripts
// ==/UserScript==
(function () {
("use strict");
GM_registerMenuCommand("更新脚本", function () {
window.location.href = "https://gitee.com/a645162/ysu-web-browser-scripts";
});
let week = 1;
let mainTable = null;
let sel1;
let CurrentConfig = {
auto: true, //自动执行脚本
week: 0, //自定义周数,0就是自动获取
delform: true, //删除没课
show_this_week: true,//打印窗口显示第一行
colornow: true, //标色当前周
color_now_font: false, //标色当前周
colorfuture: true, //标色未来周
color_future_font: false, //标色未来周
delnow: false, //删除当前周
delfuture: false, //删除未来周
ignore_selflearn: true, //忽略自习借教室
ignore_tuanri: false,//团日
ignore_pingxuan: false,//评选
ignore_shuangxuanhui: false,//双选会
ignore_zhaopin: false,//招聘
ignore_xueshenghui: false,//学生会
delignore: true, //删除上述忽略的
//下面是保存上次的选择
save_last_select: false,//是否要恢复
last_building: -1,//教学楼
last_room: -1,//教室
};
let DefaultConfig = {
auto: true, //自动执行脚本
week: 0, //自定义周数,0就是自动获取
delform: true, //删除没课
show_this_week: true,//打印窗口显示第一行
colornow: true, //标色当前周
color_now_font: false, //标色当前周
colorfuture: true, //标色未来周
color_future_font: false, //标色未来周
delnow: false, //删除当前周
delfuture: false, //删除未来周
ignore_selflearn: true, //忽略自习借教室
ignore_tuanri: false,//团日
ignore_pingxuan: false,//评选
ignore_shuangxuanhui: false,//双选会
ignore_zhaopin: false,//招聘
ignore_xueshenghui: false,//学生会
delignore: true, //删除上述忽略的
//下面是保存上次的选择
save_last_select: false,//是否要恢复
last_building: -1,//教学楼
last_room: -1,//教室
};
function UpdateSettings() {
let settingString = JSON.stringify(CurrentConfig).trim();
GM_setValue("YSUJWConfig", settingString);
setCookie("YSUKHMJSCOLOR", settingString);
}
// alert(getCookie("YSUKHMJSCOLOR"))
// Promise.all([GM_getValue("YSUJWConfig")])
Promise.all([getCookie("YSUKHMJSCOLOR")])
.then(function (data) {
// console.log(data[0])
if (data[0] !== undefined) {
if (data[0].trim().length !== 0) {
try {
CurrentConfig = JSON.parse(data[0]);
} catch (e) {
CurrentConfig = data[0];
}
} else {
CurrentConfig = DefaultConfig;
}
} else {
CurrentConfig = DefaultConfig;
}
for (let key in DefaultConfig) {
if (typeof CurrentConfig[key] == "undefined") {
CurrentConfig[key] = DefaultConfig[key];
}
}
})
.catch(function (except) {
console.log("错误!请复制后上报bug。谢谢==>", except);
});
function DoJob() {
//自动恢复上次的选择
sel1 = document.getElementsByTagName("select");
if (CurrentConfig.save_last_select) {
if (CurrentConfig.last_building >= sel1[0].length ||
CurrentConfig.last_room >= sel1[1].length) {
CurrentConfig.last_building = 0;
CurrentConfig.last_room = 0;
CurrentConfig.save_last_select = false;
UpdateSettings();
} else if (CurrentConfig.last_building !== sel1[0].selectedIndex ||
CurrentConfig.last_room !== sel1[1].selectedIndex) {
sel1[0].selectedIndex = CurrentConfig.last_building;
sel1[1].selectedIndex = CurrentConfig.last_room;
document.getElementsByName("Submit")[0].click()
}
}
let weekText = /本周为第[0-9]+周周[0-9]/.exec(
document.getElementsByTagName("body")[0].textContent
);
if (weekText != null) {
weekText = weekText[0]
weekText = /第[0-9]+周/.exec(weekText);
week = parseInt(/[0-9]+/.exec(weekText)[0]);
let as = document.getElementsByTagName("table");
for (let i = 0, j = as.length; i < j; ++i) {
let tableObj = as[i];
if (tableObj.rows.length >= 8) {
mainTable = tableObj;
TraverseTable(tableObj);
}
}
} else {
document.getElementsByName("Submit")[0].click()
}
}
function TraverseTable(tableObj) {
for (var i = 0, i1 = tableObj.rows.length; i < i1 && i1 >= 8; i++) {
for (var j = 0, j1 = tableObj.rows[i].cells.length; j < j1; j++) {
re(tableObj.rows[i].cells[j]);
}
}
}
function re(td) {
let text = td.innerText.trim();
let regExp = /[0-9]+-[0-9]+周/g;
if (!regExp.test(text)) {
return;
}
let courseSplit = text.split("\n\n");
let now = week;
if (CurrentConfig.week > 0) {
now = CurrentConfig.week;
}
//now = 10;
let state = 0;
let needIgnore = false;
for (let i = 0, j = courseSplit.length; i < j; i++) {
let current = courseSplit[i].trim();
//console.log(current);
//type:1正常上课 2考试 3借用
let type = 0;
if (current.indexOf("正常上课") !== -1) {
type = 1;
} else if (current.indexOf("考试") !== -1) {
type = 2;
} else if (current.indexOf("借用") !== -1) {
type = 3;
}
let oneLineText = current;
while (oneLineText.indexOf("\n") !== -1) {
oneLineText = oneLineText.replace("\n", "");
}
let match = /[0-9]+-[0-9]+周/g.exec(oneLineText);
match = /[0-9]+-[0-9]+/.exec(match[0].trim())[0].trim();
let l = match.split("-");
if (l.length === 2) {
let start = parseInt(l[0]);
let end = parseInt(l[1]);
if (start <= now && now <= end) {
//还在上
state = 1;
}
if (now < start && state !== 1) {
//未开课
state = 2;
}
if (type === 3 && now <= end) {
if (
(CurrentConfig.ignore_selflearn && current.indexOf("自习") !== -1) ||
(CurrentConfig.ignore_tuanri &&
(current.indexOf("团日") !== -1 ||
current.indexOf("团课") !== -1 ||
current.indexOf("团活") !== -1)) ||
(CurrentConfig.ignore_pingxuan && current.indexOf("评选") !== -1) ||
(CurrentConfig.ignore_shuangxuanhui && current.indexOf("双选会") !== -1) ||
(CurrentConfig.ignore_zhaopin && current.indexOf("招聘") !== -1) ||
(CurrentConfig.ignore_xueshenghui && current.indexOf("学生会") !== -1)
) {
type = 0;
needIgnore = true;
}
}
}
}
if (courseSplit.length !== 0) {
if (needIgnore) {
td.bgColor = "FFFFFF";
if (CurrentConfig.delignore) {
td.textContent = "";
}
} else if (state === 1) {
if (CurrentConfig.colornow) {
td.bgColor = "FFFFBB";
}
if (CurrentConfig.color_now_font) {
td.style.color = "red"
}
if (CurrentConfig.delnow) {
td.textContent = "";
}
} else if (state === 2) {
if (CurrentConfig.colorfuture) {
td.bgColor = "FFDDDD";
}
if (CurrentConfig.color_future_font) {
td.style.color = "blue"
}
if (CurrentConfig.delfuture) {
td.textContent = "";
}
} else {
td.bgColor = "FFFFFF";
if (CurrentConfig.delform) {
td.textContent = "";
}
}
}
}
function newCheckbox(parent, title, checked, change_listener, before_line, after_line) {
let cb = document.createElement("input");
cb.type = "checkbox";
cb.checked = checked;
if (change_listener != null) {
cb.addEventListener("change", change_listener);
}
parent.appendChild(cb);
for (let i = 0; i < after_line; ++i) {
cb.insertAdjacentHTML("afterend", "<br />");
}
cb.insertAdjacentText("afterend", title);
for (let i = 0; i < before_line; ++i) {
cb.insertAdjacentHTML("beforebegin", "<br />");
}
return cb;
}
function newButton(parent, title, click_listener, before_line, after_line) {
let btn = document.createElement("Button");
btn.addEventListener("click", click_listener);
btn.innerText = title;
parent.appendChild(btn);
for (let i = 0; i < before_line; ++i) {
btn.insertAdjacentHTML("beforebegin", "<br />");
}
for (let i = 0; i < after_line; ++i) {
btn.insertAdjacentHTML("afterend", "<br />");
}
return btn;
}
function addLink() {
let submit = document.getElementsByName("Submit")[0];
let parent1 = submit.parentNode;
let parent2 = submit.parentNode.parentNode.childNodes[1];
submit.insertAdjacentHTML("afterend", "<br />");
newCheckbox(parent1, "自动恢复保存的选择", CurrentConfig.save_last_select,
function (e) {
CurrentConfig.save_last_select = e.target.checked;
CurrentConfig.last_building = sel1[0].selectedIndex;
CurrentConfig.last_room = sel1[1].selectedIndex;
UpdateSettings();
}, 1, 0);
newButton(parent1, "保存当前的选择",
function () {
CurrentConfig.last_building = sel1[0].selectedIndex;
CurrentConfig.last_room = sel1[1].selectedIndex;
UpdateSettings();
}, 0, 1);
//let as = document.getElementsByTagName("table");
newCheckbox(parent1, "自动执行脚本", CurrentConfig.auto,
function (e) {
CurrentConfig.auto = e.target.checked;
UpdateSettings();
}, 1, 0);
newCheckbox(parent1, "自动删除没课的格子", CurrentConfig.delform,
function (e) {
CurrentConfig.delform = e.target.checked;
UpdateSettings();
}, 1, 2);
newCheckbox(parent1, "忽略借教室自习(这真没意思!!!)", CurrentConfig.ignore_selflearn,
function (e) {
CurrentConfig.ignore_selflearn = e.target.checked;
UpdateSettings();
}, 1, 0);
newCheckbox(parent1, "忽略团日/团课活动", CurrentConfig.ignore_tuanri, function (e) {
CurrentConfig.ignore_tuanri = e.target.checked;
UpdateSettings();
}, 1, 0);
newCheckbox(parent1, "忽略评选", CurrentConfig.ignore_pingxuan,
function (e) {
CurrentConfig.ignore_pingxuan = e.target.checked;
UpdateSettings();
}, 1, 0);
newCheckbox(parent1, "忽略招聘", CurrentConfig.ignore_zhaopin,
function (e) {
CurrentConfig.ignore_zhaopin = e.target.checked;
UpdateSettings();
}, 1, 0);
newCheckbox(parent1, "忽略双选会", CurrentConfig.ignore_shuangxuanhui,
function (e) {
CurrentConfig.ignore_shuangxuanhui = e.target.checked;
UpdateSettings();
}, 1, 0);
newCheckbox(parent1, "忽略学生会", CurrentConfig.ignore_xueshenghui,
function (e) {
CurrentConfig.ignore_xueshenghui = e.target.checked;
UpdateSettings();
}, 1, 0);
newCheckbox(parent1, "自动删除上述忽略的格子", CurrentConfig.delignore,
function (e) {
CurrentConfig.delignore = e.target.checked;
UpdateSettings();
}, 1, 0);
if (!CurrentConfig.auto) {
newButton(parent1, "执行脚本",
function () {
DoJob();
}, 1, 1);
}
//第二个格子
let keyValue = "Ctrl + P";
if (isMac()) {
keyValue = "Command + P";
}
let printBtn = newButton(parent2, "简化元素(按" + keyValue + "打印)",
function () {
if (mainTable != null) {
let wind = window.open("", "print_win",
"toolbar = no, scrollbars = yes, menubar = no,location = no");
wind.document.body.innerHTML = mainTable.outerHTML;
let room = sel1[1].options[sel1[1].selectedIndex].text;
wind.document.title = room.trim() + " 课程表(请按" + keyValue + "进行打印)";
let as = wind.document.getElementsByTagName("table");
let tableObj = as[0];
let tbody = tableObj.children[0];
for (let i = CurrentConfig.show_this_week ? 1 : 0
, j = tbody.children.length;
i < j; ++i) {
if (tbody.children[i].children.length !== 8) {
tbody.removeChild(tbody.children[i]);
i--;
}
}
}
}, 0, 0);
//printBtn.insertAdjacentHTML("beforebegin", "打印相关:<br />");
newCheckbox(parent2, "是否显示当前周数(第一行)", CurrentConfig.show_this_week,
function (e) {
CurrentConfig.show_this_week = e.target.checked;
UpdateSettings();
}, 0, 1);
newCheckbox(parent2, "当前周有课的文字着色", CurrentConfig.color_now_font,
function (e) {
CurrentConfig.color_now_font = e.target.checked;
UpdateSettings();
}, 1, 0);
newCheckbox(parent2, "未来周有课的文字着色", CurrentConfig.color_future_font,
function (e) {
CurrentConfig.color_future_font = e.target.checked;
UpdateSettings();
}, 0, 2);
let text1 = document.createElement("input");
text1.id = "text_week";
text1.type = "number";
text1.max = 20;
text1.min = 0;
text1.value = CurrentConfig.week.toString();
parent2.appendChild(text1);
text1.insertAdjacentText("afterend", "周(0为自动获取)");
text1.insertAdjacentHTML("beforebegin", "<br />");
text1.insertAdjacentHTML("beforebegin", "设置当前周数:");
newButton(parent2, "保存",
function () {
CurrentConfig.week = parseInt(text1.value);
UpdateSettings();
}, 0, 1);
newCheckbox(parent2, "标色当前周有课", CurrentConfig.colornow,
function (e) {
CurrentConfig.colornow = e.target.checked;
UpdateSettings();
}, 1, 0);
newCheckbox(parent2, "删除当前周有课", CurrentConfig.delnow,
function (e) {
CurrentConfig.delnow = e.target.checked;
UpdateSettings();
}, 0, 0).insertAdjacentHTML("beforebegin", " ");
newCheckbox(parent2, "标色未来周有课", CurrentConfig.colorfuture,
function (e) {
CurrentConfig.colorfuture = e.target.checked;
UpdateSettings();
}, 1, 0);
newCheckbox(parent2, "删除未来周有课", CurrentConfig.delfuture,
function (e) {
CurrentConfig.delfuture = e.target.checked;
UpdateSettings();
}, 0, 1).insertAdjacentHTML("beforebegin", " ");
newButton(parent2, "刷新页面",
function () {
location.reload();
}, 1, 0)
newButton(parent2, "重新从教务处网站进入本页面",
function () {
window.location.href = "http://jwc.ysu.edu.cn/?action=jskb";
}, 1, 0).insertAdjacentHTML("afterend", "刷新页面并不能刷新新周数");
newButton(parent2, "进入教务系统62",
function () {
window.location.href = "http://jwc.ysu.edu.cn/?action=jwxt62";
}, 2, 0)
newButton(parent2, "进入教务系统6(校内)",
function () {
window.location.href = "http://jwc.ysu.edu.cn/?action=jwxt9";
}, 0, 0)
newButton(parent2, "进入教务系统9(校内)",
function () {
window.location.href = "http://jwc.ysu.edu.cn/?action=jwxt9";
}, 0, 0)
newButton(parent2, "恢复默认设置",
function () {
CurrentConfig = DefaultConfig;
UpdateSettings();
location.reload();
}, 2, 0)
newButton(parent2, "更新脚本",
function () {
window.location.href = 'https://gitee.com/a645162/ysu-web-browser-scripts';
}, 2, 0)
var lab1 = document.createElement("a");
lab1.innerText = " 脚本作者:孔昊旻";
lab1.setAttribute("href", "https://gitee.com/a645162");
parent2.appendChild(lab1);
}
function isMac() {
let reg_mac = new RegExp("mac", 'i');
return navigator.platform.match(reg_mac) !== null;
}
//JS操作cookies方法!
//写cookies
function setCookie(name, value) {
// let Days = 30;
// let exp = new Date();
// exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
document.cookie =
name + "=" + escape(value) + ";"
// ""expires=" + exp.toGMTString();
}
//读取cookies
function getCookie(name) {
var arr,
reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
if ((arr = document.cookie.match(reg))) return unescape(arr[2]);
else return null;
}
//删除cookies
function delCookie(name) {
let exp = new Date();
exp.setTime(exp.getTime() - 1);
let cval = getCookie(name);
if (cval != null)
document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
}
const changeFavicon = link => {
let $favicon = document.querySelector('link[rel="icon"]');
// If a <link rel="icon"> element already exists,
// change its href to the given link.
if ($favicon !== null) {
$favicon.href = link;
// Otherwise, create a new element and append it to <head>.
} else {
$favicon = document.createElement("link");
$favicon.rel = "icon";
$favicon.href = link;
document.head.appendChild($favicon);
}
};
//DoJob();
//addLink();
function LoadJS() {
let link = window.location.href.trim();
if (link.indexOf("jwc.ysu.edu.cn") !== -1) {
let regexStr = /\?action=.*/;
if (regexStr.test(link)) {
let command = regexStr.exec(link)[0].trim()
if (/=jskb/.test(command)) {
window.location.href = "http://202.206.243.9/zjdxgc/mycjcx/wjskbcx.asp";
} else if (/=jwxt/.test(command)) {
const url_jwct = "http://202.206.243.";
let num = /[0-9]+/.exec(/=jwxt.*/.exec(link)[0])
window.location.href = url_jwct.trim() + num;
}
}
} else {
if (CurrentConfig.auto) {
DoJob();
}
addLink();
}
}
window.onload = function () {
changeFavicon("http://ysu.edu.cn/images/favicon.png");
LoadJS();
};
})();