// ==UserScript== // @name 正方教务学业预警成绩分类 // @namespace https://bbs.tampermonkey.net.cn/ // @version 0.1.3 // @description 提取页面中的课程数据并按实际修读学年整理显示-小易同学 // @author doubao // @match *://portal.nxist.com/* // @grant none // ==/UserScript== (function() { 'use strict'; // 创建提取按钮 function createExtractorButton() { const button = document.createElement('button'); button.textContent = '提取课程数据'; button.style.cssText = ` position: fixed; top: 20px; left: 20px; z-index: 9999; padding: 10px 15px; background-color: #4CAF50; color: white; border: none; border-radius: 4px; cursor: pointer; font-size: 14px; box-shadow: 0 2px 5px rgba(0,0,0,0.2); `; button.addEventListener('click', extractAndDisplayData); document.body.appendChild(button); console.log('[课程数据提取器] 按钮已加载'); } // 提取课程数据 function extractCourseData() { try { const courseData = []; const rows = document.querySelectorAll('tr'); if (rows.length === 0) { console.log('[课程数据提取器] 未找到表格行'); return courseData; } console.log(`[课程数据提取器] 找到 ${rows.length} 个表格行`); rows.forEach((row, index) => { const cells = row.querySelectorAll('td'); if (cells.length >= 8) { const course = { code: cells[0].textContent.trim(), name: cells[1].textContent.trim(), credit: cells[2].textContent.trim(), semester: cells[3].textContent.trim(), // 使用第5列数据作为实际修读学年 year: cells[4].textContent.trim(), type: cells[5].textContent.trim(), score: cells[6].textContent.trim(), status: cells[7]?.querySelector('i')?.title || '-' }; courseData.push(course); // 只打印前5条记录,避免控制台信息过多 if (index < 5) { console.log(`[课程数据提取器] 提取课程: ${course.name} (${course.year})`); } } }); console.log(`[课程数据提取器] 共提取 ${courseData.length} 条课程数据`); return courseData; } catch (error) { console.error('[课程数据提取器] 提取数据时出错:', error); alert('提取数据时发生错误: ' + error.message); return []; } } // 按实际修读学年分组数据 function groupByYear(data) { const grouped = {}; data.forEach(course => { if (!grouped[course.year]) { grouped[course.year] = []; } grouped[course.year].push(course); }); return grouped; } // 创建弹窗显示数据 function showDataInModal(data) { try { // 检查是否有数据 const years = Object.keys(data); if (years.length === 0) { alert('未找到课程数据!'); return; } const modal = document.createElement('div'); modal.style.cssText = ` position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0,0,0,0.5); display: flex; justify-content: center; align-items: center; z-index: 10000; `; const content = document.createElement('div'); content.style.cssText = ` background-color: white; width: 90%; max-width: 1200px; max-height: 90vh; overflow-y: auto; border-radius: 8px; box-shadow: 0 4px 15px rgba(0,0,0,0.3); `; // 添加关闭按钮 const closeBtn = document.createElement('button'); closeBtn.textContent = '关闭'; closeBtn.style.cssText = ` float: right; margin: 10px; padding: 8px 15px; background-color: #f44336; color: white; border: none; border-radius: 4px; cursor: pointer; `; closeBtn.addEventListener('click', () => document.body.removeChild(modal)); content.appendChild(closeBtn); // 添加标题 const title = document.createElement('h2'); title.textContent = '课程数据按实际修读学年分类'; title.style.cssText = ` text-align: center; padding: 15px; margin: 0; border-bottom: 1px solid #ddd; `; content.appendChild(title); // 为每个学年创建表格 years.forEach(year => { const courses = data[year]; const yearHeader = document.createElement('h3'); yearHeader.textContent = year; yearHeader.style.cssText = 'padding: 10px; background-color: #f0f0f0; margin: 0;'; content.appendChild(yearHeader); const table = document.createElement('table'); table.style.cssText = ` width: 100%; border-collapse: collapse; margin-bottom: 20px; `; const thead = document.createElement('thead'); thead.innerHTML = ` 课程代码 课程名称 学分 课程类型 成绩 状态 `; table.appendChild(thead); const tbody = document.createElement('tbody'); courses.forEach(course => { const tr = document.createElement('tr'); tr.innerHTML = ` ${course.code} ${course.name} ${course.credit} ${course.type} ${course.score} ${course.status} `; tbody.appendChild(tr); }); table.appendChild(tbody); content.appendChild(table); }); modal.appendChild(content); document.body.appendChild(modal); } catch (error) { console.error('[课程数据提取器] 创建弹窗时出错:', error); alert('显示数据时发生错误: ' + error.message); } } // 提取并显示数据 function extractAndDisplayData() { console.log('[课程数据提取器] 开始提取数据...'); try { const courseData = extractCourseData(); const groupedData = groupByYear(courseData); showDataInModal(groupedData); } catch (error) { console.error('[课程数据提取器] 处理数据时出错:', error); alert('处理数据时发生错误: ' + error.message); } } // 页面加载完成后创建按钮 window.addEventListener('load', createExtractorButton); })();