| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 |
- // 节次对应的时间映射表
- const TimeSlots = {
- 1: { start: "08:10", end: "08:55" },
- 2: { start: "09:05", end: "09:50" },
- 3: { start: "10:10", end: "10:55" },
- 4: { start: "11:05", end: "11:50" },
- 5: { start: "12:00", end: "12:45" },
- 6: { start: "13:05", end: "13:50" },
- 7: { start: "14:00", end: "14:45" },
- 8: { start: "14:55", end: "15:40" },
- 9: { start: "16:00", end: "16:45" },
- 10: { start: "16:55", end: "17:40" },
- 11: { start: "19:00", end: "19:45" },
- 12: { start: "19:55", end: "20:40" }
- };
- // 解析课程名称
- function parseCourseName(text) {
- if (!text) return "";
- const match = text.match(/^([^\[\]]+)/);
- return match ? match[1].trim() : text.trim();
- }
- // 解析教室信息
- function parseClassroom(text) {
- if (!text) return "";
- const parts = text.split(',');
- if (parts.length >= 3) return parts[parts.length - 1].trim();
- if (parts.length === 2) return parts[1].trim();
- return text.trim();
- }
- // 解析节次范围
- function parseSections(roomText) {
- if (!roomText) return { start: 1, end: 1 };
-
- const match = roomText.match(/(\d+)-(\d+)\s*[节]?$/);
- if (match) return { start: parseInt(match[1]), end: parseInt(match[2]) };
-
- const parts = roomText.split(',');
- for (const part of parts) {
- const sectionMatch = part.match(/(\d+)-(\d+)/);
- if (sectionMatch && !part.includes('周')) {
- return { start: parseInt(sectionMatch[1]), end: parseInt(sectionMatch[2]) };
- }
- }
-
- return { start: 1, end: 1 };
- }
- // 解析周次信息
- function parseWeeksFromRoom(roomText) {
- if (!roomText) return [];
-
- const weeks = new Set();
- const weekPatterns = roomText.match(/(\d+)(?:-(\d+))?周/g);
-
- if (weekPatterns) {
- weekPatterns.forEach(pattern => {
- const match = pattern.match(/(\d+)(?:-(\d+))?周/);
- if (match) {
- const startWeek = parseInt(match[1]);
- const endWeek = match[2] ? parseInt(match[2]) : startWeek;
- for (let week = startWeek; week <= endWeek; week++) weeks.add(week);
- }
- });
- }
-
- return Array.from(weeks).sort((a, b) => a - b);
- }
- // 提取课程数据
- function extractCourses() {
- const courses = [];
- const courseCells = document.querySelectorAll('td[data-role="item"]');
-
- courseCells.forEach(cell => {
- const courseDivs = cell.querySelectorAll('.mtt_arrange_item');
- if (courseDivs.length === 0) return;
-
- const day = parseInt(cell.getAttribute('data-week') || "1");
- const beginUnit = parseInt(cell.getAttribute('data-begin-unit') || "1");
- const endUnit = parseInt(cell.getAttribute('data-end-unit') || beginUnit);
- const startSection = Math.min(beginUnit, endUnit);
- const endSection = Math.max(beginUnit, endUnit);
-
- courseDivs.forEach(courseDiv => {
- const detailDiv = courseDiv.querySelector('.mtt_item_kcmc');
- if (!detailDiv) return;
-
- const courseNameNode = detailDiv.firstChild;
- const rawCourseName = courseNameNode?.nodeValue?.trim() || "";
- if (!rawCourseName) return;
-
- const teacherElement = detailDiv.querySelector('.mtt_item_jxbmc');
- const teacher = teacherElement?.innerText?.trim() || "";
-
- const roomElement = detailDiv.querySelector('.mtt_item_room');
- const rawRoomText = roomElement?.innerText?.trim() || "";
-
- const weeks = parseWeeksFromRoom(rawRoomText);
- if (weeks.length === 0) return;
-
- const position = parseClassroom(rawRoomText);
-
- let finalStartSection = startSection;
- let finalEndSection = endSection;
-
- if (startSection === 1 && endSection === 1) {
- const sections = parseSections(rawRoomText);
- finalStartSection = sections.start;
- finalEndSection = sections.end;
- }
-
- const course = {
- name: parseCourseName(rawCourseName),
- teacher: teacher,
- position: position,
- day: day,
- startSection: finalStartSection,
- endSection: finalEndSection,
- weeks: weeks
- };
-
- if (course.name && course.weeks.length > 0) courses.push(course);
- });
- });
-
- return courses;
- }
- // 合并重复课程
- function mergeCourses(courses) {
- const courseMap = new Map();
-
- courses.forEach(course => {
- const key = `${course.name}-${course.teacher}-${course.day}-${course.startSection}-${course.endSection}-${course.position}`;
-
- if (courseMap.has(key)) {
- const existing = courseMap.get(key);
- const mergedWeeks = [...new Set([...existing.weeks, ...course.weeks])].sort((a, b) => a - b);
- existing.weeks = mergedWeeks;
- courseMap.set(key, existing);
- } else {
- courseMap.set(key, { ...course });
- }
- });
-
- return Array.from(courseMap.values());
- }
- // 排序课程
- function sortCourses(courses) {
- return courses.sort((a, b) => {
- if (a.day !== b.day) return a.day - b.day;
- if (a.startSection !== b.startSection) return a.startSection - b.startSection;
- return a.endSection - b.endSection;
- });
- }
- // 生成时间段数据
- function generateTimeSlots() {
- return Object.entries(TimeSlots).map(([number, time]) => ({
- number: parseInt(number),
- startTime: time.start,
- endTime: time.end
- }));
- }
- // 生成配置数据
- function generateConfig(courses) {
- let maxWeek = 0;
- courses.forEach(course => {
- course.weeks.forEach(week => {
- if (week > maxWeek) maxWeek = week;
- });
- });
- return {
- semesterTotalWeeks: maxWeek > 0 ? maxWeek : 20,//不确定以后会不会改,先留着
- defaultClassDuration: 45,
- defaultBreakDuration: 10,
- firstDayOfWeek: 1
- };
- }
- //登录检测
- async function login(){
- const promptMessage =`
- 使用前请注意:
- 1、开始导入前,请确保自己已经登录并进入课表
- 2、导入过程中请保持网络连接畅通
- 3、导入完成后如学期周数等信息错误请前往设置调整
- 4、导入完成后请前往设置自行设置开学时间
- 5、本工具仅支持导入当前学期课程
- 6、学校服务器很烂,课表数据可能加载较慢,请耐心等待
- 7、如果没有进入界面请前往学校学生门户登录并在新教务系统中打开课表后尝试导入
- `;
- const wspc = "authserver.wspc.edu.cn";
- const timeTable = "jw.wspc.edu.cn";
- const currentHost = window.location.host;
- const confirmed = await window.AndroidBridgePromise.showAlert(
- "使用前请注意",
- promptMessage,
- "好的"
- );
- if (confirmed) {
- if (currentHost == wspc) {
- AndroidBridge.showToast("请先登录");
- return false;
- }
- else if (currentHost ==timeTable) {
- return true;
- }
- else
- AndroidBridge.showToast("请在学校的网站内导入");
- }
- else {
- AndroidBridge.showToast("取消导入操作");
- return false;
- }
- }
- async function saveCourses(parsedCourses) {
- try {
- await window.AndroidBridgePromise.saveImportedCourses(JSON.stringify(parsedCourses));
- AndroidBridge.showToast(`成功导入 ${parsedCourses.length} 门课程!`);
- return true;
- } catch (error) {
- AndroidBridge.showToast(`保存失败: ${error.message}`);
- return false;
- }
- }
- async function importPresetTimeSlots(timeSlots) {
- try {
- await window.AndroidBridgePromise.savePresetTimeSlots(JSON.stringify(timeSlots));
- AndroidBridge.showToast("预设时间段导入成功!");
- return true; // 添加返回值
- } catch (error) {
- AndroidBridge.showToast("导入时间段失败: " + error.message);
- return false; // 添加返回值
- }
- }
- async function saveConfig(config) {
- try {
- await window.AndroidBridgePromise.saveCourseConfig(JSON.stringify(config));
- AndroidBridge.showToast("课表配置更新成功!");
- return true;
- } catch (error) {
- AndroidBridge.showToast("保存配置失败: " + error.message);
- return false;
- }
- }
- async function runImportFlow() {
- const loginwindows = await login();
- if(!loginwindows){
- return;
- }
- // 提取课程数据
- const rawCourses = extractCourses();
- if(rawCourses.length === 0){
- AndroidBridge.showToast("未找到任何课程! 请确认已登录并进入课表页面。");
- return;
- }
- const mergedCourses = mergeCourses(rawCourses);
- if(mergedCourses.length === 0){
- AndroidBridge.showToast("课程合并失败,未生成任何课程数据!");
- return;
- }
- const finalCourses = sortCourses(mergedCourses);
- if(finalCourses.length === 0){
- AndroidBridge.showToast("课程排序失败,未生成任何课程数据!");
- return;
- }
- // 生成时间段数据
- const timeSlots = generateTimeSlots();
- if(timeSlots.length === 0){
- AndroidBridge.showToast("时间段生成失败,未生成任何时间段数据!");
- return;
- }
- // 生成配置数据
- const config = generateConfig(finalCourses);
- // 输出课程数据结构
- const saveResult = await saveCourses(finalCourses);
- if(!saveResult){
- return;
- }
- // 输出时间段数据结构
- const timeSlotResult = await importPresetTimeSlots(timeSlots);
- if(!timeSlotResult){
- return;
- }
- // 输出课表配置数据结构
- const configResult = await saveConfig(config);
- if(!configResult){
- return;
- }
- AndroidBridge.showToast("成功导入课表!请前往设置调整开学时间等信息!");
- AndroidBridge.notifyTaskCompletion();
- }
- // 执行
- runImportFlow();
|