SUSTech_GPA

SUSTech GPA plugin

当前为 2018-10-14 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name SUSTech_GPA
  3. // @author Edwardfang
  4. // @namespace http://github.com/edwardfang/sustech_gpa
  5. // @description SUSTech GPA plugin
  6. // @match *://jwxt.sustc.edu.cn/jsxsd/kscj/cjcx_list
  7. // @match *://jwxt.sustc.edu.cn/jsxsd/framework/main.jsp
  8. // @match *://jwxt.sustc.edu.cn/jsxsd/framework/xsMain.jsp
  9. // @require https://cdn.staticfile.org/jquery/1.12.4/jquery.min.js
  10. // @version 0.3.2
  11. // @run-at document-end
  12. // ==/UserScript==
  13.  
  14.  
  15.  
  16. function getTableData(){
  17. const userful_columns = ['成绩/等级', '学分']
  18. const mapping = {'成绩/等级':'grade', '学分':'credit'}
  19. let myRows = [];
  20. let $headers = $("table#dataList th");
  21. let $rows = $("table#dataList tbody tr").each(function(index) {
  22. $cells = $(this).find("td");
  23. myRows[index] = {};
  24. $cells.each(function(cellIndex) {
  25. if ($headers[cellIndex].children.length == 1) {
  26. myRows[index]['select'] = $(this).find("input").eq(0).prop("checked");
  27. }else{
  28. column_name = $($headers[cellIndex]).html();
  29. if (userful_columns.includes(column_name)) {
  30. myRows[index][mapping[column_name]] = $(this).html();
  31. }
  32. }
  33. });
  34. });
  35. return myRows.slice(1);
  36. };
  37.  
  38. function updateGPA(){
  39. const GPA_mapping_rules = {'A+':4, 'A':3.94, 'A-':3.85,
  40. 'B+':3.73, 'B':3.55, 'B-':3.32,'C+':3.09, 'C':2.78,
  41. 'C-':2.42,'D+':2.08, 'D':1.63, 'D-':1.15, 'F':0};
  42. let data = getTableData();
  43. const reducer = (info, cur) =>{
  44. // console.log(info);
  45. // console.log(cur);
  46. if (cur['grade'] != 'P' && cur['select'] == true) {
  47. let credit = parseInt(cur['credit']);
  48. info['total_credits'] += credit;
  49. info['total_points'] += GPA_mapping_rules[cur['grade']]*credit;
  50. }
  51. return info;
  52. }
  53. let init_info = {'total_credits': 0, 'total_points':0};
  54. let info = data.reduce(reducer, init_info)
  55. let final_gpa = info['total_points']/info['total_credits'];
  56. final_gpa = final_gpa.toFixed(2);
  57. $("p#gpa").text(`Selected GPA is ${final_gpa}.`);
  58. // console.log(final_gpa)
  59. //alert("Selected GPA is 4.0!");
  60. };
  61.  
  62.  
  63. function loadInGradePage(){
  64. $("table#dataList").find('th').eq(2).after('<th>\
  65. <input id="checkAll" type="checkbox" checked/>All</th>');
  66. $("table#dataList").find('th').eq(3).css("width", "40px");
  67. $('table#dataList').find('tr').each(function(){
  68. $(this).find('td').eq(2).after('<td><input name="subBox" \
  69. type="checkbox" checked /></td>');
  70. });
  71. $("#checkAll").click(function() {
  72. $('input[name="subBox"]').prop("checked",this.checked);
  73. updateGPA();
  74. });
  75. let $subBox = $("input[name='subBox']");
  76. $subBox.click(function(){
  77. $("#checkAll").attr("checked",$subBox.length == $("input[name='subBox']:checked").length ? true : false);
  78. updateGPA();
  79. });
  80. $("table#dataList").before("<p id='gpa'>Selected GPA is</p>" );
  81. $("p#gpa").css("font-size", '20px').css('color', 'red');
  82. updateGPA();
  83. }
  84.  
  85. function loadInIndexPage(){
  86. $("div.block10tex").text("GPA 查询");
  87. $("div.block10").parent().eq(0).attr('href', '/jsxsd/kscj/cjcx_list')
  88. }
  89.  
  90. $(function(){
  91. let pathname = window.location.pathname;
  92. if (pathname == '/jsxsd/kscj/cjcx_list') {
  93. loadInGradePage();
  94. }else if(pathname == '/jsxsd/framework/main.jsp' ||
  95. pathname == '/jsxsd/framework/xsMain.jsp'
  96. ){
  97. loadInIndexPage();
  98. }
  99. });