forked from mahfoozm/YorkURMP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
145 lines (132 loc) · 3.75 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
// lots of annoying stuff to get around CORS and MV3 restrictions
// publicly available token, yorkU school ID
const AUTH_TOKEN = 'dGVzdDp0ZXN0';
// currently saerching for profs at:
// YorkU (Keele and Glendon), TMU, and UofT (SG)
const SCHOOL_IDS = [
"U2Nob29sLTE0OTU=",
"U2Nob29sLTEyMTI1",
"U2Nob29sLTE0NzE=",
"U2Nob29sLTE0ODQ=",
];
// for searchProfessor and getProfessor, use a self hosted proxy to bypass CORS restrictions
const searchProfessor = async (name, schoolIDs) => {
for (const schoolID of schoolIDs) {
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 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);
}
}
return [];
};
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;
};
async function sendProfessorInfo(professorName) {
// normalize the prof's name before sending to RMP API
// (the source of all of my pain)
const normalizedName = professorName.normalize("NFKD");
const professors = await searchProfessor(normalizedName, SCHOOL_IDS);
if (professors.length === 0) {
// try searching without the middle name/initial
const names = normalizedName.split(" ");
if (names.length >= 2) {
const modifiedName = `${names[0]} ${names[names.length - 1]}`;
const modifiedProfessors = await searchProfessor(modifiedName, SCHOOL_IDS);
if (modifiedProfessors.length === 0) {
return { error: "Professor not found" };
}
const professorID = modifiedProfessors[0].id;
const professor = await getProfessor(professorID);
return professor;
}
return { error: "Professor not found" };
}
const professorID = professors[0].id;
const professor = await getProfessor(professorID);
return professor;
}
chrome.runtime.onConnect.addListener((port) => {
port.onMessage.addListener((request) => {
sendProfessorInfo(request.professorName).then((professor) => {
port.postMessage(professor);
}).catch((error) => {
console.log('Error:', error);
port.postMessage({error});
});
});
});