-
Notifications
You must be signed in to change notification settings - Fork 2
/
background.js
202 lines (189 loc) · 6.68 KB
/
background.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
const AUTH_TOKEN = 'dGVzdDp0ZXN0';
const schoolID = 'U2Nob29sLTE5Njc='; // De Anza College ID
let professorRatingMap = new Map();
let professors;
let classData;
/**
* Listens for messages from the content scripts.
* Performs actions based on the type of message received.
*/
chrome.runtime.onMessage.addListener((message) => {
if (message.type === "ProfessorSets") {
professors = Object.values(message.data);
Promise.all(professors.map(async (prof) => {
const modifiedName = getFirstAndLastName(prof);
const professor = await searchProfessorOnRmp(modifiedName, schoolID);
if (professor && professor.length > 0) {
const professorRating = await getProfessor(professor[0].id);
professorRatingMap.set(prof, {
"avgRating": professorRating.avgRating,
"avgDifficulty": professorRating.avgDifficulty,
"takeAgain": professorRating.wouldTakeAgainPercent,
"link": professorRating.legacyId,
"online": professorRating.online
});
}
})).then(() => {
const professorRatingObj = Object.fromEntries(professorRatingMap);
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
chrome.tabs.sendMessage(tabs[0].id, { type: "ProfessorRatings", data: professorRatingObj });
});
});
}
else if (message.type === "ClassData") {
Promise.all(professors.map(async (prof) => {
const modifiedName = getFirstAndLastName(prof);
const professor = await searchProfessorOnRmp(modifiedName, schoolID);
if (professor && professor.length > 0) {
const professorRating = await getProfessor(professor[0].id);
professorRatingMap.set(prof, {
"avgRating": professorRating.avgRating,
"avgDifficulty": professorRating.avgDifficulty,
"takeAgain": professorRating.wouldTakeAgainPercent,
"link": professorRating.legacyId,
"online": professorRating.online
});
}
})).then(() => {
classData = new Map(message.data);
if (professorRatingMap !== undefined && classData !== undefined) {
classData.forEach((value, key) => {
const name = value.profName;
const rating = professorRatingMap.get(name);
if (rating) {
value.avgRating = rating.avgRating;
value.avgDifficulty = rating.avgDifficulty;
value.takeAgain = rating.takeAgain;
value.link = rating.link;
}
else {
value.avgRating = "N/A";
value.avgDifficulty = "N/A";
value.takeAgain = "N/A";
value.link = "undefined";
}
});
}
})
}
});
/**
* Listens for messages from the popup.js.
* Send the data when received a request.
*/
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type === "GET_PROFESSOR_RATINGS_AND_CLASS_DATA" && classData !== undefined) {
sendResponse(Array.from(classData));
}
return true;
});
// HELPER FUNCTIONS
/**
* Searches for a professor on RateMyProfessors.com using the provided name and school ID.
* @param {string} name - The name of the professor to search for.
* @param {string} schoolID - The ID of the school where the professor is affiliated.
* @returns {Promise<Array<Object>>} - A promise that resolves to an array of professor objects found on RateMyProfessors.com.
*/
async function searchProfessorOnRmp(name, schoolID) {
const response = await fetch(
`https://www.ratemyprofessors.com/graphql`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Basic ${AUTH_TOKEN}`,
},
body: JSON.stringify({
query: `query NewSearchTeachersQuery($text: String!, $schoolID: ID!) {
newSearch {
teachers(query: {text: $text, schoolID: $schoolID}) {
edges {
cursor
node {
id
firstName
lastName
school {
name
id
}
}
}
}
}
}`,
variables: {
text: name,
schoolID,
},
}),
}
);
const json = await response.json();
if (json.data.newSearch.teachers.edges.length > 0) {
return json.data.newSearch.teachers.edges.map((edge) => edge.node);
} else {
console.log("Can't find professor: " + name);
return [];
}
}
/**
* Retrieves professor information from RateMyProfessors API.
* @param {string} id - The ID of the professor.
* @returns {Promise<Object>} - A promise that resolves to the professor information.
*/
const getProfessor = async (id) => {
const response = await fetch(
// self hosted proxy
`https://www.ratemyprofessors.com/graphql`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Basic ${AUTH_TOKEN}`,
},
body: JSON.stringify({
query: `query TeacherRatingsPageQuery($id: ID!) {
node(id: $id) {
... on Teacher {
id
firstName
lastName
school {
name
id
city
state
}
avgDifficulty
avgRating
department
numRatings
legacyId
wouldTakeAgainPercent
}
id
}
}`,
variables: {
id,
},
}),
}
);
const json = await response.json();
return json.data.node;
};
/**
* Returns the first and last name from a given full name.
* @param {string} name - The full name.
* @returns {string} - The first and last name.
*/
function getFirstAndLastName(name) {
const nameParts = name.split(/[\s-]+/);
if (nameParts.length === 2) {
return name;
} else {
return `${nameParts[0]} ${nameParts[nameParts.length - 1]}`;
}
}