zhengfang_01.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // 基于 HTML 页面抓取的拾光课表正方适配脚本
  2. /**
  3. * 解析表格
  4. */
  5. function parserTbale() {
  6. const regexName = /[●★○]/g;
  7. const courseInfoList = [];
  8. const $ = window.jQuery;
  9. if (!$) return courseInfoList;
  10. $('#kbgrid_table_0 td').each((i, td) => {
  11. if ($(td).hasClass('td_wrap') && $(td).text().trim() !== '') {
  12. const day = parseInt($(td).attr('id').split('-')[0]);
  13. $(td).find('.timetable_con.text-left').each((i, course) => {
  14. const name = $(course).find('.title font').text().replace(regexName, '').trim();
  15. const infoStr = $(course).find('p').eq(0).find('font').eq(1).text().trim();
  16. const position = $(course).find('p').eq(1).find('font').text().trim();
  17. const teacher = $(course).find('p').eq(2).find('font').text().trim();
  18. if (infoStr && infoStr.match(/\((\d+-\d+节)\)/) && infoStr.split('节)')[1]) {
  19. const [sections, weeks] = parserInfo(infoStr);
  20. if (name && position && teacher && sections.length && weeks.length) {
  21. const startSection = sections[0];
  22. const endSection = sections[sections.length - 1];
  23. const finalPosition = position.split(/\s+/).pop();
  24. const data = { name, day, weeks, teacher, position: finalPosition, startSection, endSection };
  25. courseInfoList.push(data);
  26. }
  27. }
  28. });
  29. }
  30. });
  31. return courseInfoList;
  32. }
  33. /**
  34. * 解析列表
  35. */
  36. function parserList() {
  37. const regexName = /[●★○]/g;
  38. const regexWeekNum = /周数:|周/g;
  39. const regexPosition = /上课地点:/g;
  40. const regexTeacher = /教师 :/g;
  41. const $ = window.jQuery;
  42. if (!$) return [];
  43. let courseInfoList = [];
  44. $('#kblist_table tbody').each((day, tbody) => {
  45. if (day > 0 && day < 8) {
  46. let sections;
  47. $(tbody).find('tr:not(:first-child)').each((trIndex, tr) => {
  48. let name, font;
  49. if ($(tr).find('td').length > 1) {
  50. sections = parserSections($(tr).find('td:first-child').text());
  51. name = $(tr).find('td:nth-child(2)').find('.title').text().replace(regexName, '').trim();
  52. font = $(tr).find('td:nth-child(2)').find('p font');
  53. } else {
  54. name = $(tr).find('td').find('.title').text().replace(regexName, '').trim();
  55. font = $(tr).find('td').find('p font');
  56. }
  57. const weekStr = $(font[0]).text().replace(regexWeekNum, '').trim();
  58. const weeks = parserWeeks(weekStr);
  59. const positionRaw = $(font[1]).text().replace(regexPosition, '').trim();
  60. const finalPosition = positionRaw.split(/\s+/).pop();
  61. const teacher = $(font[2]).text().replace(regexTeacher, '').trim();
  62. if (name && sections && weeks.length && teacher && finalPosition) {
  63. const startSection = sections[0];
  64. const endSection = sections[sections.length - 1];
  65. const data = {
  66. name,
  67. day,
  68. weeks,
  69. teacher,
  70. position: finalPosition,
  71. startSection,
  72. endSection
  73. };
  74. courseInfoList.push(data);
  75. }
  76. });
  77. }
  78. });
  79. return courseInfoList;
  80. }
  81. /**
  82. * 解析课程信息
  83. */
  84. function parserInfo(str) {
  85. const sections = parserSections(str.match(/\((\d+-\d+节)\)/)[1].replace(/节/g, ''));
  86. const weekStrWithMarker = str.split('节)')[1];
  87. const weeks = parserWeeks(weekStrWithMarker.replace(/周/g, '').trim());
  88. return [sections, weeks];
  89. }
  90. /**
  91. * 解析节次
  92. */
  93. function parserSections(str) {
  94. const [start, end] = str.split('-').map(Number);
  95. if (isNaN(start) || isNaN(end) || start > end) return [];
  96. return Array.from({ length: end - start + 1 }, (_, i) => start + i);
  97. }
  98. /**
  99. * 解析周次
  100. */
  101. function parserWeeks(str) {
  102. const segments = str.split(',');
  103. let weeks = [];
  104. const segmentRegex = /(\d+)(?:-(\d+))?\s*(\([单双]\))?/g;
  105. for (const segment of segments) {
  106. // 清理段落中的周字和多余空格
  107. const cleanSegment = segment.replace(/周/g, '').trim();
  108. // 重置正则的 lastIndex
  109. segmentRegex.lastIndex = 0;
  110. let match;
  111. // 循环匹配一个段落内可能存在的所有周次定义(虽然通常只有一个)
  112. while ((match = segmentRegex.exec(cleanSegment)) !== null) {
  113. const start = parseInt(match[1]);
  114. const end = match[2] ? parseInt(match[2]) : start;
  115. const flagStr = match[3] || ''; // (单) 或 (双) 或 ""
  116. let flag = 0;
  117. if (flagStr.includes('单')) {
  118. flag = 1;
  119. } else if (flagStr.includes('双')) {
  120. flag = 2;
  121. }
  122. for (let i = start; i <= end; i++) {
  123. // 过滤单双周
  124. if (flag === 1 && i % 2 !== 1) continue; // 仅保留单周
  125. if (flag === 2 && i % 2 !== 0) continue; // 仅保留双周
  126. // 防止重复添加
  127. if (!weeks.includes(i)) {
  128. weeks.push(i);
  129. }
  130. }
  131. }
  132. }
  133. // 排序并返回唯一的周次列表
  134. return weeks.sort((a, b) => a - b);
  135. }
  136. async function scrapeAndParseCourses() {
  137. AndroidBridge.showToast("正在检查页面并抓取课程数据...");
  138. const ts = `1.登陆教务系统\n2.导航到学生课表查询页面\n3.等待课表信息加载,选择对应学年、学期,确认无误后点击【查询】\n4.确保页面上显示了课程表\n5.点击下方【一键导入】`
  139. try {
  140. const response = await fetch(window.location.href);
  141. const text = await response.text();
  142. if (!text.includes("课表查询")) {
  143. console.log("页面内容检查失败!");
  144. await window.AndroidBridgePromise.showAlert("导入失败", "当前页面似乎不是学生课表查询页面。请检查:\n" + ts, "确定");
  145. return null;
  146. }
  147. const typeElement = document.querySelector('#shcPDF');
  148. if (!typeElement) {
  149. console.log("未能找到视图类型元素 (#shcPDF)");
  150. await window.AndroidBridgePromise.showAlert("导入失败", "未能识别课表视图类型,请确认您已点击查询且课表已加载完毕。", "确定");
  151. return null;
  152. }
  153. const type = typeElement.dataset['type'];
  154. const tableElement = document.querySelector(type === 'list' ? '#kblist_table' : '#kbgrid_table_0');
  155. if (!tableElement) {
  156. console.log("未能找到课表主体 HTML");
  157. await window.AndroidBridgePromise.showAlert("导入失败", `未能找到课表主体 (${type} 视图),请确认您已点击查询且课表已加载完毕。`, "确定");
  158. return null;
  159. }
  160. let result = [];
  161. if (type === 'list') {
  162. result = parserList();
  163. } else {
  164. result = parserTbale();
  165. }
  166. if (result.length === 0) {
  167. AndroidBridge.showToast("未找到任何课程数据,请检查所选学年学期是否正确或本学期无课。");
  168. return null;
  169. }
  170. console.log(`JS: 课程数据解析成功,共找到 ${result.length} 门课程。`);
  171. return { courses: result };
  172. } catch (error) {
  173. AndroidBridge.showToast(`抓取或解析失败: ${error.message}`);
  174. console.error('JS: Scrape/Parse Error:', error);
  175. await window.AndroidBridgePromise.showAlert("抓取或解析失败", `发生错误:${error.message}。请重试或联系开发者。`, "确定");
  176. return null;
  177. }
  178. }
  179. async function saveCourses(parsedCourses) {
  180. AndroidBridge.showToast(`正在保存 ${parsedCourses.length} 门课程...`);
  181. console.log(`JS: 尝试保存 ${parsedCourses.length} 门课程...`);
  182. try {
  183. await window.AndroidBridgePromise.saveImportedCourses(JSON.stringify(parsedCourses, null, 2));
  184. console.log("JS: 课程保存成功!");
  185. return true;
  186. } catch (error) {
  187. AndroidBridge.showToast(`课程保存失败: ${error.message}`);
  188. console.error('JS: Save Courses Error:', error);
  189. return false;
  190. }
  191. }
  192. async function runImportFlow() {
  193. const alertConfirmed = await window.AndroidBridgePromise.showAlert(
  194. "教务系统课表导入",
  195. "导入前请确保您已在浏览器中成功登录教务系统,并处于课表查询页面且已点击查询。",
  196. "好的,开始导入"
  197. );
  198. if (!alertConfirmed) {
  199. AndroidBridge.showToast("用户取消了导入。");
  200. return;
  201. }
  202. if (typeof window.jQuery === 'undefined' && typeof $ === 'undefined') {
  203. const errorMsg = "当前教务系统页面似乎没有加载 jQuery 库。本脚本依赖 jQuery 进行 DOM 解析。";
  204. AndroidBridge.showToast(errorMsg);
  205. await window.AndroidBridgePromise.showAlert("导入失败", errorMsg + "\n请尝试刷新页面或使用其他导入方式。", "确定");
  206. console.error("JS: 缺少 jQuery 依赖,流程终止。");
  207. return;
  208. }
  209. const result = await scrapeAndParseCourses();
  210. if (result === null) {
  211. console.log("JS: 课程获取或解析失败,流程终止。");
  212. return;
  213. }
  214. const { courses } = result;
  215. const saveResult = await saveCourses(courses);
  216. if (!saveResult) {
  217. console.log("JS: 课程保存失败,流程终止。");
  218. return;
  219. }
  220. AndroidBridge.showToast(`课程导入成功,共导入 ${courses.length} 门课程!`);
  221. console.log("JS: 整个导入流程执行完毕并成功。");
  222. AndroidBridge.notifyTaskCompletion();
  223. }
  224. runImportFlow();