-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
215 lines (199 loc) · 7.22 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
cache = []
function handle_background_color(rating, numRatings) {
if (rating === undefined || numRatings === 0) {return '#DDDDDD'}
if (rating <= 1) {return '#E43939'}
if (rating <= 2) {return '#FD7224'}
if (rating <= 3) {return '#FFBC3D'}
if (rating <= 4) {return '#B2E545'}
return '#00BA00'
}
function handle_instructor_info_display(result) {
const result_name = result[0]
const result_data = result[1]
const info_box = document.createElement('div')
info_box.className = 'instructor-info'
const background_color = handle_background_color(result_data.avgRating, result_data.numRatings)
info_box.style.backgroundColor = background_color
info_box.innerHTML = `<div class="instructor-name">${result_name}</div>`
const expanded_details = document.createElement('div')
expanded_details.className = 'instructor-details'
expanded_details.style.backgroundColor = background_color
if (result_data.numRatings === 0) {
expanded_details.innerHTML = `
<div class="spacer"></div>
${result_data.department}
<div class="spacer"></div>
No data available
<div class="spacer"></div>
`
} else if (result_data.error) {
expanded_details.textContent = 'Instructor is not on RMP'
} else {
const rating_text = result_data.numRatings === 1 ? 'rating' : 'ratings'
let wouldTakeAgainHtml = '';
if (result_data.takeAgain !== -1) {
const rounded_would_take_again = Math.round(result_data.takeAgain);
wouldTakeAgainHtml = `${rounded_would_take_again}% would take again<br>`;
}
expanded_details.innerHTML = `
<div class="spacer"></div>
${result_data.department}<br>
${result_data.numRatings} ${rating_text}
<div class="spacer"></div>
Rating: ${result_data.avgRating}/5<br>
Difficulty: ${result_data.avgDifficulty}/5<br>
${wouldTakeAgainHtml}
<div class="spacer"></div>
`
}
info_box.appendChild(expanded_details)
info_box.addEventListener('click', (event) => {
event.stopPropagation()
if (result_data.legacyId === undefined) {
const school_id = 1072
const url = `https://www.ratemyprofessors.com/search/professors/${school_id}/?q=${encodeURIComponent(result_name)}`
window.open(url, '_blank')
}
else {
const url = `https://www.ratemyprofessors.com/professor/${result_data.legacyId}`
window.open(url, '_blank')
}
})
return info_box
}
function handle_disgested_rmp_response(search_term) {
return [search_term, cache[search_term]]
}
// gets called for every name instance
function handle_raw_rmp_response(response_data, search_term) {
cache[search_term] = {}
if (
!response_data.data ||
!response_data.data.search ||
!response_data.data.search.teachers ||
!response_data.data.search.teachers.edges
) {
cache[search_term] = {error: "Edge finding error"}
}
const edges = response_data.data.search.teachers.edges
if (edges.length === 0) {
cache[search_term] = {error: "No RMP results"}
}
const nodes = edges.map(edge => edge.node)
const [lastName, firstInitial] = search_term.split(' ')
const matching_instructors = nodes.filter(
node => {
const lastNameMatch = node.lastName.toUpperCase() === lastName.toUpperCase()
const firstInitialMatch = node.firstName[0].toUpperCase() === firstInitial.toUpperCase()
return lastNameMatch && firstInitialMatch
}
)
if (matching_instructors.length > 0) {
const selected_match = matching_instructors[0]
cache[search_term] = {
firstName: selected_match.firstName,
lastName: selected_match.lastName,
id: selected_match.id,
legacyId: selected_match.legacyId,
department: selected_match.department,
avgRating: selected_match.avgRating,
avgDifficulty: selected_match.avgDifficulty,
numRatings: selected_match.numRatings,
takeAgain: selected_match.wouldTakeAgainPercent
}
}
else {
cache[search_term] = {error: "No matching instructors found"}
}
return handle_disgested_rmp_response(search_term)
}
// gets called for every name instance
function fetch_rmp_data(search_term) {
return new Promise((resolve, reject) => {
if (cache[search_term]) {
resolve(handle_disgested_rmp_response(search_term));
} else {
console.log('FETCHING', search_term)
chrome.runtime.sendMessage(
{action: "fetchRMP", name: search_term},
function(response) {
if (response.data) {
resolve(handle_raw_rmp_response(response.data, search_term));
} else if (response.error) {
console.error('Error:', response.error);
reject(response.error);
}
}
)
}
})
}
// gets called for every name instance
async function handle_name_text_string(instructor_name) {
let names = instructor_name.split(', ')
let results = await Promise.all(names.map(name => fetch_rmp_data(name)))
return results
}
// gets called every time table is updated
async function table_modifier(table_body) {
const rows = table_body.querySelectorAll('tr')
for (const row of rows) {
const cells = row.querySelectorAll('td')
if (cells.length >= 3) {
const nameCell = cells[2]
// Check if the cell has already been modified
if (!nameCell.hasAttribute('data-rmp-modified')) {
const nameText = nameCell.textContent.trim()
if (nameText !== "") {
try {
const result = await handle_name_text_string(nameText)
nameCell.innerHTML = ''
result.forEach((result) => {
const instructor_content = handle_instructor_info_display(result)
nameCell.appendChild(instructor_content)
})
nameCell.setAttribute('data-rmp-modified', 'true')
} catch (error) {
console.error('Error processing name:', nameText, error)
}
}
}
}
}
}
function retryOperation(operation, retries, maxRetries, refresh_interval) {
if (retries < maxRetries) {
setTimeout(() => operation(retries + 1, maxRetries, refresh_interval), refresh_interval)
}
}
async function modifyLiveTable(retries = 0, maxRetries = 100, refresh_interval = 100) {
const tbody_dir = "#root > div > div > div._root_szin5_37 > section > div > table > tbody"
const table_body = document.querySelector(tbody_dir)
if (table_body) {
await table_modifier(table_body)
const observer = new MutationObserver(async (mutations) => {
for (let mutation of mutations) {
if (mutation.type === 'childList') {
for (let node of mutation.addedNodes) {
if (node.nodeType === Node.ELEMENT_NODE && (node.tagName === 'TR' || node.querySelector('tr'))) {
await table_modifier(table_body)
break
}
}
}
}
})
observer.observe(table_body, { childList: true, subtree: true })
} else {
retryOperation(modifyLiveTable, retries, maxRetries, refresh_interval)
}
}
const catalog_url = "https://berkeleytime.com/catalog"
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "checkUrl" && window.location.href.includes(catalog_url)) {
modifyLiveTable().catch(console.error)
}
})
if (window.location.href.includes(catalog_url)) {
modifyLiveTable().catch(console.error)
}