-
Notifications
You must be signed in to change notification settings - Fork 1
/
gpa_calc.js
79 lines (66 loc) · 2.84 KB
/
gpa_calc.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// ==UserScript==
// @name Real Time GPA Calculator UCAS
// @namespace https://github.com/TimeSea05/gpa-calc-ucas
// @version 0.2
// @description calculate your gpa in real time
// @author Avalanche
// @match https://xkcts.ucas.ac.cn/score/bks/*
// @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant none
// ==/UserScript==
(function() {
'use strict';
const userInfoTable = document.querySelectorAll('table')[0];
const scoreTable = document.querySelectorAll('table')[1];
let totalCredits = 0, totalScore = 0, totalGradePoints = 0;
let gradePointAverage = 0, scoreAverage = 0;
function getGradePoint(score) {
// 分数和GPA的对应表
const scoreGPAObj = {
90: 4.0, 89: 3.9, 88: 3.9, 87: 3.9, 86: 3.8,
85: 3.8, 84: 3.7, 83: 3.7, 82: 3.6, 81: 3.5,
80: 3.5, 79: 3.4, 78: 3.4, 77: 3.3, 76: 3.3,
75: 3.2, 74: 3.1, 73: 3.0, 72: 2.9, 71: 2.8,
70: 2.7, 69: 2.7, 68: 2.6, 67: 2.5, 66: 2.4,
65: 2.3, 64: 2.3, 63: 2.2, 62: 2.1, 61: 1.8,
60: 1.6
};
if (score >= 90) return scoreGPAObj[90];
else if (score >= 60 && score < 90) return scoreGPAObj[score];
else return 0;
}
for (let i = 1; i < scoreTable.rows.length; i++) {
// 第一行是表头, 跳过
// 在每一行中, 表格的第四列和第五列分别是学分和成绩
const credit = Number(scoreTable.rows[i].cells[3].innerHTML);
const scoreString = scoreTable.rows[i].cells[4].innerHTML;
const score = Number(scoreString);
// 如果分数一栏是"合格", "不合格"等非数字字样,那么跳过
// 课程未评估,分数一栏显示未空,同样也跳过
if (scoreString === '' || Number.isNaN(score)) continue;
totalCredits += credit;
totalScore += credit * score;
totalGradePoints += credit * getGradePoint(score);
}
if (totalScore === 0) {
const courseResult = document.querySelectorAll('h5')[document.querySelectorAll('h5').length - 1];
courseResult.innerHTML += '(脚本未生效)';
return;
}
gradePointAverage = (totalGradePoints / totalCredits).toFixed(3);
scoreAverage = (totalScore / totalCredits).toFixed(2);
const currentURL = document.URL;
let gpaProp = null;
if (currentURL.includes('all')) gpaProp = '所有学期';
else gpaProp = '当前学期';
// 更改个人信息栏中的GPA等信息
userInfoTable.rows[2].cells[0].innerHTML = gpaProp + `GPA(实时): ${gradePointAverage}`;
userInfoTable.rows[2].cells[1].innerHTML += '(非实时)';
// 增加平均分信息
const tr = document.createElement('tr');
const gradePointAverageTd = document.createElement('td');
gradePointAverageTd.innerHTML = `加权平均分(实时): ${scoreAverage}`;
tr.append(gradePointAverageTd);
tr.append(document.createElement('td')); // 添加空栏
userInfoTable.append(tr);
})();