GDKM.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. //https://jw.gdkm.edu.cn/jsxsd/xskb/xskb_list.do
  2. // 广东科贸职业学院(gdkm.edu.cn) 拾光课程表适配脚本
  3. // 参考南昌航空大学科技学院(stcnchu.edu.cn) 拾光课程表适配脚本
  4. // 非该大学开发者适配,开发者无法及时发现问题
  5. // 出现问题请提联系开发者或者提交pr更改,这更加快速
  6. // 工具函数
  7. window.validateYearInput = function(input) {
  8. return /^[0-9]{4}$/.test(input) ? false : "请输入四位数字的学年喵~";
  9. };
  10. /**
  11. * 节次合并与去重
  12. */
  13. function mergeAndDistinctCourses(courses) {
  14. if (courses.length <= 1) return courses;
  15. courses.sort((a, b) => {
  16. return a.name.localeCompare(b.name) ||
  17. a.day - b.day ||
  18. a.startSection - b.startSection ||
  19. a.weeks.join(',').localeCompare(b.weeks.join(','));
  20. });
  21. const merged = [];
  22. let current = courses[0];
  23. for (let i = 1; i < courses.length; i++) {
  24. const next = courses[i];
  25. const isSameCourse =
  26. current.name === next.name &&
  27. current.teacher === next.teacher &&
  28. current.position === next.position &&
  29. current.day === next.day &&
  30. current.weeks.join(',') === next.weeks.join(',');
  31. const isContinuous = current.endSection + 1 === next.startSection;
  32. if (isSameCourse && isContinuous) {
  33. current.endSection = next.endSection;
  34. } else if (isSameCourse && current.startSection === next.startSection && current.endSection === next.endSection) {
  35. continue;
  36. } else {
  37. merged.push(current);
  38. current = next;
  39. }
  40. }
  41. merged.push(current);
  42. return merged;
  43. }
  44. // 核心解析逻辑
  45. function parseTimetableToModel(doc) {
  46. let timetable = doc.querySelector('table.qz-weeklyTable');
  47. if (!timetable) {
  48. const tables = Array.from(doc.querySelectorAll('table'));
  49. timetable = tables.sort((a, b) =>
  50. b.querySelectorAll('td').length - a.querySelectorAll('td').length
  51. )[0];
  52. }
  53. if (!timetable) return [];
  54. const rawCourses = [];
  55. const rows = Array.from(timetable.querySelectorAll('tbody tr'));
  56. // rowspan 占位表:key = 行下标,value = 该行被上方 rowspan 占用的列集合
  57. // 列号从 1 开始(1 = 周一)
  58. const occupied = {}; // { [rowIndex]: Set<col> }
  59. rows.forEach((row, rowIndex) => {
  60. const timeTd = row.querySelector('td[name="timeTd"]');
  61. const sectionText = timeTd ? timeTd.innerText : '';
  62. const rowSection = parseSectionFromLabel(sectionText);
  63. const courseTds = Array.from(row.querySelectorAll('td[name="kbDataTd"]'));
  64. // 本行被占用的列
  65. const occupiedCols = occupied[rowIndex] || new Set();
  66. let col = 1; // 从第 1 列(周一)开始
  67. let tdIndex = 0; // courseTds 的下标
  68. while (tdIndex < courseTds.length) {
  69. // 跳过被上方 rowspan 占用的列
  70. while (occupiedCols.has(col)) col++;
  71. const td = courseTds[tdIndex];
  72. const day = col; // 这个 td 真正对应的星期几
  73. // 处理这个 td 里的课程
  74. const items = td.querySelectorAll('li.courselists-item');
  75. items.forEach(li => {
  76. const name = li.querySelector('.qz-hasCourse-title')?.innerText.trim() || '';
  77. if (!name) return;
  78. const abbr = li.querySelector('.qz-hasCourse-abbrinfo')?.innerText || '';
  79. const teacher = extractField(abbr, '老师') || '未知教师';
  80. const position = extractField(abbr, '地点') || '未知地点';
  81. const weekStr = extractField(abbr, '时间') || '';
  82. const parsed = parseSectionFromWeekStr(weekStr);
  83. const finalStart = parsed.startSection || rowSection.startSection;
  84. const finalEnd = parsed.endSection || rowSection.endSection;
  85. if (finalStart > 0) {
  86. rawCourses.push({
  87. name,
  88. teacher,
  89. weeks: parseWeeks(weekStr),
  90. position,
  91. day,
  92. startSection: finalStart,
  93. endSection: finalEnd
  94. });
  95. }
  96. });
  97. // 看这个 td 有没有 rowspan
  98. const rowspan = parseInt(td.getAttribute('rowspan') || '1', 10);
  99. if (rowspan > 1) {
  100. // 它占用的列:接下来的 rowspan-1 行,这一列都不出现 td
  101. for (let r = rowIndex + 1; r < rowIndex + rowspan; r++) {
  102. if (!occupied[r]) occupied[r] = new Set();
  103. occupied[r].add(col);
  104. }
  105. }
  106. col++;
  107. tdIndex++;
  108. }
  109. });
  110. return mergeAndDistinctCourses(rawCourses);
  111. }
  112. //工具
  113. // 从 "老师:张三;时间:3周[1-6节];地点:xxx" 里取某个字段
  114. function extractField(text, key) {
  115. if (!text) return '';
  116. // 1. 找到 "老师:" 或 "老师:" 的位置
  117. let startIdx = -1;
  118. const colonVariants = [key + ':', key + ':'];
  119. for (let i = 0; i < colonVariants.length; i++) {
  120. startIdx = text.indexOf(colonVariants[i]);
  121. if (startIdx !== -1) {
  122. startIdx += colonVariants[i].length; // 跳过 key 和冒号
  123. break;
  124. }
  125. }
  126. if (startIdx === -1) return '';
  127. // 2. 从 startIdx 往后找第一个 ; 或 ;或换行
  128. let endIdx = text.length;
  129. for (let i = startIdx; i < text.length; i++) {
  130. const ch = text[i];
  131. if (ch === ';' || ch === ';' || ch === '\n') {
  132. endIdx = i;
  133. break;
  134. }
  135. }
  136. return text.substring(startIdx, endIdx).trim();
  137. }
  138. // 从 "第一大节 (01、02小节)" 里取节次
  139. function parseSectionFromLabel(text) {
  140. let startSection = 0, endSection = 0;
  141. if (!text) return { startSection, endSection };
  142. // 找 "(" 和 "小节"
  143. const openIdx = Math.max(text.indexOf('('), text.indexOf('('));
  144. const closeIdx = text.indexOf('小节', openIdx);
  145. if (openIdx === -1 || closeIdx === -1) return { startSection, endSection };
  146. const inside = text.substring(openIdx + 1, closeIdx); // 例如 "01、02"
  147. // 用 、 或 , 切开
  148. const parts = inside.split(/[、,,]/);
  149. const nums = [];
  150. parts.forEach(p => {
  151. const n = parseInt(p.trim(), 10);
  152. if (!isNaN(n)) nums.push(n);
  153. });
  154. if (nums.length > 0) {
  155. startSection = nums[0];
  156. endSection = nums[nums.length - 1];
  157. }
  158. return { startSection, endSection };
  159. }
  160. // 从 "3周[1-6节]" 里取节次
  161. function parseSectionFromWeekStr(weekStr) {
  162. let startSection = 0, endSection = 0;
  163. if (!weekStr) return { startSection, endSection };
  164. // 找 [ 和 节]
  165. const openIdx = weekStr.indexOf('[');
  166. const closeIdx = weekStr.indexOf('节]', openIdx);
  167. if (openIdx === -1 || closeIdx === -1) return { startSection, endSection };
  168. const inside = weekStr.substring(openIdx + 1, closeIdx); // 例如 "1-6" 或 "5-8"
  169. const dashIdx = inside.indexOf('-');
  170. if (dashIdx === -1) {
  171. // 只有一个数字
  172. const n = parseInt(inside.trim(), 10);
  173. if (!isNaN(n)) {
  174. startSection = n;
  175. endSection = n;
  176. }
  177. } else {
  178. const a = parseInt(inside.substring(0, dashIdx).trim(), 10);
  179. const b = parseInt(inside.substring(dashIdx + 1).trim(), 10);
  180. if (!isNaN(a)) startSection = a;
  181. if (!isNaN(b)) endSection = b;
  182. }
  183. return { startSection, endSection };
  184. }
  185. // 从 "3-4,9-18周" 里解析出所有周次
  186. // 返回 [3,4,9,10,11,...,18] 这样的数组
  187. function parseWeeks(weekStr) {
  188. if (!weekStr) return [];
  189. // 1. 去掉 "周" 之后的内容,比如 "3-4,9-18周[1-2节]" -> "3-4,9-18"
  190. let endIdx = weekStr.indexOf('周');
  191. let core = endIdx === -1 ? weekStr : weekStr.substring(0, endIdx);
  192. // 2. 去掉 "[" 之前可能残留的东西(保险)
  193. const bracketIdx = core.indexOf('[');
  194. if (bracketIdx !== -1) core = core.substring(0, bracketIdx);
  195. const weeks = [];
  196. // 3. 按逗号切开,每段可能是 "3" 或 "9-18"
  197. const segments = core.split(/[,,]/);
  198. segments.forEach(seg => {
  199. seg = seg.trim();
  200. if (!seg) return;
  201. const dashIdx = seg.indexOf('-');
  202. if (dashIdx === -1) {
  203. // 单个周
  204. const n = parseInt(seg, 10);
  205. if (!isNaN(n)) weeks.push(n);
  206. } else {
  207. // 范围周
  208. const a = parseInt(seg.substring(0, dashIdx).trim(), 10);
  209. const b = parseInt(seg.substring(dashIdx + 1).trim(), 10);
  210. if (!isNaN(a) && !isNaN(b)) {
  211. for (let i = a; i <= b; i++) weeks.push(i);
  212. }
  213. }
  214. });
  215. // 4. 去重 + 排序
  216. const unique = [];
  217. weeks.forEach(w => {
  218. if (unique.indexOf(w) === -1) unique.push(w);
  219. });
  220. unique.sort((a, b) => a - b);
  221. return unique;
  222. }
  223. // 配置与流程
  224. async function saveAppConfig(semesterStartDate,semesterTotalWeeks) {
  225. const config = { "semesterTotalWeeks": semesterTotalWeeks, "firstDayOfWeek": 1 ,"semesterStartDate" : semesterStartDate};
  226. return await window.shiguangBridgePromise.saveCourseConfig(JSON.stringify(config));
  227. }
  228. /**
  229. * 返回作息
  230. */
  231. async function saveAppTimeSlots() {
  232. const slots = [
  233. { "number": 1, "startTime": "08:30", "endTime": "09:10" },
  234. { "number": 2, "startTime": "09:20", "endTime": "10:00" },
  235. { "number": 3, "startTime": "10:20", "endTime": "11:00" },
  236. { "number": 4, "startTime": "11:10", "endTime": "11:50" },
  237. { "number": 5, "startTime": "14:00", "endTime": "14:40" },
  238. { "number": 6, "startTime": "14:50", "endTime": "15:20" },
  239. { "number": 7, "startTime": "15:30", "endTime": "16:10" },
  240. { "number": 8, "startTime": "16:20", "endTime": "16:50" },
  241. { "number": 9, "startTime": "18:30", "endTime": "19:10" },
  242. { "number": 10, "startTime": "19:20", "endTime": "19:50" },
  243. { "number": 11, "startTime": "20:00", "endTime": "20:40" },
  244. { "number": 12, "startTime": "20:50", "endTime": "21:20" },
  245. ]
  246. return await window.shiguangBridgePromise.savePresetTimeSlots(JSON.stringify(slots));
  247. }
  248. window.isYMD = function(str) {
  249. // 1. 格式匹配:4位-2位-2位
  250. if (!/^\d{4}-\d{2}-\d{2}$/.test(str)) return "开始日期格式错误了喵~。请再次输入喵~";
  251. // 2. 校验是否是真实存在的日期(防止 2024-02-30)
  252. const [y, m, d] = str.split('-').map(Number);
  253. const date = new Date(y, m - 1, d);
  254. if (! (date.getFullYear() === y && date.getMonth() === m - 1 &&date.getDate() === d)){
  255. return "开始日期格式错误了喵~。请再次输入喵~"
  256. }
  257. return false
  258. }
  259. // ================= 流程编排 =================
  260. window.semesterTotalWeeksInput = function(str){
  261. return /^-?\d+$/.test(str) ? false:"请输入周数喵~只要数字喵~"
  262. }
  263. async function runImportFlow() {
  264. try {
  265. const confirmed = await window.shiguangBridgePromise.showAlert("提示喵~", "请确保已成功登录教务系统喵~。是否开始导入?", "开始");
  266. if (!confirmed) return;
  267. /*
  268. // 1. 获取就读校区
  269. const campusIndex = await window.shiguangBridgePromise.showSingleSelection("选择所在校区喵~", JSON.stringify(["清远校区", "广州白云校区","广州天河校区"]), -1);
  270. if (campusIndex === null) return;
  271. */
  272. // 2. 获取学年
  273. const year = await window.shiguangBridgePromise.showPrompt("选择学年喵~", "请输入要导入课程的起始学年喵~(例如 2025-2026 应输入2025):", "", "validateYearInput");
  274. if (!year) return;
  275. // 3. 获取学期并记录索引
  276. const semesterIndex = await window.shiguangBridgePromise.showSingleSelection("选择学期喵~", JSON.stringify(["第一学期", "第二学期"]), -1);
  277. if (semesterIndex === null) return;
  278. const semesterId = `${year}-${parseInt(year) + 1}-${semesterIndex + 1}`;
  279. // 4. 获得学期开始日期
  280. const semesterStartDate = await window.shiguangBridgePromise.showPrompt("选择开始日期喵~", "请输入要导入课程的开始日期喵~ 格式为:YYYY-MM-DD,例如1145-01-04", "", "isYMD");
  281. // 2. 获取周数
  282. const semesterTotalWeeks = await window.shiguangBridgePromise.showPrompt("选择周数喵~", "请输入要导入课程的周数喵~(例如 20):", "", "semesterTotalWeeksInput");
  283. if (!semesterTotalWeeks) return;
  284. window.shiguangBridge.showToast("正在请求数据喵~");
  285. const url = `https://jw.gdkm.edu.cn/jsxsd/xskb/xskb_list.do?viweType=0&xnxq01id=${semesterId}&zc=`;
  286. const response = await fetch(url, {
  287. method: "GET",
  288. credentials: "include",
  289. headers: {
  290. "Referer": "https://jw.gdkm.edu.cn/jsxsd/"
  291. }
  292. });
  293. const html = await response.text();
  294. const finalCourses = parseTimetableToModel(new DOMParser().parseFromString(html, "text/html"));
  295. if (finalCourses.length === 0) {
  296. window.shiguangBridge.showToast("未发现课程,请检查学期选择或登录状态喵~");
  297. return;
  298. }
  299. // 保存全局设置
  300. await saveAppConfig(semesterStartDate,parseInt(semesterTotalWeeks));
  301. // 传入作息
  302. await saveAppTimeSlots();
  303. // 保存课程
  304. await window.shiguangBridgePromise.saveImportedCourses(JSON.stringify(finalCourses));
  305. window.shiguangBridge.showToast(`成功导入 ${finalCourses.length} 门课程喵~`);
  306. window.shiguangBridge.notifyTaskCompletion();
  307. } catch (error) {
  308. window.shiguangBridge.showToast("异常喵~ " + error.message);
  309. }
  310. }
  311. // 启动
  312. runImportFlow();