get-table.js
· 2.6 KiB · JavaScript
Raw
// ==UserScript==
// @name 武汉理工大学获取成绩表格
// @namespace http://tampermonkey.net/
// @version 2024-09-08
// @license MIT
// @description 提取教务处中的成绩为表格并进行下载,适用地址:http://202.114.50.130/Score/login.do*
// @author LiuShen
// @match http://202.114.50.130/Score/login.do*
// @icon https://www.google.com/s2/favicons?sz=64&domain=50.130
// @grant none
// ==/UserScript==
(function() {
'use strict';
// 添加导出按钮
function addExportButton() {
const btn = document.createElement('button');
btn.textContent = '导出成绩表格';
btn.style.position = 'fixed';
btn.style.bottom = '86px';
btn.style.right = '30px';
btn.style.zIndex = '1000';
btn.style.padding = '10px';
btn.style.backgroundColor = '#007bff';
btn.style.color = '#fff';
btn.style.border = 'none';
btn.style.borderRadius = '5px';
btn.style.cursor = 'pointer';
btn.addEventListener('click', () => {
const table = document.querySelector('.grid table');
if (table) {
const csv = tableToCSV(table);
downloadCSV(csv);
} else {
alert('找不到表格');
}
});
document.body.appendChild(btn);
}
// 将表格转换为CSV格式
function tableToCSV(table) {
let tbody = document.querySelector('.grid');
if (!tbody) return;
const rows = tbody.querySelectorAll('tr');
const head = rows[0];
let cellhead = head.querySelectorAll('th div');
let headData = Array.from(cellhead).map(cellhead => cellhead.textContent.trim());
let data = [];
data.push(headData);
data.join('\n');
rows.forEach((row, index) => {
if (index === 0) return;
let cells = row.querySelectorAll('td div');
let rowData = Array.from(cells).map(cell => cell.textContent.trim());
data.push(rowData);
data.join('\n');
});
return data.join('\n');
}
// 下载CSV文件
function downloadCSV(csv) {
const csvFile = new Blob([csv], { type: 'text/csv' });
const downloadLink = document.createElement('a');
downloadLink.download = '成绩表格.csv';
downloadLink.href = window.URL.createObjectURL(csvFile);
downloadLink.style.display = 'none';
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
}
// 运行函数
addExportButton();
})();
| 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 | })(); |