SUSTech_GPA

SUSTech GPA plugin

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

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name          SUSTech_GPA
// @author        Edwardfang
// @namespace     http://github.com/edwardfang/sustech_gpa
// @description   SUSTech GPA plugin
// @match         *://jwxt.sustc.edu.cn/jsxsd/kscj/cjcx_list
// @match         *://jwxt.sustc.edu.cn/jsxsd/framework/main.jsp
// @match         *://jwxt.sustc.edu.cn/jsxsd/framework/xsMain.jsp
// @require       https://cdn.staticfile.org/jquery/1.12.4/jquery.min.js
// @version       0.3.2
// @run-at        document-end
// ==/UserScript==



function getTableData(){
	const userful_columns = ['成绩/等级', '学分']
	const mapping = {'成绩/等级':'grade', '学分':'credit'}
	let myRows = [];
	let $headers = $("table#dataList th");
	let $rows = $("table#dataList tbody tr").each(function(index) {
	  $cells = $(this).find("td");
	  myRows[index] = {};
	  $cells.each(function(cellIndex) {
	  	if ($headers[cellIndex].children.length == 1) {
	  		myRows[index]['select'] = $(this).find("input").eq(0).prop("checked");
	  	}else{
	  		column_name = $($headers[cellIndex]).html();
	  		if (userful_columns.includes(column_name)) {
	  			myRows[index][mapping[column_name]] = $(this).html();
	  		}
	  	}
	  });    
	});
	return myRows.slice(1);
};

function updateGPA(){
	const GPA_mapping_rules = {'A+':4, 'A':3.94, 'A-':3.85,
	 'B+':3.73, 'B':3.55, 'B-':3.32,'C+':3.09, 'C':2.78,
	  'C-':2.42,'D+':2.08, 'D':1.63, 'D-':1.15, 'F':0};
	let data = getTableData();
	const reducer = (info, cur) =>{
		// console.log(info);
		// console.log(cur);
		if (cur['grade'] != 'P' && cur['select'] == true) {
			let credit = parseInt(cur['credit']);
			info['total_credits'] += credit;
			info['total_points'] += GPA_mapping_rules[cur['grade']]*credit;
		}
		return info;
	}
	let init_info = {'total_credits': 0, 'total_points':0};
	let info = data.reduce(reducer, init_info)
	let final_gpa = info['total_points']/info['total_credits'];
	final_gpa = final_gpa.toFixed(2);
	$("p#gpa").text(`Selected GPA is ${final_gpa}.`);
	// console.log(final_gpa)
	//alert("Selected GPA is 4.0!");
};


function loadInGradePage(){
	$("table#dataList").find('th').eq(2).after('<th>\
		<input id="checkAll" type="checkbox" checked/>All</th>');
	$("table#dataList").find('th').eq(3).css("width", "40px");
	$('table#dataList').find('tr').each(function(){
        $(this).find('td').eq(2).after('<td><input name="subBox" \
        	type="checkbox" checked /></td>');
    });
    $("#checkAll").click(function() {
        $('input[name="subBox"]').prop("checked",this.checked);
        updateGPA();
    });
    let $subBox = $("input[name='subBox']");
    $subBox.click(function(){
        $("#checkAll").attr("checked",$subBox.length == $("input[name='subBox']:checked").length ? true : false);
        updateGPA();
    });
    $("table#dataList").before("<p id='gpa'>Selected GPA is</p>" );
    $("p#gpa").css("font-size", '20px').css('color', 'red');
    updateGPA();
}

function loadInIndexPage(){
	$("div.block10tex").text("GPA 查询");
	$("div.block10").parent().eq(0).attr('href', '/jsxsd/kscj/cjcx_list')
}

$(function(){
	let pathname = window.location.pathname;
	if (pathname == '/jsxsd/kscj/cjcx_list') {
		loadInGradePage();
	}else if(pathname == '/jsxsd/framework/main.jsp' ||
		pathname == '/jsxsd/framework/xsMain.jsp'
		){
		loadInIndexPage();
	}
});