BUPT GPA

Calculate GPA in URP system

当前为 2019-01-31 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name BUPT GPA
  3. // @namespace https://ssine.cc/
  4. // @version 1.9
  5. // @description Calculate GPA in URP system
  6. // @author Liu Siyao
  7. // @include *://jwxt.bupt.edu.cn/jwLoginAction.do
  8. // @include *://vpn.bupt.edu.cn/http/jwxt.bupt.edu.cn/jwLoginAction.do
  9. // @grant none
  10. // @require https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js
  11. // @require https://cdn.jsdelivr.net/npm/vue
  12. // ==/UserScript==
  13.  
  14. (function() {
  15. 'use strict';
  16. window.parent.frames[1].onload = ()=>{
  17. $.get('/gradeLnAllAction.do?type=ln&oper=qbinfo').then(
  18. (res)=>{
  19.  
  20. var algoNames = ['标准4.0', '改进4.0', '北大4.0', '加拿大4.3', '中科大4.3', '上海交大4.3'];
  21. var algoArea = [
  22. [59, 69, 79, 89, 100],
  23. [59, 69, 84, 100],
  24. [59, 63, 67, 71, 74, 77, 81, 84, 89, 100],
  25. [59, 64, 69, 74, 79, 84, 89, 100],
  26. [59, 60, 63, 64, 67, 71, 74, 77, 81, 84, 89, 94, 100],
  27. [59, 61, 64, 66, 69, 74, 79, 84, 89, 94, 100]
  28. ];
  29. var algoGp = [
  30. [0, 1, 2, 3, 4],
  31. [0, 2, 3, 4],
  32. [0, 1, 1.5, 2, 2.3, 2.7, 3, 3.3, 3.7, 4],
  33. [0, 2.3, 2.7, 3, 3.3, 3.7, 4, 4.3],
  34. [0, 1, 1.3, 1.5, 1.7, 2, 2.3, 2.7, 3, 3.3, 3.7, 4, 4.3],
  35. [0, 1, 1.7, 2, 2.3, 2.7, 3, 3.3, 3.7, 4, 4.3]
  36. ];
  37. function getGP(score, i) {
  38. var area = algoArea[i];
  39. var gp = algoGp[i];
  40. for (var idx in area) {
  41. if(score <= area[idx])
  42. return gp[idx];
  43. }
  44. return score;
  45. };
  46.  
  47. class course {
  48. constructor(no, name, semester, type, credit, grade) {
  49. this.no = no;
  50. this.name = name;
  51. this.semester = semester;
  52. this.type = type;
  53. this.credit = credit;
  54. this.grade = grade;
  55. }
  56. }
  57.  
  58. var calc_mat = [];
  59. var course_lst = [];
  60. var semesters = [];
  61. var course_types = ['必修', '选修', '任选'];
  62. var semester_name = '';
  63.  
  64. function showResult() {
  65. // console.log(course_lst);
  66. var sum = 0, total_credit = 0;
  67. var gpLst = [0, 0, 0, 0, 0, 0];
  68. for (var idx = 0; idx < course_lst.length; idx++) {
  69. var course = course_lst[idx];
  70. if (!calc_mat[semesters.indexOf(course.semester)][course_types.indexOf(course.type)])
  71. continue;
  72. total_credit += course.credit;
  73. sum += course.credit * course.grade;
  74. for (var j in gpLst) {
  75. gpLst[j] += course.credit * getGP(course.grade, j);
  76. }
  77. };
  78.  
  79.  
  80. $('#gpa-res').empty();
  81. $('#gpa-res').append($('<table>\
  82. <tr><th>算法</th><th>GPA</th></tr>\
  83. </table>'));
  84.  
  85. for (var idx in gpLst) {
  86. var newTr = "<tr><td>" + algoNames[idx] + "</td><td>" + (gpLst[idx]/total_credit).toFixed(2) + "</td></tr>";
  87. $('#gpa-res table').append($(newTr));
  88. }
  89. var contentStr = "特殊加权学分绩: " + (sum / total_credit).toFixed(2);
  90. contentStr += "<br>已修读学分: " + total_credit.toString();
  91. $('#gpa-res').append($('<p>' + contentStr + '</p>'));
  92. }
  93.  
  94. function appendResult(lst, name) {
  95. var sum = 0, total = 0;
  96. var gpLst = [0, 0, 0, 0, 0, 0];
  97.  
  98. for (var idx = 0; idx < lst.length; idx++) {
  99. var items = lst[idx].getElementsByTagName('td');
  100. if (isNaN(parseFloat(items[6].innerText)))
  101. continue;
  102. total += parseFloat(items[4].innerText);
  103. sum += parseFloat(items[4].innerText) * parseFloat(items[6].innerText);
  104. for (var j in gpLst) {
  105. gpLst[j] += parseFloat(items[4].innerText) * getGP(parseFloat(items[6].innerText), j);
  106. }
  107. };
  108.  
  109. var frame = window.parent.frames[1].document.getElementsByName('mainFrame')[0];
  110. frame = frame.contentDocument || frame.contentWindow.document;
  111. var injectEntry = $(frame).find('.hometopbg1:first');
  112. var contentStr = "特殊加权学分绩: " + (sum / total).toFixed(2);
  113. contentStr += "\\n已修读学分: " + total.toString();
  114. for (var idx in gpLst) {
  115. contentStr += "\\nGPA(" + algoNames[idx] + "): " + (gpLst[idx]/total).toFixed(2);
  116. }
  117. var newtr = $('<tr><td height="25"><a href="javascript:alert(\'' + contentStr +
  118. '\');">查看GPA(' + name + ')</a></td></tr>');
  119. injectEntry.append(newtr);
  120. }
  121.  
  122. $.get('/gradeLnAllAction.do?type=ln&oper=sxinfo&lnsxdm=001').then( (res)=>{
  123. var doc = new DOMParser().parseFromString(res, "text/html");
  124. appendResult(doc.querySelectorAll('a[name=qb_001] .odd'), "必修");
  125. appendResult(doc.querySelectorAll('a[name=qb_001] .odd, a[name=qb_02] .odd'), "必修+选修");
  126. appendResult(doc.querySelectorAll('.odd'), "必修+选修+任选");
  127. });
  128.  
  129. var parser = new DOMParser();
  130. var doc = parser.parseFromString(res, "text/html");
  131.  
  132. var body_lst = doc.getElementsByTagName('body')[0].childNodes;
  133.  
  134.  
  135.  
  136. for(var i = 0; i < body_lst.length; i++) {
  137. if (body_lst[i].tagName == 'A') {
  138. semester_name = body_lst[i].name;
  139. if (semesters.indexOf(semester_name) == -1) {
  140. semesters.push(semester_name);
  141. }
  142. } else if (body_lst[i].className == 'titleTop2') {
  143. var entry = $(body_lst[i]).find('.odd');
  144. for (var j = 0; j < entry.length; j++) {
  145. var lst = entry[j].getElementsByTagName('td');
  146. var grade = parseFloat(entry[j].getElementsByTagName('p')[0].innerText);
  147. if (isNaN(grade)) continue;
  148. course_lst.push(new course(
  149. lst[0].innerText.trim(),
  150. lst[2].innerText.trim(),
  151. semester_name,
  152. lst[5].innerText.trim(),
  153. parseFloat(lst[4].innerText),
  154. grade
  155. ));
  156. }
  157. }
  158. }
  159.  
  160. for (var i = 0; i < semesters.length; i++)
  161. calc_mat.push([true, true, false]);
  162.  
  163.  
  164. var gpa_div = $('<div id="gpa">\
  165. <div id="gpa-side">\
  166. <div id="gpa-modify">\
  167. <h2>课程属性:</h2>\
  168. <table>\
  169. <tr>\
  170. <th>课程名</th>\
  171. <th>类型</th>\
  172. <th>成绩</th>\
  173. <th>学分</th>\
  174. </tr>\
  175. <tr v-for="c in courses">\
  176. <td>{{c.name}}</td>\
  177. <td>\
  178. <select v-model="c.type">\
  179. <option>必修</option>\
  180. <option>选修</option>\
  181. <option>任选</option>\
  182. </select>\
  183. </td>\
  184. <td>{{c.grade}}</td>\
  185. <td>{{c.credit}}</td>\
  186. </tr>\
  187. </table></div>\
  188. </div>\
  189. \
  190. \
  191. <div id="gpa-main-frame">\
  192. <div id="calc-app">\
  193. <h2>要计算的课程:</h2>\
  194. <table>\
  195. <tr>\
  196. <th>学期</th>\
  197. <th>必修</th>\
  198. <th>选修</th>\
  199. <th>任选</th>\
  200. </tr>\
  201. <tr v-for="(r, idx) in mat">\
  202. <td>{{ semesters[idx] }}</td>\
  203. <td><input type="checkbox" id="checkbox" v-model="r[0]"></td>\
  204. <td><input type="checkbox" id="checkbox" v-model="r[1]"></td>\
  205. <td><input type="checkbox" id="checkbox" v-model="r[2]"></td>\
  206. </tr>\
  207. </table></div>\
  208. \
  209. \
  210. <h2>结果:</h2>\
  211. <div id="gpa-res">\
  212. </div>\
  213. <hr>\
  214. <p>程序完全基于前端,不会存储个人信息。</p>\
  215. <p>觉得好用来<a target="_blank" href="https://github.com/ssine/BUPT-GPA">仓库</a>点个star好不好ヽ(✿゚▽゚)ノ</p>\
  216. <p>欢迎把<a target="_blank" href="https://greasyfork.org/zh-CN/scripts/369550-bupt-gpa">这个脚本</a>分享给你的朋友哦(*/ω\*)</p>\
  217. </div>\
  218. </div>');
  219.  
  220. var sheet_css = $('<style>\
  221. #gpa {\
  222. position: absolute;\
  223. right: 70px;\
  224. bottom: 20px;\
  225. height: 80%;\
  226. background-color: rgba(255,255,255,0.9);\
  227. }\
  228. #gpa-side {\
  229. float: left;\
  230. margin-right: 20px;\
  231. height: 100%;\
  232. overflow: auto;\
  233. }\
  234. #gpa-main-frame {\
  235. float: left;\
  236. height: 100%;\
  237. overflow: auto;\
  238. }\
  239. #gpa-modify table tr td:first-child, #gpa-modify table tr th:first-child {\
  240. width: 200px;\
  241. }\
  242. #calc-app {\
  243. }\
  244. #res-app {\
  245. margin-top: 50px;\
  246. }\
  247. #gpa-btn {\
  248. position: absolute;\
  249. right: 20px;\
  250. bottom: 20px;\
  251. background-color: RGB(119,119,119);\
  252. color: rgb(255,255,255);\
  253. height: 50px;\
  254. width: 50px;\
  255. border-radius: 50px;\
  256. font-family: sans-serif;\
  257. }\
  258. </style>');
  259. $('head').append(sheet_css);
  260.  
  261. gpa_div.hide();
  262. $('html').append(gpa_div);
  263.  
  264. var btn = $('<button id="gpa-btn">GPA</button>');
  265. btn.click(() => {
  266. var app = $('#gpa');
  267. if (app.css('display') == 'none')
  268. app.css('display', '');
  269. else
  270. app.css('display', 'none');
  271. });
  272. $('html').append(btn);
  273.  
  274. showResult();
  275.  
  276. var gpa_modify = new Vue({
  277. el: '#gpa-modify',
  278. data: {
  279. courses: course_lst
  280. },
  281. watch: {
  282. courses: {
  283. handler(newValue, oldValue) {
  284. showResult();
  285. },
  286. deep: true
  287. }
  288. }
  289. });
  290. var calc_app = new Vue({
  291. el: '#calc-app',
  292. data: {
  293. mat: calc_mat,
  294. semesters: semesters
  295. },
  296. watch: {
  297. mat: showResult
  298. }
  299. });
  300.  
  301. }
  302. )
  303. }
  304. })();