cqcst_01.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. async function runImportFlow() {
  2. // 兼容电脑端测试
  3. if (typeof window.AndroidBridgePromise === 'undefined') {
  4. window.AndroidBridgePromise = {
  5. showAlert: async () => true,
  6. saveImportedCourses: async (json) => {
  7. console.log("===============================");
  8. console.log("🎉 【解析成功】以下是整理好的课表数据:");
  9. console.table(JSON.parse(json));
  10. console.log("===============================");
  11. alert("抓取成功!请在 F12 控制台查看具体的课程数据格式。");
  12. return true;
  13. }
  14. };
  15. window.AndroidBridge = {
  16. showToast: (msg) => console.log("[系统提示] " + msg),
  17. notifyTaskCompletion: () => console.log("[流程结束] 任务已完成并通知APP")
  18. };
  19. }
  20. AndroidBridge.showToast("开始提取课表数据...");
  21. const table = document.getElementById('kbtable') || document.querySelector('.table_border') || document.querySelector('table');
  22. if (!table || !table.innerText.includes('星期')) {
  23. AndroidBridge.showToast("没找到课表!请确保您当前在“学期理论课表”页面。");
  24. return;
  25. }
  26. const alertConfirmed = await window.AndroidBridgePromise.showAlert(
  27. "强智教务解析",
  28. "已检测到课表页面,是否提取数据并导入?",
  29. "确认导入"
  30. );
  31. if (!alertConfirmed) return;
  32. try {
  33. let courses = [];
  34. let courseSet = new Set();
  35. let rows = table.querySelectorAll('tr');
  36. // 遍历课表每一行(跳过第一行的表头)
  37. for (let i = 1; i < rows.length; i++) {
  38. // 【关键修复1】同时获取 th 和 td,防止错位
  39. let cells = rows[i].querySelectorAll('td, th');
  40. for (let j = 0; j < cells.length; j++) {
  41. let cell = cells[j];
  42. // 【关键修复2】逆向计算星期几:倒数第7列永远是周一,倒数第1列永远是周日
  43. // 这能完美解决强智系统左侧节次列导致的数据错位问题
  44. let day = 7 - (cells.length - 1 - j);
  45. if (day < 1 || day > 7) continue; // 如果算出来不是1-7,说明是左侧的节次列,跳过
  46. let blocks = cell.innerText.split(/-{5,}/).map(t => t.trim()).filter(t => t);
  47. for (let block of blocks) {
  48. if (!block || block === ' ' || block === '') continue;
  49. let lines = block.split(/\n/).map(l => l.trim()).filter(l => l);
  50. if(lines.length < 4) {
  51. lines = block.split(/\s+/).map(l => l.trim()).filter(l => l);
  52. }
  53. if (lines.length < 3) continue;
  54. let name = lines[0].replace(/\[.*?\]/g, '').trim();
  55. let teacher = lines[1] || "未知";
  56. let timeRegex = /([\d\-,]+)(?:\((单|双|.*?)\))?.*?\[([\d\-]+)节\]/;
  57. let timeLineIdx = lines.findIndex(l => timeRegex.test(l));
  58. if (timeLineIdx === -1) continue;
  59. let match = lines[timeLineIdx].match(timeRegex);
  60. let weeksStr = match[1];
  61. let oddEven = match[2];
  62. let sectionsStr = match[3];
  63. let position = (timeLineIdx + 1 < lines.length) ? lines[timeLineIdx + 1] : "未知地点";
  64. let weeks = [];
  65. let weekParts = weeksStr.split(',');
  66. for (let wp of weekParts) {
  67. if (wp.includes('-')) {
  68. let parts = wp.split('-');
  69. let start = parseInt(parts[0]);
  70. let end = parseInt(parts[1]);
  71. for (let w = start; w <= end; w++) {
  72. if (oddEven === '单' && w % 2 === 0) continue;
  73. if (oddEven === '双' && w % 2 !== 0) continue;
  74. weeks.push(w);
  75. }
  76. } else {
  77. weeks.push(parseInt(wp));
  78. }
  79. }
  80. let secParts = sectionsStr.split('-');
  81. let startSection = parseInt(secParts[0]);
  82. let endSection = parseInt(secParts[secParts.length - 1]);
  83. let uid = `${name}-${day}-${startSection}-${endSection}-${weeks.join(',')}`;
  84. if (!courseSet.has(uid)) {
  85. courseSet.add(uid);
  86. courses.push({
  87. name: name,
  88. teacher: teacher,
  89. position: position,
  90. day: day,
  91. startSection: startSection,
  92. endSection: endSection,
  93. weeks: weeks
  94. });
  95. }
  96. }
  97. }
  98. }
  99. if (courses.length === 0) {
  100. AndroidBridge.showToast("没有抓取到数据,可能当前表格为空。");
  101. return;
  102. }
  103. AndroidBridge.showToast(`提取成功,共发现 ${courses.length} 门课程,正在保存...`);
  104. const saveResult = await window.AndroidBridgePromise.saveImportedCourses(JSON.stringify(courses));
  105. if (saveResult) {
  106. AndroidBridge.showToast("导入大功告成!");
  107. AndroidBridge.notifyTaskCompletion();
  108. }
  109. } catch (error) {
  110. console.error("解析过程中发生错误:", error);
  111. AndroidBridge.showToast("解析出错啦: " + error.message);
  112. }
  113. }
  114. runImportFlow();