-
Notifications
You must be signed in to change notification settings - Fork 1
/
background.js
156 lines (149 loc) · 4.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
// This file is responsible for handling the communication between the extension and the website
// It sends a message to the background script to retrieve the professor's info from RMP
// The background script then sends the info back to this script, which inserts it into the page
// The background script is necessary because the website is not allowed to make requests to RMP
// due to CORS restrictions.
// console.log('background script running');
const AUTH_TOKEN = 'dGVzdDp0ZXN0';
// UWS, UWB, UWT
const SCHOOL_ID = ['U2Nob29sLTE1MzA=', 'U2Nob29sLTQ0NjY=', 'U2Nob29sLTQ3NDQ='];
// Not needed for MV2
const PROXY_URL = '';
const searchProfessor = async (name, schoolID) => {
console.log('Searching for professor:', name);
try {
const response = await fetch(`${PROXY_URL}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 text = await response.text();
let json;
try {
json = JSON.parse(text);
// console.log('json response for ' + name + ' at ' + schoolID, json);
} catch (error) {
console.error('Error parsing JSON:', error);
throw new Error('Error parsing JSON: ' + text);
}
if (json.data.newSearch.teachers === null) {
// console.log('No results found for professor:', name);
return [];
}
return json.data.newSearch.teachers.edges.map((edge) => edge.node);
} catch (error) {
console.error('Error searching for professor:', error);
throw error;
}
};
const getProfessor = async (id) => {
// console.log('Fetching professor data for ID:', id);
try {
const response = await fetch(`${PROXY_URL}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();
// console.log('Professor data by ID: ' + id, json.data.node);
return json.data.node;
} catch (error) {
console.error('Error fetching professor data:', error);
throw error;
}
};
async function sendProfessorInfo(professorName) {
const normalizedName = professorName.normalize('NFKD');
try {
// for each in SCHOOL_ID, search for professor
// if found, get professor info, if not found, continue
let professorID;
for (let i = 0; i < SCHOOL_ID.length; i++) {
const professors = await searchProfessor(normalizedName, SCHOOL_ID[i]);
if (professors.length === 0) {
// console.log('No ' + professorName + ' found at RMP for schoolID:', SCHOOL_ID[i] + ', continuing to next SchoolID');
continue;
}
professorID = professors[0].id;
console.log('SUCCESS! ' + professorName + ' professorID: ' + professorID + ' found at schoolID: ' + SCHOOL_ID[i]);
break;
}
if (professorID === undefined) {
console.log('No ' + professorName + ' found for any schoolID:', SCHOOL_ID);
return { error: professorName + ' not found on RMP for any given SCHOOL_ID' };
}
const professor = await getProfessor(professorID);
console.log(professor);
return professor;
} catch (error) {
console.error('Error sending professor info for ' + professorName, error);
throw error;
}
}
chrome.runtime.onConnect.addListener((port) => {
port.onMessage.addListener((request) => {
sendProfessorInfo(request.professorName)
.then((professor) => {
port.postMessage(professor);
})
.catch((error) => {
console.error('Error:', error);
port.postMessage({ error });
});
});
});