tjau.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // 天津农学院(tjau.edu.cn) 拾光课程表适配脚本
  2. // 非该大学开发者适配,开发者无法及时发现问题
  3. // 出现问题请提issues或者提交pr更改,这更加快速
  4. function powerSplit(paramsRaw) {
  5. const args = [];
  6. let current = "";
  7. let depth = 0;
  8. let inQuote = false;
  9. let quoteChar = "";
  10. for (let i = 0; i < paramsRaw.length; i++) {
  11. let char = paramsRaw[i];
  12. if ((char === '"' || char === "'") && (i === 0 || paramsRaw[i - 1] !== '\\')) {
  13. if (!inQuote) { inQuote = true; quoteChar = char; }
  14. else if (char === quoteChar) { inQuote = false; }
  15. }
  16. if (!inQuote) {
  17. if (char === '(' || char === '[' || char === '{') depth++;
  18. if (char === ')' || char === ']' || char === '}') depth--;
  19. }
  20. if (char === ',' && depth === 0 && !inQuote) {
  21. args.push(cleanArg(current));
  22. current = "";
  23. } else {
  24. current += char;
  25. }
  26. }
  27. args.push(cleanArg(current));
  28. return args;
  29. }
  30. function cleanArg(s) {
  31. s = s.trim();
  32. if (s === "null") return null;
  33. return s.replace(/^["']|["']$/g, "");
  34. }
  35. /**
  36. * 全局课程合并逻辑
  37. */
  38. function mergeContinuousLessons(lessons) {
  39. if (!lessons || lessons.length <= 1) return lessons;
  40. lessons.sort((a, b) => {
  41. if (a.name !== b.name) return a.name.localeCompare(b.name);
  42. if (a.day !== b.day) return a.day - b.day;
  43. if (JSON.stringify(a.weeks) !== JSON.stringify(b.weeks))
  44. return JSON.stringify(a.weeks).localeCompare(JSON.stringify(b.weeks));
  45. return a.startSection - b.startSection;
  46. });
  47. const merged = [];
  48. let current = lessons[0];
  49. for (let i = 1; i < lessons.length; i++) {
  50. let next = lessons[i];
  51. const isSameLesson =
  52. current.name === next.name &&
  53. current.teacher === next.teacher &&
  54. current.position === next.position &&
  55. current.day === next.day &&
  56. JSON.stringify(current.weeks) === JSON.stringify(next.weeks);
  57. const isContinuous = current.endSection + 1 === next.startSection;
  58. if (isSameLesson && isContinuous) {
  59. current.endSection = next.endSection;
  60. } else {
  61. merged.push(current);
  62. current = next;
  63. }
  64. }
  65. merged.push(current);
  66. return merged;
  67. }
  68. function parseTaskActivities(html) {
  69. const rawResults = [];
  70. const blocks = html.split(/var\s+teachers\s*=/);
  71. for (let i = 1; i < blocks.length; i++) {
  72. const block = blocks[i];
  73. let teacherName = "未知教师";
  74. const tMatch = block.match(/actTeachers\s*=\s*\[\s*\{[\s\S]*?name:\s*"(.*?)"/);
  75. if (tMatch) teacherName = tMatch[1];
  76. const activityMatch = block.match(/new\s+TaskActivity\(([\s\S]*?)\);/);
  77. if (!activityMatch) continue;
  78. const args = powerSplit(activityMatch[1]);
  79. const courseName = (args[3] || "未知课程").split('(')[0];
  80. const position = (args[5] || "未知地点").replace(/\(.*?\)/g, "");
  81. const weeksBitmap = args[6] || "";
  82. const weeks = [];
  83. for (let j = 0; j < weeksBitmap.length; j++) {
  84. if (weeksBitmap[j] === '1') weeks.push(j);
  85. }
  86. const unitCountMatch = html.match(/unitCount\s*=\s*(\d+)/);
  87. const unitCount = unitCountMatch ? parseInt(unitCountMatch[1]) : 14;
  88. const idxRegex = /index\s*=\s*(\d+)\s*\*\s*unitCount\s*\+\s*(\d+);/g;
  89. let m;
  90. while ((m = idxRegex.exec(block)) !== null) {
  91. const day = parseInt(m[1]) + 1;
  92. const section = parseInt(m[2]) + 1;
  93. rawResults.push({
  94. "name": courseName,
  95. "teacher": teacherName,
  96. "position": position,
  97. "day": day,
  98. "startSection": section,
  99. "endSection": section,
  100. "weeks": weeks
  101. });
  102. }
  103. }
  104. // 执行全局合并逻辑
  105. const mergedResults = mergeContinuousLessons(rawResults);
  106. return mergedResults;
  107. }
  108. async function request(url, options = {}) {
  109. const res = await fetch(url, { credentials: "include", ...options });
  110. if (!res.ok) throw new Error(`网络请求失败: ${res.status}`);
  111. return await res.text();
  112. }
  113. async function detectParameters() {
  114. const html = await request("http://jwxt.tjau.edu.cn/eams/courseTableForStd.action?sf_request_type=ajax");
  115. const idsMatch = html.match(/bg\.form\.addInput\(form,"ids","(\d+)"\)/);
  116. const tagIdMatch = html.match(/id="(semesterBar\d+Semester)"/);
  117. if (!idsMatch || !tagIdMatch) return null;
  118. return { ids: idsMatch[1], tagId: tagIdMatch[1] };
  119. }
  120. async function getSelectedSemester(tagId) {
  121. const raw = await request(`http://jwxt.tjau.edu.cn/eams/dataQuery.action?sf_request_type=ajax`, {
  122. method: "POST",
  123. headers: { "Content-Type": "application/x-www-form-urlencoded" },
  124. body: `tagId=${encodeURIComponent(tagId)}&dataType=semesterCalendar`
  125. });
  126. const data = Function(`return (${raw});`)();
  127. const list = [];
  128. for (let key in data.semesters) {
  129. data.semesters[key].forEach(s => list.push({ id: s.id, name: `${s.schoolYear} ${s.name}学期` }));
  130. }
  131. const idx = await window.AndroidBridgePromise.showSingleSelection("选择学期", JSON.stringify(list.map(s => s.name)), 0);
  132. return idx !== null ? list[idx] : null;
  133. }
  134. async function fetchAndParseCourses(semesterId, ids) {
  135. const html = await request(`http://jwxt.tjau.edu.cn/eams/courseTableForStd!courseTable.action?sf_request_type=ajax`, {
  136. method: "POST",
  137. headers: { "Content-Type": "application/x-www-form-urlencoded" },
  138. body: `ignoreHead=1&setting.kind=std&semester.id=${semesterId}&ids=${ids}`
  139. });
  140. return parseTaskActivities(html);
  141. }
  142. async function applyTimeSlots() {
  143. const slots = [
  144. { "number": 1, "startTime": "08:30", "endTime": "09:15" },
  145. { "number": 2, "startTime": "09:20", "endTime": "10:05" },
  146. { "number": 3, "startTime": "10:25", "endTime": "11:10" },
  147. { "number": 4, "startTime": "11:15", "endTime": "12:00" },
  148. { "number": 5, "startTime": "14:00", "endTime": "14:45" },
  149. { "number": 6, "startTime": "14:50", "endTime": "15:35" },
  150. { "number": 7, "startTime": "15:55", "endTime": "16:40" },
  151. { "number": 8, "startTime": "16:45", "endTime": "17:30" },
  152. { "number": 9, "startTime": "18:30", "endTime": "19:15" },
  153. { "number": 10, "startTime": "19:20", "endTime": "20:05" },
  154. { "number": 11, "startTime": "20:10", "endTime": "20:55" }
  155. ];
  156. return await window.AndroidBridgePromise.savePresetTimeSlots(JSON.stringify(slots));
  157. }
  158. async function runImportFlow() {
  159. try {
  160. AndroidBridge.showToast("开始探测教务参数...");
  161. const params = await detectParameters();
  162. if (!params) throw new Error("未能识别教务参数,请确认已登录");
  163. const semester = await getSelectedSemester(params.tagId);
  164. if (!semester) return;
  165. AndroidBridge.showToast("正在同步课表...");
  166. const courses = await fetchAndParseCourses(semester.id, params.ids);
  167. if (!courses || courses.length === 0) throw new Error("未解析到课程数据");
  168. await applyTimeSlots();
  169. const saveResult = await window.AndroidBridgePromise.saveImportedCourses(JSON.stringify(courses));
  170. if (saveResult) {
  171. AndroidBridge.showToast(`成功导入 ${courses.length} 个课程条目`);
  172. AndroidBridge.notifyTaskCompletion();
  173. }
  174. } catch (e) {
  175. console.error(`[异常] ${e.message}`);
  176. AndroidBridge.showToast(e.message);
  177. }
  178. }
  179. runImportFlow();