فهرست منبع

feat: 移除 jQuery 依赖,改用原生 DOM 方法解析课程数据

XingHeYuZhuan 2 ساعت پیش
والد
کامیت
53c8140a10
1فایلهای تغییر یافته به همراه59 افزوده شده و 75 حذف شده
  1. 59 75
      resources/zhengfang_jiaowu/zhengfang_01.js

+ 59 - 75
resources/zhengfang_jiaowu/zhengfang_01.js

@@ -6,20 +6,19 @@
 function parserTbale() {
     const regexName = /[●★○]/g;
     const courseInfoList = [];
-    const $ = window.jQuery; 
-    if (!$) return courseInfoList;
-
-    $('#kbgrid_table_0 td').each((i, td) => {
-        if ($(td).hasClass('td_wrap') && $(td).text().trim() !== '') {
-            const day = parseInt($(td).attr('id').split('-')[0]); 
-            
-            $(td).find('.timetable_con.text-left').each((i, course) => {
-                const name = $(course).find('.title font').text().replace(regexName, '').trim();
-                
-                const infoStr = $(course).find('p').eq(0).find('font').eq(1).text().trim(); 
-                
-                const position = $(course).find('p').eq(1).find('font').text().trim();
-                const teacher = $(course).find('p').eq(2).find('font').text().trim();
+
+    document.querySelectorAll('#kbgrid_table_0 td').forEach((td) => {
+        if (td.classList.contains('td_wrap') && td.textContent.trim() !== '') {
+            const day = parseInt(td.getAttribute('id').split('-')[0]);
+
+            td.querySelectorAll('.timetable_con.text-left').forEach((course) => {
+                const name = [...course.querySelectorAll('.title font')].map(e => e.textContent).join('').replace(regexName, '').trim();
+
+                const ps = course.querySelectorAll('p');
+                const infoStr = ps[0]?.querySelectorAll('font[color]')[1]?.textContent.trim() || '';
+                const position = ps[1] ? [...ps[1].querySelectorAll('font[color]')].map(e => e.textContent).join('').trim() : '';
+                const teacherRaw = ps[2] ? [...ps[2].querySelectorAll('font[color]')].map(e => e.textContent).join('').trim() : '';
+                const teacher = teacherRaw.replace(/[\((][^\))]*[\))]/g, '').trim();
 
                 if (infoStr && infoStr.match(/\((\d+-\d+节)\)/) && infoStr.split('节)')[1]) {
                     const [sections, weeks] = parserInfo(infoStr);
@@ -27,9 +26,9 @@ function parserTbale() {
                     if (name && position && teacher && sections.length && weeks.length) {
                         const startSection = sections[0];
                         const endSection = sections[sections.length - 1];
-                        
+
                         const finalPosition = position.split(/\s+/).pop();
-                        
+
                         const data = { name, day, weeks, teacher, position: finalPosition, startSection, endSection };
                         courseInfoList.push(data);
                     }
@@ -49,45 +48,46 @@ function parserList() {
     const regexPosition = /上课地点:/g;
     const regexTeacher = /教师 :/g;
 
-    const $ = window.jQuery; 
-    if (!$) return [];
-    
     let courseInfoList = [];
-    $('#kblist_table tbody').each((day, tbody) => {
-        if (day > 0 && day < 8) { 
+    document.querySelectorAll('#kblist_table tbody').forEach((tbody, day) => {
+        if (day > 0 && day < 8) {
             let sections;
-            $(tbody).find('tr:not(:first-child)').each((trIndex, tr) => {
+            tbody.querySelectorAll('tr:not(:first-child)').forEach((tr) => {
                 let name, font;
-                
-                if ($(tr).find('td').length > 1) { 
-                    sections = parserSections($(tr).find('td:first-child').text());
-                    name = $(tr).find('td:nth-child(2)').find('.title').text().replace(regexName, '').trim();
-                    font = $(tr).find('td:nth-child(2)').find('p font');
-                } else { 
-                    name = $(tr).find('td').find('.title').text().replace(regexName, '').trim();
-                    font = $(tr).find('td').find('p font');
+
+                if (tr.querySelectorAll('td').length > 1) {
+                    const firstTd = tr.querySelector('td:first-child');
+                    sections = parserSections(firstTd.textContent);
+                    const secondTd = tr.querySelector('td:nth-child(2)');
+                    name = [...secondTd.querySelectorAll('.title')].map(e => e.textContent).join('').replace(regexName, '').trim();
+                    font = secondTd.querySelectorAll('p font');
+                } else {
+                    const td = tr.querySelector('td');
+                    name = [...td.querySelectorAll('.title')].map(e => e.textContent).join('').replace(regexName, '').trim();
+                    font = td.querySelectorAll('p font');
                 }
-                
-                const weekStr = $(font[0]).text().replace(regexWeekNum, '').trim();
+
+                const weekStr = font[0] ? font[0].textContent.replace(regexWeekNum, '').trim() : '';
                 const weeks = parserWeeks(weekStr);
 
-                const positionRaw = $(font[1]).text().replace(regexPosition, '').trim(); 
+                const positionRaw = font[1] ? font[1].textContent.replace(regexPosition, '').trim() : '';
                 const finalPosition = positionRaw.split(/\s+/).pop();
-                
-                const teacher = $(font[2]).text().replace(regexTeacher, '').trim();
-                
+
+                const teacherRaw = font[2] ? font[2].textContent.replace(regexTeacher, '').trim() : '';
+                const teacher = teacherRaw.replace(/[\((][^\))]*[\))]/g, '').trim();
+
                 if (name && sections && weeks.length && teacher && finalPosition) {
                     const startSection = sections[0];
                     const endSection = sections[sections.length - 1];
-                    
-                    const data = { 
-                        name, 
-                        day, 
-                        weeks, 
-                        teacher, 
-                        position: finalPosition, 
-                        startSection, 
-                        endSection 
+
+                    const data = {
+                        name,
+                        day,
+                        weeks,
+                        teacher,
+                        position: finalPosition,
+                        startSection,
+                        endSection
                     };
                     courseInfoList.push(data);
                 }
@@ -125,19 +125,15 @@ function parserWeeks(str) {
     const segmentRegex = /(\d+)(?:-(\d+))?\s*(\([单双]\))?/g;
 
     for (const segment of segments) {
-        // 清理段落中的周字和多余空格
         const cleanSegment = segment.replace(/周/g, '').trim();
-        
-        // 重置正则的 lastIndex
         segmentRegex.lastIndex = 0;
 
         let match;
-        // 循环匹配一个段落内可能存在的所有周次定义(虽然通常只有一个)
         while ((match = segmentRegex.exec(cleanSegment)) !== null) {
             const start = parseInt(match[1]);
             const end = match[2] ? parseInt(match[2]) : start;
-            const flagStr = match[3] || ''; // (单) 或 (双) 或 ""
-            
+            const flagStr = match[3] || '';
+
             let flag = 0;
             if (flagStr.includes('单')) {
                 flag = 1;
@@ -146,11 +142,8 @@ function parserWeeks(str) {
             }
 
             for (let i = start; i <= end; i++) {
-                // 过滤单双周
-                if (flag === 1 && i % 2 !== 1) continue; // 仅保留单周
-                if (flag === 2 && i % 2 !== 0) continue; // 仅保留双周
-                
-                // 防止重复添加
+                if (flag === 1 && i % 2 !== 1) continue;
+                if (flag === 2 && i % 2 !== 0) continue;
                 if (!weeks.includes(i)) {
                     weeks.push(i);
                 }
@@ -158,7 +151,6 @@ function parserWeeks(str) {
         }
     }
 
-    // 排序并返回唯一的周次列表
     return weeks.sort((a, b) => a - b);
 }
 
@@ -170,27 +162,27 @@ async function scrapeAndParseCourses() {
         const text = await response.text();
         if (!text.includes("课表查询")) {
             console.log("页面内容检查失败!");
-            await window.shiguangBridgePromise.showAlert("导入失败", "当前页面似乎不是学生课表查询页面。请检查:\n" + ts, "确定"); 
+            await window.shiguangBridgePromise.showAlert("导入失败", "当前页面似乎不是学生课表查询页面。请检查:\n" + ts, "确定");
             return null;
         }
         const typeElement = document.querySelector('#shcPDF');
         if (!typeElement) {
-             console.log("未能找到视图类型元素 (#shcPDF)");
-             await window.shiguangBridgePromise.showAlert("导入失败", "未能识别课表视图类型,请确认您已点击查询且课表已加载完毕。", "确定");
-             return null;
+            console.log("未能找到视图类型元素 (#shcPDF)");
+            await window.shiguangBridgePromise.showAlert("导入失败", "未能识别课表视图类型,请确认您已点击查询且课表已加载完毕。", "确定");
+            return null;
         }
         const type = typeElement.dataset['type'];
         const tableElement = document.querySelector(type === 'list' ? '#kblist_table' : '#kbgrid_table_0');
         if (!tableElement) {
-             console.log("未能找到课表主体 HTML");
-             await window.shiguangBridgePromise.showAlert("导入失败", `未能找到课表主体 (${type} 视图),请确认您已点击查询且课表已加载完毕。`, "确定");
-             return null;
+            console.log("未能找到课表主体 HTML");
+            await window.shiguangBridgePromise.showAlert("导入失败", `未能找到课表主体 (${type} 视图),请确认您已点击查询且课表已加载完毕。`, "确定");
+            return null;
         }
         let result = [];
         if (type === 'list') {
-            result = parserList(); 
+            result = parserList();
         } else {
-            result = parserTbale(); 
+            result = parserTbale();
         }
         if (result.length === 0) {
             window.shiguangBridge.showToast("未找到任何课程数据,请检查所选学年学期是否正确或本学期无课。");
@@ -231,14 +223,6 @@ async function runImportFlow() {
         window.shiguangBridge.showToast("用户取消了导入。");
         return;
     }
-    
-    if (typeof window.jQuery === 'undefined' && typeof $ === 'undefined') {
-        const errorMsg = "当前教务系统页面似乎没有加载 jQuery 库。本脚本依赖 jQuery 进行 DOM 解析。";
-        window.shiguangBridge.showToast(errorMsg);
-        await window.shiguangBridgePromise.showAlert("导入失败", errorMsg + "\n请尝试刷新页面或使用其他导入方式。", "确定");
-        console.error("JS: 缺少 jQuery 依赖,流程终止。");
-        return;
-    }
 
     const result = await scrapeAndParseCourses();
     if (result === null) {
@@ -252,7 +236,7 @@ async function runImportFlow() {
         console.log("JS: 课程保存失败,流程终止。");
         return;
     }
-    
+
     window.shiguangBridge.showToast(`课程导入成功,共导入 ${courses.length} 门课程!`);
     console.log("JS: 整个导入流程执行完毕并成功。");
     window.shiguangBridge.notifyTaskCompletion();