cqjtu.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. // ===== 登录检查 =====
  2. const checkLogin = () => {
  3. const hostnameOk = window.location.hostname === "jwgln.cqjtu.edu.cn";
  4. const nameEl = document.querySelector(".userInfo span:last-child");
  5. const nameOk = nameEl && nameEl.innerText.trim().length > 0;
  6. return hostnameOk && nameOk;
  7. };
  8. const getUserName = () => {
  9. const el = document.querySelector(".userInfo span:last-child");
  10. return el ? el.innerText.trim() : "";
  11. };
  12. // ===== 工具函数 =====
  13. // 周次展开: "1-9,11-16(单周)" → [1,3,5,7,9,11,13,15]
  14. function parseWeeks(weekStr) {
  15. weekStr = weekStr.replace(/\[\d+(?:-\d+)?节\]$/, "");
  16. const typeMatch = weekStr.match(/\(([^)]+)\)$/);
  17. const weekType = typeMatch ? typeMatch[1] : "周";
  18. const pureWeekStr = weekStr.replace(/\([^)]+\)$/, "");
  19. const weekRanges = pureWeekStr.split(",");
  20. let weeks = [];
  21. for (const range of weekRanges) {
  22. const parts = range.split("-");
  23. const start = Number(parts[0]);
  24. const end = parts.length > 1 ? Number(parts[1]) : start;
  25. for (let i = start; i <= end; i++) {
  26. weeks.push(i);
  27. }
  28. }
  29. if (weekType === "单周") {
  30. weeks = weeks.filter((w) => w % 2 === 1);
  31. } else if (weekType === "双周") {
  32. weeks = weeks.filter((w) => w % 2 === 0);
  33. }
  34. return weeks;
  35. }
  36. // 提取节次: "1-9,11-16(周)[01-02节]" → { start: 1, end: 2 }
  37. function parseSections(weekStr) {
  38. const match = weekStr.match(/\[(\d+)(?:-(\d+))?节\]/);
  39. if (!match) return null;
  40. const start = Number(match[1]);
  41. const end = match[2] ? Number(match[2]) : start;
  42. return { start, end };
  43. }
  44. // 格式化分钟为 HH:MM
  45. function formatTime(minutes) {
  46. const h = Math.floor(minutes / 60);
  47. const m = minutes % 60;
  48. return `${String(h).padStart(2, "0")}:${String(m).padStart(2, "0")}`;
  49. }
  50. // 计算时间槽
  51. function calculateTimeSlots(sectionNumbers, startH, startM, endH, endM) {
  52. const n = sectionNumbers.length;
  53. const totalMinutes = endH * 60 + endM - (startH * 60 + startM);
  54. const rawDuration = Math.floor(totalMinutes / n);
  55. const z = Math.floor(rawDuration / 5) * 5;
  56. const interval = n > 1 ? Math.floor((totalMinutes - z * n) / (n - 1)) : 0;
  57. return sectionNumbers.map((num, idx) => {
  58. const offset = idx * (z + interval);
  59. const start = startH * 60 + startM + offset;
  60. const end = start + z;
  61. return {
  62. number: num,
  63. startTime: formatTime(start),
  64. endTime: formatTime(end),
  65. };
  66. });
  67. }
  68. // 根据学期值计算开学日期, 就是个简单计算,谁还不能自己手动调了,鬼知道教务处要怎么安排后续的学期
  69. function getSemesterStartDate(semesterValue) {
  70. const year = parseInt(semesterValue.substring(0, 4));
  71. const termType = semesterValue.slice(-1);
  72. if (termType === "1") {
  73. return `${year}-09-08`;
  74. } else {
  75. return `${year + 1}-03-02`;
  76. }
  77. }
  78. // 从timeSlots计算课程配置
  79. function getSemesterConfig(timeSlots) {
  80. if (timeSlots.length === 0) {
  81. return {};
  82. }
  83. const [sH, sM] = timeSlots[0].startTime.split(":").map(Number);
  84. const [eH, eM] = timeSlots[0].endTime.split(":").map(Number);
  85. const classDuration = eH * 60 + eM - (sH * 60 + sM);
  86. const defaultClassDuration = Math.round(classDuration / 5) * 5;
  87. let defaultBreakDuration = 5;
  88. if (timeSlots.length >= 2) {
  89. const [e1H, e1M] = timeSlots[0].endTime.split(":").map(Number);
  90. const [s2H, s2M] = timeSlots[1].startTime.split(":").map(Number);
  91. defaultBreakDuration = s2H * 60 + s2M - (e1H * 60 + e1M);
  92. }
  93. return {
  94. defaultClassDuration,
  95. defaultBreakDuration,
  96. semesterTotalWeeks: 18,
  97. firstDayOfWeek: 1,
  98. };
  99. }
  100. // 在主文档和frames中查找元素
  101. function findElementInFrames(selector) {
  102. let el = document.querySelector(selector);
  103. if (el) return el;
  104. for (let i = 0; i < window.frames.length; i++) {
  105. try {
  106. const frameDoc = window.frames[i].document;
  107. if (frameDoc) {
  108. el = frameDoc.querySelector(selector);
  109. if (el) return el;
  110. }
  111. } catch (e) {}
  112. }
  113. return null;
  114. }
  115. // ===== 主解析函数 =====
  116. function parseSchedule() {
  117. let table = findElementInFrames("#timetable");
  118. if (!table) {
  119. AndroidBridge.showToast("请去往学期理论课表界面");
  120. return [];
  121. }
  122. const courses = [];
  123. const timeSlots = [];
  124. const tbody = table.querySelector("tbody");
  125. if (!tbody) {
  126. const rows = table.querySelectorAll("tr");
  127. if (rows.length > 0) {
  128. return parseRowsDirectly(rows);
  129. }
  130. AndroidBridge.showToast("课表结构异常");
  131. return [];
  132. }
  133. const rows = tbody.querySelectorAll("tr");
  134. for (let i = 1; i < rows.length; i++) {
  135. const row = rows[i];
  136. const th = row.querySelector("th");
  137. if (!th) continue;
  138. const thMatch = th.innerText.match(/\(([\d,]+)小节\)/);
  139. if (!thMatch) continue;
  140. const sectionNumbers = thMatch[1].split(",").map(Number);
  141. const baseStart = sectionNumbers[0];
  142. const baseEnd = sectionNumbers[sectionNumbers.length - 1];
  143. // 提取时间范围并计算时间槽
  144. const timeMatch = th.innerText.match(/(\d+):(\d+)-(\d+):(\d+)/);
  145. if (timeMatch) {
  146. const startH = Number(timeMatch[1]);
  147. const startM = Number(timeMatch[2]);
  148. const endH = Number(timeMatch[3]);
  149. const endM = Number(timeMatch[4]);
  150. const slots = calculateTimeSlots(
  151. sectionNumbers,
  152. startH,
  153. startM,
  154. endH,
  155. endM,
  156. );
  157. timeSlots.push(...slots);
  158. }
  159. const tds = row.querySelectorAll("td");
  160. for (let day = 1; day <= 7 && day <= tds.length; day++) {
  161. const td = tds[day - 1];
  162. const allKbDivs = td.querySelectorAll("div.kbcontent");
  163. const kbDivs = Array.from(allKbDivs).filter((div) => {
  164. const style = div.getAttribute("style") || "";
  165. return !style.includes("display:none");
  166. });
  167. if (kbDivs.length === 0) continue;
  168. for (const kbDiv of kbDivs) {
  169. const html = kbDiv.innerHTML;
  170. const parts = html.split("</font>---------------------<br>");
  171. for (const part of parts) {
  172. const firstFontMatch = part.match(
  173. /<font onmouseover="kbtc\(this\)" onmouseout="kbot\(this\)">([^<]*)<\/font>/,
  174. );
  175. if (!firstFontMatch) continue;
  176. const name = firstFontMatch[1].trim();
  177. if (!name) continue;
  178. const teacherMatch = part.match(
  179. /<font title="教师"[^>]*>([^<]*)<\/font>/,
  180. );
  181. const teacher = teacherMatch ? teacherMatch[1].trim() : "未知";
  182. const weeksMatch = part.match(
  183. /<font title="周次\(节次\)"[^>]*>([^<]*)<\/font>/,
  184. );
  185. if (!weeksMatch) continue;
  186. const weeksStr = weeksMatch[1].trim();
  187. const posMatch = part.match(
  188. /<font title="教室"[^>]*>([^<]*)<\/font>/,
  189. );
  190. const position = posMatch ? posMatch[1].trim() : "";
  191. let startSection = baseStart;
  192. let endSection = baseEnd;
  193. const sectionInfo = parseSections(weeksStr);
  194. if (sectionInfo) {
  195. startSection = sectionInfo.start;
  196. endSection = sectionInfo.end;
  197. }
  198. const weeks = parseWeeks(weeksStr);
  199. if (weeks.length === 0) continue;
  200. courses.push({
  201. name,
  202. teacher,
  203. position,
  204. day,
  205. startSection,
  206. endSection,
  207. weeks,
  208. });
  209. }
  210. }
  211. }
  212. }
  213. return { courses, timeSlots };
  214. }
  215. // 直接解析table的rows(无tbody的情况)
  216. function parseRowsDirectly(rows) {
  217. const courses = [];
  218. const timeSlots = [];
  219. for (let i = 1; i < rows.length; i++) {
  220. const row = rows[i];
  221. const th = row.querySelector("th");
  222. if (!th) continue;
  223. const thMatch = th.innerText.match(/\(([\d,]+)小节\)/);
  224. if (!thMatch) continue;
  225. const sectionNumbers = thMatch[1].split(",").map(Number);
  226. const baseStart = sectionNumbers[0];
  227. const baseEnd = sectionNumbers[sectionNumbers.length - 1];
  228. const timeMatch = th.innerText.match(/(\d+):(\d+)-(\d+):(\d+)/);
  229. if (timeMatch) {
  230. const startH = Number(timeMatch[1]);
  231. const startM = Number(timeMatch[2]);
  232. const endH = Number(timeMatch[3]);
  233. const endM = Number(timeMatch[4]);
  234. const slots = calculateTimeSlots(
  235. sectionNumbers,
  236. startH,
  237. startM,
  238. endH,
  239. endM,
  240. );
  241. timeSlots.push(...slots);
  242. }
  243. const tds = row.querySelectorAll("td");
  244. for (let day = 1; day <= 7 && day <= tds.length; day++) {
  245. const td = tds[day - 1];
  246. const allKbDivs = td.querySelectorAll("div.kbcontent");
  247. const kbDivs = Array.from(allKbDivs).filter((div) => {
  248. const style = div.getAttribute("style") || "";
  249. return !style.includes("display:none");
  250. });
  251. if (kbDivs.length === 0) continue;
  252. for (const kbDiv of kbDivs) {
  253. const html = kbDiv.innerHTML;
  254. const parts = html.split("</font>---------------------<br>");
  255. for (const part of parts) {
  256. const firstFontMatch = part.match(
  257. /<font onmouseover="kbtc\(this\)" onmouseout="kbot\(this\)">([^<]*)<\/font>/,
  258. );
  259. if (!firstFontMatch) continue;
  260. const name = firstFontMatch[1].trim();
  261. if (!name) continue;
  262. const teacherMatch = part.match(
  263. /<font title="教师"[^>]*>([^<]*)<\/font>/,
  264. );
  265. const teacher = teacherMatch ? teacherMatch[1].trim() : "未知";
  266. const weeksMatch = part.match(
  267. /<font title="周次\(节次\)"[^>]*>([^<]*)<\/font>/,
  268. );
  269. if (!weeksMatch) continue;
  270. const weeksStr = weeksMatch[1].trim();
  271. const posMatch = part.match(
  272. /<font title="教室"[^>]*>([^<]*)<\/font>/,
  273. );
  274. const position = posMatch ? posMatch[1].trim() : "";
  275. let startSection = baseStart;
  276. let endSection = baseEnd;
  277. const sectionInfo = parseSections(weeksStr);
  278. if (sectionInfo) {
  279. startSection = sectionInfo.start;
  280. endSection = sectionInfo.end;
  281. }
  282. const weeks = parseWeeks(weeksStr);
  283. if (weeks.length === 0) continue;
  284. courses.push({
  285. name,
  286. teacher,
  287. position,
  288. day,
  289. startSection,
  290. endSection,
  291. weeks,
  292. });
  293. }
  294. }
  295. }
  296. }
  297. return { courses, timeSlots };
  298. }
  299. // ===== 保存函数 =====
  300. async function saveCourses(courses) {
  301. await window.AndroidBridgePromise.saveImportedCourses(
  302. JSON.stringify(courses),
  303. );
  304. }
  305. async function saveTimeSlots(timeSlots) {
  306. await window.AndroidBridgePromise.savePresetTimeSlots(
  307. JSON.stringify(timeSlots),
  308. );
  309. }
  310. async function saveConfig(config) {
  311. await window.AndroidBridgePromise.saveCourseConfig(JSON.stringify(config));
  312. }
  313. // ===== 主流程 =====
  314. (async () => {
  315. if (!checkLogin()) {
  316. AndroidBridge.showToast("尚未登录,请先登录!");
  317. return;
  318. }
  319. const { courses, timeSlots } = parseSchedule();
  320. if (courses.length === 0) {
  321. AndroidBridge.showToast("未解析到任何课程");
  322. return;
  323. }
  324. // 获取学期配置
  325. const semesterSelect = findElementInFrames("#xnxq01id");
  326. const courseConfigData = getSemesterConfig(timeSlots);
  327. if (semesterSelect) {
  328. courseConfigData.semesterStartDate = getSemesterStartDate(
  329. semesterSelect.value,
  330. );
  331. }
  332. console.log("准备保存课程:", courses.length, "门");
  333. console.log("准备保存时间槽:", timeSlots.length, "个");
  334. console.log("准备保存配置:", JSON.stringify(courseConfigData));
  335. await saveCourses(courses);
  336. await saveTimeSlots(timeSlots);
  337. await saveConfig(courseConfigData);
  338. AndroidBridge.showToast(`导入成功!${courses.length}门课程`);
  339. AndroidBridge.notifyTaskCompletion();
  340. })();