-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
214 lines (187 loc) · 7.24 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
async function fetchProfRMPInfo(professorName) {
const apiUrl = `http://localhost:3000/getRating?name=${encodeURIComponent(professorName)}`;
try {
const response = await fetch(apiUrl);
const data = await response.json();
console.log(professorName, "data",data)
return data
} catch (error) {
console.error('Error fetching rating:', error);
return [];
}
}
function processInfo(profRMPInfo) {
if (profRMPInfo.length === 0) return null;
if (profRMPInfo.length === 1) return profRMPInfo[0];
console.log('PROF LISTINGS > 1 for', profRMPInfo)
let totalWeightedRating = 0;
let totalWeightedDifficulty = 0;
let totalWeightedWouldTakeAgain = 0;
let totalRatings = 0;
for (const info of profRMPInfo) {
if (info.numRatings > 0) {
totalWeightedRating += info.rating * info.numRatings;
totalWeightedDifficulty += info.avgDifficulty * info.numRatings;
// Only include wouldTakeAgainPercent if it's not -1 (which likely indicates N/A)
if (info.wouldTakeAgainPercent !== -1) {
totalWeightedWouldTakeAgain += info.wouldTakeAgainPercent * info.numRatings;
}
totalRatings += info.numRatings;
}
}
console.log('totalRatings',totalRatings)
if (totalRatings === 0) return null;
return {
name: profRMPInfo[0].name,
rating: totalWeightedRating / totalRatings,
avgDifficulty: totalWeightedDifficulty / totalRatings,
wouldTakeAgainPercent: totalWeightedWouldTakeAgain / totalRatings,
numRatings: totalRatings,
school: profRMPInfo[0].school
};
}
async function processInstructors() {
const instructorContainers = document.querySelectorAll('.ls-instructors');
for (const container of instructorContainers) {
const instructorElements = container.querySelectorAll('span:not(.icon)');
for (const element of instructorElements) {
const professorName = element.textContent.trim();
if (professorName && professorName !== ',') {
const profRMPInfo = await fetchProfRMPInfo(professorName);
console.log('fetchRaitngs results',profRMPInfo)
addProfRMPInfoToDOM(element, profRMPInfo);
}
}
}
console.log("DONE PROCESSING INSTRUCTORS");
}
//if profRMPInfo.length = 0, means no teacher exists, if rating exists but = 0, may be N/a or acc 0
function addProfRMPInfoToDOM(professorElement, profRMPInfo) {
const profRMPInfoSpan = document.createElement('span');
const processedInfo = processInfo(profRMPInfo);
console.log('processe profRMPInfo',processedInfo)
if (processedInfo && processedInfo.numRatings > 0) {
profRMPInfoSpan.textContent = ` (RMP: ${processedInfo.rating.toFixed(1)})`;
profRMPInfoSpan.dataset.originalRatings = JSON.stringify(profRMPInfo);
profRMPInfoSpan.dataset.processedRating = JSON.stringify(processedInfo);
profRMPInfoSpan.addEventListener('click', showDetailedRatingsPopup);
} else {
profRMPInfoSpan.textContent = ' --';
}
profRMPInfoSpan.style.color = '#007bff';
professorElement.appendChild(profRMPInfoSpan);
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', processInstructors);
} else {
processInstructors();
}
function showDetailedRatingsPopup(event) {
const span = event.target;
const originalRatings = JSON.parse(span.dataset.originalRatings);
const processedRating = JSON.parse(span.dataset.processedRating);
let detailedInfo = '';
if (processedRating.isWeightedAverage) {
detailedInfo += `<h3 class="popup-heading">Weighted Averages:</h3>`;
detailedInfo += `<p class="popup-detail"><strong>Rating:</strong> ${processedRating.rating.toFixed(2)}</p>`;
detailedInfo += `<p class="popup-detail"><strong>Difficulty:</strong> ${processedRating.avgDifficulty.toFixed(2)}</p>`;
detailedInfo += `<p class="popup-detail"><strong>Would Take Again:</strong> ${processedRating.wouldTakeAgainPercent.toFixed(2)}%</p>`;
detailedInfo += `<p class="popup-detail"><strong>Total Ratings:</strong> ${processedRating.numRatings}</p>`;
detailedInfo += `<h3 class="popup-heading">Original Listings:</h3>`;
}
originalRatings.forEach((rating, index) => {
const indexText = index > 0 ? ` (${index + 1})` : '';
detailedInfo += `<h4 class="popup-professor-name"><a href="https://www.ratemyprofessors.com/professor/${rating.urlId}" target="_blank">${rating.name}${indexText}</a></h4>`;
if (rating.numRatings === 0) {
detailedInfo += `<p class="popup-no-ratings">No ratings</p>`;
} else {
detailedInfo += `<p class="popup-detail"><strong>Rating:</strong> ${rating.rating}</p>`;
detailedInfo += `<p class="popup-detail"><strong>Difficulty:</strong> ${rating.avgDifficulty}</p>`;
detailedInfo += `<p class="popup-detail"><strong>Would Take Again:</strong> ${Math.round(rating.wouldTakeAgainPercent)}%</p>`;
detailedInfo += `<p class="popup-detail"><strong>Number of Ratings:</strong> ${rating.numRatings}</p>`;
}});
// Create popup
const popup = document.createElement('div');
popup.innerHTML = detailedInfo;
popup.classList.add('popup');
// Add close button
const closeButton = document.createElement('button');
closeButton.textContent = 'Close';
closeButton.classList.add('popup-close-button');
closeButton.addEventListener('click', () => document.body.removeChild(popup));
popup.appendChild(closeButton);
document.body.appendChild(popup);
// Close popup when clicking outside
document.addEventListener('click', function closePopup(e) {
if (!popup.contains(e.target) && e.target !== span) {
document.body.removeChild(popup);
document.removeEventListener('click', closePopup);
}
});
const style = document.createElement('style');
style.textContent = `
.popup {
position: fixed;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
background-color: #fff;
border: 1px solid #ddd;
border-radius: 10px;
padding: 20px;
z-index: 1000;
max-width: 400px;
max-height: 80vh;
overflow-y: auto;
box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
font-family: 'Arial', sans-serif;
color: #333;
animation: fadeIn 0.3s ease-in-out;
}
.popup-heading {
font-size: 18px;
margin-bottom: 10px;
font-weight: bold;
color: #0056b3; /* Softer blue */
}
.popup-detail {
font-size: 16px;
margin-bottom: 8px;
color: #555;
}
.popup-professor-name a {
font-size: 16px;
color: #0056b3; /* Softer blue */
text-decoration: underline; /* Always underlined */
font-weight: bold;
}
.popup-no-ratings {
font-size: 14px;
color: #999;
}
.popup-close-button {
margin-top: 15px;
background-color: #f0f0f0;
color: #333;
padding: 8px 15px;
border: 1px solid #ddd;
border-radius: 8px;
cursor: pointer;
font-size: 14px;
transition: background-color 0.2s ease;
}
.popup-close-button:hover {
background-color: #e0e0e0;
}
@keyframes fadeIn {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
`;
// Append the style to the document head
document.head.appendChild(style);
}