-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontent.js
237 lines (209 loc) · 8.04 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
const instructorIndex = 14;
const rmpIndex = instructorIndex + 1;
let profSet;
let classData = new Map();
/**
* Main function that runs when the page loads.
* Scrape Data -> Send Data to Background -> Send Data to Content
*/
window.onload = () => {
addLoadingRating();
profSet = scrapeInstructors(instructorIndex);
classData = scrapeRows();
if (profSet !== null || classData !== null) {
chrome.runtime.sendMessage({ type: "ProfessorSets", data: Array.from(profSet) }, () => {
// console.log("professor set sent from content to background");
});
chrome.runtime.sendMessage({ type: "ClassData", data: Array.from(classData) }, () => {
// console.log("class data sent from content to background");
});
}
}
/**
* Listens for a message from the background script containing professor ratings.
* This message is used to update the ratings column in the table.
*/
chrome.runtime.onMessage.addListener((message) => {
if (message.type === "ProfessorRatings") {
const professorRatings = message.data;
updateRatings(professorRatings);
}
});
/**
* Scrapes the instructors' names from a table and returns a set of unique names.
*
* @param {number} instructorIndex - The index of the column containing the instructor names.
* @returns {Set<string> | null} - A set of unique instructor names, or null if no rows are found.
*/
function scrapeInstructors(instructorIndex) {
const rows = document.querySelectorAll('.datadisplaytable tbody tr');
let profSet = new Set();
if (!rows || rows.length === 0) {
return null;
}
rows.forEach((row) => {
const cells = row.querySelectorAll('td');
if (instructorIndex < cells.length) {
// clean up professor name
const profName = cells[instructorIndex].textContent.trim().replace(/\(P\)|\(T\)/g, '')
.replace(/\s\s+/g, ' ').replace(/\s\(/g, '(').trim();
if (profName !== null && profName !== undefined) {
profSet.add(profName);
}
}
})
return profSet;
}
// scrape CRN, Rem, WL Rem
/**
* Scrapes the rows of a table and returns a map of course information.
* @returns {Map<string, { profName: string, rem: string, wlRem: string }>} A map containing course information
* with CRN as the key and an object with professor name, remaining seats, and waitlist remaining seats as the value.
*/
function scrapeRows() {
const rows = document.querySelectorAll('.datadisplaytable tbody tr');
if (!rows || rows.length === 0) {
return null;
}
const tempMap = new Map();
rows.forEach((row) => {
const cells = row.querySelectorAll('td');
if (cells.length === 20) {
const crn = cells[1].textContent.trim();
const rem = cells[12].textContent.trim();
const wlRem = cells[13].textContent.trim();
const location = cells[17].textContent.trim();
let online = false;
if (location === "DW ONLINE") {
online = true;
}
const profName = cells[instructorIndex].textContent.trim().replace(/\(P\)|\(T\)/g, '')
.replace(/\s\s+/g, ' ').replace(/\s\(/g, '(').trim();
if (crn !== null && crn !== undefined && crn !== "") {
tempMap.set(crn, { profName, rem, wlRem, online })
}
}
})
return tempMap;
}
/**
* Adds a loading rating column to the table.
*/
function addLoadingRating() {
const rows = document.querySelectorAll('.datadisplaytable tbody tr');
if (!rows || rows.length === 0) {
return;
}
for (let i = 1; i < rows.length; i++) {
if (rows[i].cells.length <= 3) {
return;
}
const ratingIndex = rows[i].cells.length < instructorIndex + 1 ? rows[i].cells.length : 15;
const newCell = rows[i].insertCell(ratingIndex);
if (i === 1) {
newCell.classList.add('ddheader');
newCell.textContent = "RMP Rating";
} else {
newCell.classList.add('dddefault');
newCell.textContent = "N/A";
}
}
}
/**
* Updates the ratings of professors in a table based on the provided professor ratings.
*
* @param {Object} professorRatings - The object containing the ratings of professors.
*/
function updateRatings(professorRatings) {
const rows = document.querySelectorAll('.datadisplaytable tbody tr');
if (!rows || rows.length === 0) {
return;
}
for (let i = 2; i < rows.length; i++) {
if (rows[i].cells.length <= 3 || instructorIndex >= rows[i].cells.length) {
continue;
}
const profName = rows[i].cells[instructorIndex].textContent.trim().replace(/\(P\)|\(T\)/g, '').replace(/\s\s+/g, ' ').replace(/\s\(/g, '(').trim();
const style = document.createElement('style');
style.textContent = `
.rating-low {
display: inline-block;
background-color: rgba(246, 43, 43, 0.736);
color: white;
padding: 2px 7px;
border-radius: 7px;
}
.rating-mid {
display: inline-block;
background-color: orange;
color: white;
padding: 2px 7px;
border-radius: 7px;
}
.rating-low:hover, .rating-mid:hover, .rating-high:hover {
transform: scale(1.05);
opacity: 0.8;
}
.not-found {
display: inline-block;
background-color: rgba(208, 201, 189, 0.821);
color: rgb(89, 88, 88);
padding: 2px 7px;
border-radius: 7px;
pointer-events: none
}
.rating-high {
display: inline-block;
background-color: rgba(10, 192, 10, 0.83);
color: white;
padding: 2px 7px;
border-radius: 7px;
}
.rating-align {
text-align: center !important;
vertical-align: middle;
}
.black-link {
color: black !important;
text-decoration: none;
}`
document.head.appendChild(style);
const rating = professorRatings[profName]?.avgRating || "N/A";
const ratingElement = document.createElement('a');
ratingElement.href = `https://www.ratemyprofessors.com/professor/${professorRatings[profName]?.link}`
ratingElement.target = "_blank";
ratingElement.classList.add('black-link');
const spanWrapper = document.createElement('span');
spanWrapper.textContent = rating;
if (rating === "N/A") {
spanWrapper.classList.add('not-found');
ratingElement.style.fontSize = "11px";
ratingElement.style.color = "gray";
ratingElement.style.pointerEvents = "none";
}
else if (professorRatings[profName].avgRating >= 4) {
spanWrapper.classList.add('rating-high');
} else if (professorRatings[profName].avgRating >= 2.9) {
spanWrapper.classList.add('rating-mid');
} else {
spanWrapper.classList.add('rating-low');
}
ratingElement.appendChild(spanWrapper);
rows[i].cells[rmpIndex].innerHTML = '';
rows[i].cells[rmpIndex].classList.add('rating-align');
rows[i].cells[rmpIndex].appendChild(ratingElement);
// Map professors' names to de anza profile links
const words = profName.split(" ");
const trimmedWords = words.map(word => word.trim());
const profFirstName = trimmedWords[0];
const profLastName = trimmedWords[trimmedWords.length - 1];
const profileLink = "https://www.deanza.edu/directory/user.html?u=" + profLastName + profFirstName;
// Embedding link to prof name
const profLink = document.createElement('a');
profLink.textContent = rows[i].cells[instructorIndex].textContent;
rows[i].cells[instructorIndex].textContent = '';
profLink.target = '_blank';
profLink.href = profileLink;
rows[i].cells[instructorIndex].appendChild(profLink);
}
}