-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontent.js
363 lines (304 loc) · 16.1 KB
/
content.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
function main() {
// check if lms is logged in
if (window.location.href.includes("web?#id="))
return;
// check if the current page is the LMS DMC page
if (!window.location.href.includes("view_type=form&model=obe.grade.book&menu_id=572")) {
return;
}
// Get the refresh count from sessionStorage (default to 0)
let refreshCount = sessionStorage.getItem("refreshCount") || 0;
// If refresh count is 0, reload and increment count
if (refreshCount < 1) {
sessionStorage.setItem("refreshCount", "1"); // Mark as reloaded
window.location.reload(); // Reload only once
}
try {
// Select the table parent container
var select = '/html/body/div[2]/table/tbody/tr/td[2]/div/div/div/div/div/div[2]/div/div[4]/div/div/div/div/table[5]/tbody/tr/td/div/div';
const element = document.evaluate(select, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
// If the element is not loaded, wait for 2 seconds and try again
if (!element) {
setTimeout(main, 2000);
return;
}
// Select the table
var selector = `#${element.id} > div > div > table`
var table = document.querySelector(selector);
// If the table is not loaded, wait for 2 seconds and try again
if (!table) {
setTimeout(main, 2000);
return;
}
// If the table has rows
if (table) {
// If the table has only 3 rows(empty rows), wait for 2 seconds and try again
if (table.rows.length == 3) {
setTimeout(main, 2000);
return;
}
// If the table has more than 3 rows
var rows = table.rows;
// get the first semester name and update the value for current semester
var semester1 = rows[2].cells[0].textContent;
var obj = [
{
"semester": semester1,
"courses": []
}
]
// Iterate through the rows and get the data of all the semesters
var count = 0;
for (var i = 2; i < rows.length - 1; i++) {
// get the current semester name
var semester = rows[i].cells[0].textContent;
// If the current semester is the same as the previous semester
if (semester === semester1) {
var course = rows[i].cells[1].textContent;
var courseCode = course.split(" ")[0];
var courseTitle = course.split(" ").slice(1).join(" ");
var instructor = rows[i].cells[2].textContent;
var creditHour = rows[i].cells[3].textContent;
var gradePoint = rows[i].cells[4].textContent;
var grade = rows[i].cells[5].textContent;
var status = rows[i].cells[6].textContent;
var courseObj = {
"courseCode": courseCode,
"courseTitle": courseTitle,
"instructor": instructor,
"creditHour": creditHour,
"gradePoint": gradePoint,
"grade": grade,
"status": status
}
obj[count].courses.push(courseObj);
}
// If the current semester is different from the previous semester
if (semester !== semester1) {
count++;
semester1 = semester;
var course = rows[i].cells[1].textContent;
var courseCode = course.split(" ")[0];
var courseTitle = course.split(" ").slice(1).join(" ");
var instructor = rows[i].cells[2].textContent;
var creditHour = rows[i].cells[3].textContent;
var gradePoint = rows[i].cells[4].textContent;
var grade = rows[i].cells[5].textContent;
var status = rows[i].cells[6].textContent;
var courseObj = {
"courseCode": courseCode,
"courseTitle": courseTitle,
"instructor": instructor,
"creditHour": creditHour,
"gradePoint": gradePoint,
"grade": grade,
"status": status
}
obj.push({
"semester": semester1,
"courses": [courseObj]
});
}
}
// If the grade is "WF" or "F", set the credit hour to 0, only if the course is passed in any subsequent semester
let semestersData = obj;
// Iterate through each semester
for (let i = 0; i < semestersData.length; i++) {
let currentSemesterCourses = semestersData[i].courses;
// Iterate through each course in the current semester
for (let j = 0; j < currentSemesterCourses.length; j++) {
let currentCourse = currentSemesterCourses[j];
// Check if the grade is "WF" or "F"
if (currentCourse.grade === "WF" || currentCourse.grade === "F") {
let currentCourseCode = currentCourse.courseCode;
let currentCourseTitle = currentCourse.courseTitle;
// Check subsequent semesters
for (let k = i + 1; k < semestersData.length; k++) {
let nextSemesterCourses = semestersData[k].courses;
// Check if the course is passed in any subsequent semester
for (let l = 0; l < nextSemesterCourses.length; l++) {
let nextCourse = nextSemesterCourses[l];
if (nextCourse.courseCode === currentCourseCode &&
nextCourse.courseTitle === currentCourseTitle &&
(nextCourse.grade !== "WF" && nextCourse.grade !== "F")) {
// Set credit hour to 0 in the previous semester
currentCourse.creditHour = "0.00";
break; // Move to the next course in the current semester
}
}
}
}
}
}
var count = 1;
var previous = 0;
// Add the GPA and CGPA to the table
semestersData.forEach((object) => {
// row for the GPA
var row = document.createElement("tr");
row.style.backgroundColor = "#ffffff";
row.style.fontWeight = "bold";
// empty cell
var cell = document.createElement("td");
row.appendChild(cell);
// empty cell
var cell2 = document.createElement("td");
row.appendChild(cell2);
// empty cell
var cell3 = document.createElement("td");
row.appendChild(cell3);
// calculate the Credit Hours of the current semester and add it to the table
var cell4 = document.createElement("td");
// Exclude the courses with grade "I" and "IP" from the calculation
var ch = object.courses.reduce((a, b) => b.grade !== "I" && b.grade !== "IP" ? a + parseFloat(b.creditHour) : a, 0);
cell4.textContent = ch.toFixed(1);
row.appendChild(cell4);
// calculate the Grade Points of the current semester and add it to the table
var cell5 = document.createElement("td");
// Exclude the courses with grade "I" and "IP" from the calculation
var gp = object.courses.reduce((a, b) => b.grade !== "I" && b.grade !== "IP" ? a + parseFloat(b.gradePoint) : a, 0);
cell5.textContent = gp.toFixed(2);
row.appendChild(cell5);
// Add the GPA row to the table of the current semester
var cell6 = document.createElement("td");
cell6.textContent = "GPA: " + (gp / ch).toFixed(3);
cell6.style.width = "80px";
row.appendChild(cell6);
// if the current semester is not the first semester, show the CGPA
if (count > 1) {
var cell7 = document.createElement("td");
// calculate the Credit Points till the current semester excluding the courses with grade "I" and "IP"
var totalcpa = semestersData.slice(0, count).reduce((a, b) => a + parseFloat(b.courses.reduce((a, b) => b.grade !== "I" && b.grade !== "IP" ? a + parseFloat(b.gradePoint) : a, 0)), 0);
// calculate the Credit Hours till the current semester excluding the courses with grade "I" and "IP"
var totalch = semestersData.slice(0, count).reduce((a, b) => a + parseFloat(b.courses.reduce((a, b) => b.grade !== "I" && b.grade !== "IP" ? a + parseFloat(b.creditHour) : a, 0)), 0);
// Add the CGPA to the table of the current semester
cell7.textContent = "CGPA: " + (totalcpa / totalch).toFixed(3);
cell7.style.width = "90px";
row.appendChild(cell7);
count++;
}
// if the current semester is first semester, don't show the CGPA
else {
count++;
}
table.rows[object.courses.length + 2 + previous].parentNode.insertBefore(row, table.rows[object.courses.length + 2 + previous]);
previous += object.courses.length + 1;
});
// row for the CGPA
var row = table.insertRow();
row.style.backgroundColor = "#ffffff";
row.style.fontWeight = "bold";
// calculate the CGPA excluding the courses with grade "I" and "IP"
var gp = semestersData.reduce((a, b) => a + parseFloat(b.courses.reduce((a, b) => b.grade !== "I" && b.grade !== "IP" ? a + parseFloat(b.gradePoint) : a, 0)), 0);
var ch = semestersData.reduce((a, b) => a + parseFloat(b.courses.reduce((a, b) => b.grade !== "I" && b.grade !== "IP" ? a + parseFloat(b.creditHour) : a, 0)), 0);
var cgpa = gp / ch;
// Add the CGPA to the table
row.insertCell(); // empty cell
row.insertCell(); // empty cell
var cell = row.insertCell(); // cgpa cell
cell.textContent = `CGPA: ${gp} ÷ ${ch} = ${cgpa.toFixed(3)}`; // cgpa value
var form = document.querySelector("body > div.openerp.openerp_webclient_container > table > tbody > tr > td.oe_application > div > div > div > div > div > div.oe_view_manager_view_form > div > div.oe_form_container > div > div > div > div");
form.style.width = "900px"
form.style.margin = "100px 100px";
}
}
catch (err) {
console.log(err);
}
try {
// Create the "Print DMC" button
var printButton = document.createElement("button");
printButton.textContent = "Print DMC to PDF";
printButton.classList.add("oe_button");
printButton.classList.add("oe_highlight");
printButton.style.marginLeft = "5px";
// Append the button to a specific location
var header = document.querySelector('body > div.openerp.openerp_webclient_container > table > tbody > tr > td.oe_application > div > div > div > div > div > div.oe_view_manager_view_form > div > div.oe_form_container > div > div > div > div > table:nth-child(4) > tbody > tr:nth-child(2) > td:nth-child(1)');
if (header) {
header.appendChild(printButton);
}
// Add the event listener to the button to open the print window
printButton.addEventListener("click", function () {
// Inject print-specific CSS into the document
var style = document.createElement("style");
style.innerHTML = `
@media print {
body * {
visibility: hidden;
}
#notebook_page_9 > div > div > table, #notebook_page_9 > div > div > table * {
visibility: visible;
}
#notebook_page_9 > div > div {
position: absolute;
left: 0;
top: 0;
}
}`;
document.head.appendChild(style);
// Contains Text "Student DMC"
const t1 = document.querySelector("body > div.openerp.openerp_webclient_container > table > tbody > tr > td.oe_application > div > div > div > div > div > div.oe_view_manager_view_form > div > div.oe_form_container > div > div > div > div > table:nth-child(1)");
// Contains Registration Number and Student Name
const t2 = document.querySelector("body > div.openerp.openerp_webclient_container > table > tbody > tr > td.oe_application > div > div > div > div > div > div.oe_view_manager_view_form > div > div.oe_form_container > div > div > div > div > table:nth-child(2)");
// Contains Last Semester GPA Row and Final CGPA Row
const tfoot = document.querySelector("#notebook_page_9 > div > div > table > tfoot");
if (tfoot) {
tfoot.style.display = "none";
// Get all <tr> elements inside the <tfoot>
const trElements = tfoot.querySelectorAll("tr");
// Get the <tbody> where you want to append the copied rows
const tbody = document.querySelector("#notebook_page_9 > div > div > table > tbody");
// Check if tbody exists
if (tbody) {
// Loop through each <tr> element in the <tfoot>
trElements.forEach(function (tr) {
// Append the cloned <tr> to the <tbody>
tbody.appendChild(tr);
});
tbody.appendChild(document.createElement("tr"));
}
}
// Contains the table to be printed
const printContainer = document.querySelector("#notebook_page_9 > div > div ");
// Append Registration Number and Student Name to the top of the table
if (printContainer) {
if (t2) {
printContainer.insertBefore(t2, printContainer.firstChild);
}
// Insert t1 before the table
if (t1) {
printContainer.insertBefore(t1, printContainer.firstChild);
}
}
// Trigger the print dialog for the current page
window.print();
// Show white screen After print to prevent the user from interacting with the page
var whiteScreen = document.createElement("div");
whiteScreen.style.position = "fixed";
whiteScreen.style.top = "0";
whiteScreen.style.left = "0";
whiteScreen.style.width = "100%";
whiteScreen.style.height = "100%";
whiteScreen.style.backgroundColor = "white";
whiteScreen.style.zIndex = "9999";
whiteScreen.style.opacity = "1";
whiteScreen.style.display = "block";
// Reverse the changes by reload
window.location.reload();
});
}
catch (err) {
console.log(err);
}
}
// Call the main function when the page is loaded
window.onpopstate = function (event) {
const hashLoc = "view_type=form&model=obe.grade.book&menu_id=572"
const hash = window.location.hash;;
if (hash.includes(hashLoc)) {
main();
}
else{
sessionStorage.removeItem("refreshCount");
}
}