| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- // 天津农学院(tjau.edu.cn) 拾光课程表适配脚本
- // 非该大学开发者适配,开发者无法及时发现问题
- // 出现问题请提issues或者提交pr更改,这更加快速
- function powerSplit(paramsRaw) {
- const args = [];
- let current = "";
- let depth = 0;
- let inQuote = false;
- let quoteChar = "";
- for (let i = 0; i < paramsRaw.length; i++) {
- let char = paramsRaw[i];
- if ((char === '"' || char === "'") && (i === 0 || paramsRaw[i - 1] !== '\\')) {
- if (!inQuote) { inQuote = true; quoteChar = char; }
- else if (char === quoteChar) { inQuote = false; }
- }
- if (!inQuote) {
- if (char === '(' || char === '[' || char === '{') depth++;
- if (char === ')' || char === ']' || char === '}') depth--;
- }
- if (char === ',' && depth === 0 && !inQuote) {
- args.push(cleanArg(current));
- current = "";
- } else {
- current += char;
- }
- }
- args.push(cleanArg(current));
- return args;
- }
- function cleanArg(s) {
- s = s.trim();
- if (s === "null") return null;
- return s.replace(/^["']|["']$/g, "");
- }
- /**
- * 全局课程合并逻辑
- */
- function mergeContinuousLessons(lessons) {
- if (!lessons || lessons.length <= 1) return lessons;
- lessons.sort((a, b) => {
- if (a.name !== b.name) return a.name.localeCompare(b.name);
- if (a.day !== b.day) return a.day - b.day;
- if (JSON.stringify(a.weeks) !== JSON.stringify(b.weeks))
- return JSON.stringify(a.weeks).localeCompare(JSON.stringify(b.weeks));
- return a.startSection - b.startSection;
- });
- const merged = [];
- let current = lessons[0];
- for (let i = 1; i < lessons.length; i++) {
- let next = lessons[i];
- const isSameLesson =
- current.name === next.name &&
- current.teacher === next.teacher &&
- current.position === next.position &&
- current.day === next.day &&
- JSON.stringify(current.weeks) === JSON.stringify(next.weeks);
- const isContinuous = current.endSection + 1 === next.startSection;
- if (isSameLesson && isContinuous) {
- current.endSection = next.endSection;
- } else {
- merged.push(current);
- current = next;
- }
- }
- merged.push(current);
- return merged;
- }
- function parseTaskActivities(html) {
- const rawResults = [];
- const blocks = html.split(/var\s+teachers\s*=/);
- for (let i = 1; i < blocks.length; i++) {
- const block = blocks[i];
- let teacherName = "未知教师";
- const tMatch = block.match(/actTeachers\s*=\s*\[\s*\{[\s\S]*?name:\s*"(.*?)"/);
- if (tMatch) teacherName = tMatch[1];
- const activityMatch = block.match(/new\s+TaskActivity\(([\s\S]*?)\);/);
- if (!activityMatch) continue;
- const args = powerSplit(activityMatch[1]);
- const courseName = (args[3] || "未知课程").split('(')[0];
- const position = (args[5] || "未知地点").replace(/\(.*?\)/g, "");
- const weeksBitmap = args[6] || "";
-
- const weeks = [];
- for (let j = 0; j < weeksBitmap.length; j++) {
- if (weeksBitmap[j] === '1') weeks.push(j);
- }
- const unitCountMatch = html.match(/unitCount\s*=\s*(\d+)/);
- const unitCount = unitCountMatch ? parseInt(unitCountMatch[1]) : 14;
- const idxRegex = /index\s*=\s*(\d+)\s*\*\s*unitCount\s*\+\s*(\d+);/g;
- let m;
- while ((m = idxRegex.exec(block)) !== null) {
- const day = parseInt(m[1]) + 1;
- const section = parseInt(m[2]) + 1;
- rawResults.push({
- "name": courseName,
- "teacher": teacherName,
- "position": position,
- "day": day,
- "startSection": section,
- "endSection": section,
- "weeks": weeks
- });
- }
- }
- // 执行全局合并逻辑
- const mergedResults = mergeContinuousLessons(rawResults);
- return mergedResults;
- }
- async function request(url, options = {}) {
- const res = await fetch(url, { credentials: "include", ...options });
- if (!res.ok) throw new Error(`网络请求失败: ${res.status}`);
- return await res.text();
- }
- async function detectParameters() {
- const html = await request("http://jwxt.tjau.edu.cn/eams/courseTableForStd.action?sf_request_type=ajax");
- const idsMatch = html.match(/bg\.form\.addInput\(form,"ids","(\d+)"\)/);
- const tagIdMatch = html.match(/id="(semesterBar\d+Semester)"/);
- if (!idsMatch || !tagIdMatch) return null;
- return { ids: idsMatch[1], tagId: tagIdMatch[1] };
- }
- async function getSelectedSemester(tagId) {
- const raw = await request(`http://jwxt.tjau.edu.cn/eams/dataQuery.action?sf_request_type=ajax`, {
- method: "POST",
- headers: { "Content-Type": "application/x-www-form-urlencoded" },
- body: `tagId=${encodeURIComponent(tagId)}&dataType=semesterCalendar`
- });
- const data = Function(`return (${raw});`)();
- const list = [];
- for (let key in data.semesters) {
- data.semesters[key].forEach(s => list.push({ id: s.id, name: `${s.schoolYear} ${s.name}学期` }));
- }
- const idx = await window.AndroidBridgePromise.showSingleSelection("选择学期", JSON.stringify(list.map(s => s.name)), 0);
- return idx !== null ? list[idx] : null;
- }
- async function fetchAndParseCourses(semesterId, ids) {
- const html = await request(`http://jwxt.tjau.edu.cn/eams/courseTableForStd!courseTable.action?sf_request_type=ajax`, {
- method: "POST",
- headers: { "Content-Type": "application/x-www-form-urlencoded" },
- body: `ignoreHead=1&setting.kind=std&semester.id=${semesterId}&ids=${ids}`
- });
- return parseTaskActivities(html);
- }
- async function applyTimeSlots() {
- const slots = [
- { "number": 1, "startTime": "08:30", "endTime": "09:15" },
- { "number": 2, "startTime": "09:20", "endTime": "10:05" },
- { "number": 3, "startTime": "10:25", "endTime": "11:10" },
- { "number": 4, "startTime": "11:15", "endTime": "12:00" },
- { "number": 5, "startTime": "14:00", "endTime": "14:45" },
- { "number": 6, "startTime": "14:50", "endTime": "15:35" },
- { "number": 7, "startTime": "15:55", "endTime": "16:40" },
- { "number": 8, "startTime": "16:45", "endTime": "17:30" },
- { "number": 9, "startTime": "18:30", "endTime": "19:15" },
- { "number": 10, "startTime": "19:20", "endTime": "20:05" },
- { "number": 11, "startTime": "20:10", "endTime": "20:55" }
- ];
- return await window.AndroidBridgePromise.savePresetTimeSlots(JSON.stringify(slots));
- }
- async function runImportFlow() {
- try {
- AndroidBridge.showToast("开始探测教务参数...");
- const params = await detectParameters();
- if (!params) throw new Error("未能识别教务参数,请确认已登录");
- const semester = await getSelectedSemester(params.tagId);
- if (!semester) return;
- AndroidBridge.showToast("正在同步课表...");
- const courses = await fetchAndParseCourses(semester.id, params.ids);
-
- if (!courses || courses.length === 0) throw new Error("未解析到课程数据");
- await applyTimeSlots();
- const saveResult = await window.AndroidBridgePromise.saveImportedCourses(JSON.stringify(courses));
-
- if (saveResult) {
- AndroidBridge.showToast(`成功导入 ${courses.length} 个课程条目`);
- AndroidBridge.notifyTaskCompletion();
- }
- } catch (e) {
- console.error(`[异常] ${e.message}`);
- AndroidBridge.showToast(e.message);
- }
- }
- runImportFlow();
|