forked from vaarun5/PHUHSGradeChecker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
117 lines (108 loc) · 3.48 KB
/
popup.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
gradeObject = [];
var future = 1;
var tFuture = [];
function createFutureGrade(){
tFuture[future] = document.createElement("div");
let x = document.createElement("input");
x.setAttribute("value", "hello");
test = document.createTextNode(
`Theoretical Assignment ${future}\n`
);
inputs = document.createElement("input");
inputs.setAttribute("value", "0 / 0");
inputs.id = "gradeInput-" + i;
tFuture[future].appendChild(test);
tFuture[future].appendChild(inputs);
tFuture[future].appendChild(document.createElement("br"));
document.getElementById("assignmentSection").appendChild(tFuture[future]);
future++;
}
function removeFuture(){
document.getElementById("assignmentSection").removeChild(tFuture[tFuture.length-1]);
tFuture.pop();
future--;
}
function createGradeEntries(itemsArray) {
let test = [];
let x = document.createElement("input");
x.setAttribute("value", "hello");
for (i = 0; i < itemsArray.length; i++) {
test[i] = document.createTextNode(
`Assignment: ${itemsArray[i].name} (${itemsArray[i].type})\n`
);
}
return test;
}
function fetchGrade() {
chrome.tabs.executeScript(
{
file: "/fetchGrades.js",
},
(results) => {
let objArray = results[0];
console.log(objArray);
const newDiv = document.createElement("div");
const newContent = createGradeEntries(objArray);
let inputs = [];
for (i = 0; i < objArray.length; i++) {
inputs[i] = newDiv.appendChild(document.createElement("input"));
}
for (i = 0; i < objArray.length; i++) {
inputs[i].setAttribute("value", objArray[i].grades);
inputs[i].id = "gradeInput-" + i;
}
for (i = 0; i < objArray.length; i++) {
newDiv.appendChild(newContent[i]);
newDiv.appendChild(inputs[i]);
newDiv.appendChild(document.createElement("br"));
}
document.getElementById("assignmentSection").innerHTML = "";
document.getElementById("assignmentSection").appendChild(newDiv);
}
);
future = 1;
var tFuture = [];
}
function testWeighting() {
chrome.tabs.executeScript(
{
file: '/fetchWeighting.js'
}, (results) => {z = results[0]; alert(z[0].category + z[0].weight + '/' + z[1].category + z[1].weight); console.log(results)}
)
}
function calculateAverage() {
len = document.querySelectorAll("input");
gradeInputs = [];
let x = [];
for (i = 0; i < len.length; i++) {
x[i] = document.getElementById(`gradeInput-${i}`);
gradeInputs[i] = x[i].value;
}
let numerator = 0;
let denominator = 0;
let split = [];
let earnedPoints = [];
let possiblePoints = [];
for (i=0; i<gradeInputs.length; i++) {
fraction = gradeInputs[i];
split[i] = fraction.split('/');
if (split[i][0] != "NG "){
earnedPoints[i] = parseFloat(split[i][0]);
possiblePoints[i] = parseFloat(split[i][1]);
}
else{
earnedPoints[i] = 0;
possiblePoints[i] = 0;
}
}
for (i = 0; i < earnedPoints.length; i++) {
numerator = numerator + earnedPoints[i];
denominator = denominator + possiblePoints[i];
}
alert(100 * (numerator/denominator) + "%");
}
document.getElementById("clickme").addEventListener("click", fetchGrade);
document.getElementById("average").addEventListener("click", calculateAverage);
document.getElementById("weighting").addEventListener("click", testWeighting);
document.getElementById("future").addEventListener("click", createFutureGrade);
document.getElementById("remove").addEventListener("click", removeFuture);