Skip to content

Instantly share code, notes, and snippets.

@crabtw
Created January 23, 2009 05:42
Show Gist options
  • Save crabtw/50906 to your computer and use it in GitHub Desktop.
Save crabtw/50906 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name calc-grade
// @namespace [email protected]
// @description calculate average grades
// @include https://portal.yzu.edu.tw/Personal/Student/student_learn2.asp
// ==/UserScript==
var table = document.getElementsByClassName('table_1')[0].firstChild;
var iter = document.evaluate(
'tr[position() > 4 and position() != last()]',
table,
null,
XPathResult.UNORDERED_NODE_ITERATOR_TYPE,
null
);
var tr = iter.iterateNext();
var totalCredits = 0;
var totalGrades = 0;
var totalPoints = 0;
var missedCredits = 0;
while(tr) {
var td = tr.cells;
var grade = parseInt(td[6].innerHTML);
var credit = parseInt(td[5].innerHTML);
if(grade >= 60 && grade <= 100) {
totalCredits += credit
totalGrades += grade * credit;
} else {
missedCredits += credit;
}
var point = Math.floor(grade / 10) - 4;
if(point > 4) point = 4;
totalPoints += credit * point;
tr = iter.iterateNext();
}
var lastRow = document.createElement('tr');
var creditCell = document.createElement('td');
var gradeCell = document.createElement('td');
var averageCell = document.createElement('td');
var pointCell = document.createElement('td');
creditCell.innerHTML = totalCredits;
creditCell.align = 'center';
gradeCell.innerHTML = totalGrades;
gradeCell.align = 'center';
pointCell.innerHTML = totalPoints / (totalCredits + missedCredits);
pointCell.align = 'center';
averageCell.innerHTML = totalGrades / totalCredits;
averageCell.colSpan = 4;
averageCell.align = 'center';
lastRow.appendChild(averageCell);
lastRow.appendChild(pointCell);
lastRow.appendChild(creditCell);
lastRow.appendChild(gradeCell);
table.insertBefore(lastRow, table.lastChild);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment