-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontentScript.js
More file actions
66 lines (57 loc) · 1.97 KB
/
contentScript.js
File metadata and controls
66 lines (57 loc) · 1.97 KB
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
function checkCourseGradesPage() {
const navElement = document.getElementById('js_navToApp');
if (navElement) {
const activeElement = navElement.querySelector('li a.active');
if (activeElement) {
const gotoAttribute = activeElement.getAttribute('goto');
if (gotoAttribute && gotoAttribute.startsWith('education/grades')) {
return true;
}
}
}
return false;
}
function displayErrorMessage() {
alert('Please go to the Course Grades page (النتائج الدراسية).');
}
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === 'start') {
console.log('Start message received');
if (!checkCourseGradesPage()) {
displayErrorMessage();
sendResponse({ error: 'Not on the Course Grades page' });
return;
}
const tfootElements = document.querySelectorAll('tfoot');
console.log('tfoot elements:', tfootElements);
let totalHours = 0;
tfootElements.forEach((tfoot) => {
const divs = tfoot.querySelectorAll('div');
if (divs.length > 0) {
const lastDiv = divs[divs.length - 1];
console.log('last div:', lastDiv);
const textContent = lastDiv.textContent.trim();
if (textContent) {
// Extract numbers from the text content
const numbers = textContent.match(/-?\d+(\.\d+)?/g);
if (numbers) {
numbers.forEach((number) => {
const numericValue = parseFloat(number.replace(/,/g, ''));
if (!isNaN(numericValue)) {
totalHours += numericValue;
}
});
}
}
}
});
// Store the total hours in chrome storage
chrome.storage.local.set({ totalHours }, function () {
console.log('Total hours stored:', totalHours);
// Send the total hours back to the popup
sendResponse({ totalHours });
});
// Return true to indicate you want to send a response asynchronously
return true;
}
});