LiuShen revised this gist 1 year ago. Go to revision
1 file changed, 79 insertions
get-table.js(file created)
| @@ -0,0 +1,79 @@ | |||
| 1 | + | // ==UserScript== | |
| 2 | + | // @name 武汉理工大学获取成绩表格 | |
| 3 | + | // @namespace http://tampermonkey.net/ | |
| 4 | + | // @version 2024-09-08 | |
| 5 | + | // @license MIT | |
| 6 | + | // @description 提取教务处中的成绩为表格并进行下载,适用地址:http://202.114.50.130/Score/login.do* | |
| 7 | + | // @author LiuShen | |
| 8 | + | // @match http://202.114.50.130/Score/login.do* | |
| 9 | + | // @icon https://www.google.com/s2/favicons?sz=64&domain=50.130 | |
| 10 | + | // @grant none | |
| 11 | + | // ==/UserScript== | |
| 12 | + | ||
| 13 | + | (function() { | |
| 14 | + | 'use strict'; | |
| 15 | + | ||
| 16 | + | // 添加导出按钮 | |
| 17 | + | function addExportButton() { | |
| 18 | + | const btn = document.createElement('button'); | |
| 19 | + | btn.textContent = '导出成绩表格'; | |
| 20 | + | btn.style.position = 'fixed'; | |
| 21 | + | btn.style.bottom = '86px'; | |
| 22 | + | btn.style.right = '30px'; | |
| 23 | + | btn.style.zIndex = '1000'; | |
| 24 | + | btn.style.padding = '10px'; | |
| 25 | + | btn.style.backgroundColor = '#007bff'; | |
| 26 | + | btn.style.color = '#fff'; | |
| 27 | + | btn.style.border = 'none'; | |
| 28 | + | btn.style.borderRadius = '5px'; | |
| 29 | + | btn.style.cursor = 'pointer'; | |
| 30 | + | ||
| 31 | + | btn.addEventListener('click', () => { | |
| 32 | + | const table = document.querySelector('.grid table'); | |
| 33 | + | if (table) { | |
| 34 | + | const csv = tableToCSV(table); | |
| 35 | + | downloadCSV(csv); | |
| 36 | + | } else { | |
| 37 | + | alert('找不到表格'); | |
| 38 | + | } | |
| 39 | + | }); | |
| 40 | + | ||
| 41 | + | document.body.appendChild(btn); | |
| 42 | + | } | |
| 43 | + | ||
| 44 | + | // 将表格转换为CSV格式 | |
| 45 | + | function tableToCSV(table) { | |
| 46 | + | let tbody = document.querySelector('.grid'); | |
| 47 | + | if (!tbody) return; | |
| 48 | + | const rows = tbody.querySelectorAll('tr'); | |
| 49 | + | const head = rows[0]; | |
| 50 | + | let cellhead = head.querySelectorAll('th div'); | |
| 51 | + | let headData = Array.from(cellhead).map(cellhead => cellhead.textContent.trim()); | |
| 52 | + | let data = []; | |
| 53 | + | data.push(headData); | |
| 54 | + | data.join('\n'); | |
| 55 | + | rows.forEach((row, index) => { | |
| 56 | + | if (index === 0) return; | |
| 57 | + | let cells = row.querySelectorAll('td div'); | |
| 58 | + | let rowData = Array.from(cells).map(cell => cell.textContent.trim()); | |
| 59 | + | data.push(rowData); | |
| 60 | + | data.join('\n'); | |
| 61 | + | }); | |
| 62 | + | return data.join('\n'); | |
| 63 | + | } | |
| 64 | + | ||
| 65 | + | // 下载CSV文件 | |
| 66 | + | function downloadCSV(csv) { | |
| 67 | + | const csvFile = new Blob([csv], { type: 'text/csv' }); | |
| 68 | + | const downloadLink = document.createElement('a'); | |
| 69 | + | downloadLink.download = '成绩表格.csv'; | |
| 70 | + | downloadLink.href = window.URL.createObjectURL(csvFile); | |
| 71 | + | downloadLink.style.display = 'none'; | |
| 72 | + | document.body.appendChild(downloadLink); | |
| 73 | + | downloadLink.click(); | |
| 74 | + | document.body.removeChild(downloadLink); | |
| 75 | + | } | |
| 76 | + | ||
| 77 | + | // 运行函数 | |
| 78 | + | addExportButton(); | |
| 79 | + | })(); | |
Newer
Older