csuft.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // 中南林业科技大学(csuft.edu.cn) 拾光课程表适配脚本
  2. // 强智教务系统,通过 WebVPN 访问
  3. window.validateYearInput = function(input) {
  4. return /^[0-9]{4}$/.test(input) ? false : "请输入四位数字的学年!";
  5. };
  6. function parseWeeks(weekStr) {
  7. const weeks = [];
  8. if (!weekStr) return weeks;
  9. const pure = weekStr.split('(')[0];
  10. pure.split(',').forEach(seg => {
  11. if (seg.includes('-')) {
  12. const [s, e] = seg.split('-').map(Number);
  13. if (!isNaN(s) && !isNaN(e)) {
  14. for (let i = s; i <= e; i++) weeks.push(i);
  15. }
  16. } else {
  17. const w = parseInt(seg);
  18. if (!isNaN(w)) weeks.push(w);
  19. }
  20. });
  21. return [...new Set(weeks)].sort((a, b) => a - b);
  22. }
  23. function mergeCourses(courses) {
  24. if (courses.length <= 1) return courses;
  25. courses.sort((a, b) => {
  26. return a.name.localeCompare(b.name) || a.day - b.day || a.startSection - b.startSection;
  27. });
  28. const merged = [];
  29. let cur = courses[0];
  30. for (let i = 1; i < courses.length; i++) {
  31. const n = courses[i];
  32. if (cur.name === n.name && cur.teacher === n.teacher && cur.position === n.position && cur.day === n.day && cur.endSection + 1 === n.startSection) {
  33. cur.endSection = n.endSection;
  34. } else {
  35. merged.push(cur);
  36. cur = n;
  37. }
  38. }
  39. merged.push(cur);
  40. return merged;
  41. }
  42. const SECTION_MAP = {
  43. '第1,2节': [1, 2], '第1,2节': [1, 2], '第1, 2节': [1, 2],
  44. '第3,4节': [3, 4], '第3,4节': [3, 4], '第3, 4节': [3, 4],
  45. '第5,6节': [5, 6], '第5,6节': [5, 6], '第5, 6节': [5, 6],
  46. '第7,8节': [7, 8], '第7,8节': [7, 8], '第7, 8节': [7, 8],
  47. '第9,10节': [9, 10], '第9,10节': [9, 10], '第9, 10节': [9, 10],
  48. };
  49. const TIME_SLOTS = [
  50. { number: 1, startTime: "08:00", endTime: "08:45" },
  51. { number: 2, startTime: "08:55", endTime: "09:40" },
  52. { number: 3, startTime: "10:00", endTime: "10:45" },
  53. { number: 4, startTime: "10:55", endTime: "11:40" },
  54. { number: 5, startTime: "14:00", endTime: "14:45" },
  55. { number: 6, startTime: "14:55", endTime: "15:40" },
  56. { number: 7, startTime: "16:00", endTime: "16:45" },
  57. { number: 8, startTime: "16:55", endTime: "17:40" },
  58. { number: 9, startTime: "19:00", endTime: "19:45" },
  59. { number: 10, startTime: "19:55", endTime: "20:40" },
  60. ];
  61. function parseSchedule(doc) {
  62. const table = doc.getElementById('kbtable');
  63. if (!table) return [];
  64. let raw = [];
  65. const rows = Array.from(table.querySelectorAll('tr')).filter(r => r.querySelector('td'));
  66. rows.forEach(row => {
  67. const th = row.querySelector('th');
  68. if (!th) return;
  69. const thText = th.textContent.trim();
  70. if (thText.includes('备注')) return;
  71. const section = SECTION_MAP[thText];
  72. if (!section) return;
  73. const cells = row.querySelectorAll('td');
  74. cells.forEach((cell, idx) => {
  75. const day = idx + 1;
  76. const divs = cell.querySelectorAll('div.kbcontent');
  77. divs.forEach(div => {
  78. const html = div.innerHTML.trim();
  79. if (!html || html === '&nbsp;' || div.innerText.trim().length < 2) return;
  80. const blocks = html.split(/[-]{5,}/);
  81. blocks.forEach(block => {
  82. if (!block.trim()) return;
  83. const temp = document.createElement('div');
  84. temp.innerHTML = block;
  85. let name = '';
  86. for (let node of temp.childNodes) {
  87. if (node.nodeType === 3 && node.textContent.trim() !== '') {
  88. name = node.textContent.trim();
  89. break;
  90. }
  91. }
  92. const teacher = (temp.querySelector('font[title="老师"]') || temp.querySelector('font[title="教师"]'))?.innerText || '';
  93. const position = temp.querySelector('font[title="教室"]')?.innerText || '';
  94. const weekStr = temp.querySelector('font[title="周次(节次)"]')?.innerText || '';
  95. if (name && section[0] > 0) {
  96. raw.push({
  97. name: name,
  98. teacher: teacher,
  99. weeks: parseWeeks(weekStr),
  100. position: position,
  101. day: day,
  102. startSection: section[0],
  103. endSection: section[1]
  104. });
  105. }
  106. });
  107. });
  108. });
  109. });
  110. return mergeCourses(raw);
  111. }
  112. async function runImportFlow() {
  113. try {
  114. const confirmed = await window.AndroidBridgePromise.showAlert("提示", "请确保已成功登录教务系统。是否开始导入?", "开始");
  115. if (!confirmed) return;
  116. const year = await window.AndroidBridgePromise.showPrompt("选择学年", "请输入要导入的起始学年(例如 2025-2026 应输入2025):", "", "validateYearInput");
  117. if (!year) return;
  118. const semesterIdx = await window.AndroidBridgePromise.showSingleSelection("选择学期", JSON.stringify(["第一学期", "第二学期"]), -1);
  119. if (semesterIdx === null) return;
  120. const semId = `${year}-${parseInt(year) + 1}-${semesterIdx + 1}`;
  121. AndroidBridge.showToast("正在获取课表数据...");
  122. const url = "https://http-jwgl-csuft-edu-cn-80.webvpn.csuft.edu.cn/jsxsd/xskb/xskb_list.do";
  123. const resp = await fetch(url, {
  124. method: "POST",
  125. headers: { "Content-Type": "application/x-www-form-urlencoded" },
  126. body: `xnxq01id=${semId}`,
  127. credentials: "include"
  128. });
  129. const html = await resp.text();
  130. const courses = parseSchedule(new DOMParser().parseFromString(html, "text/html"));
  131. if (courses.length === 0) {
  132. AndroidBridge.showToast("未找到课程,请检查学年学期选择或登录状态。");
  133. return;
  134. }
  135. await window.AndroidBridgePromise.saveCourseConfig(JSON.stringify({ semesterTotalWeeks: 20, firstDayOfWeek: 1 }));
  136. await window.AndroidBridgePromise.savePresetTimeSlots(JSON.stringify(TIME_SLOTS));
  137. await window.AndroidBridgePromise.saveImportedCourses(JSON.stringify(courses));
  138. AndroidBridge.showToast(`成功导入 ${courses.length} 门课程`);
  139. AndroidBridge.notifyTaskCompletion();
  140. } catch (err) {
  141. AndroidBridge.showToast("错误: " + err.message);
  142. }
  143. }
  144. runImportFlow();