Skip to content

Commit

Permalink
Merge pull request #4 from mahfoozm/mv3
Browse files Browse the repository at this point in the history
migrate to mv3, use fetch
  • Loading branch information
mahfoozm authored Jul 8, 2023
2 parents 5e1948e + 962ef27 commit 4311a36
Show file tree
Hide file tree
Showing 4 changed files with 982 additions and 4,286 deletions.
170 changes: 99 additions & 71 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,99 +4,127 @@
// The background script is necessary because the website is not allowed to make requests to RMP
// due to CORS restrictions.

const {GraphQLClient, gql} = require('graphql-request');

// GraphQL queries to get the professor's info from RMP
const searchProfessorQuery = gql`
query NewSearchTeachersQuery($text: String!, $schoolID: ID!)
{
newSearch {
teachers(query: {text: $text, schoolID: $schoolID}) {
edges {
cursor
node {
id
firstName
lastName
school {
name
id
}
}
}
}
}
}
`;


const getProfessorQuery = gql`
query TeacherRatingsPageQuery(
$id: ID!
) {
node(id: $id) {
... on Teacher {
id
firstName
lastName
school {
name
id
city
state
}
avgDifficulty
avgRating
department
numRatings
legacyId
wouldTakeAgainPercent
}
id
}
}
`;

// publicly available token, yorkU school ID
const AUTH_TOKEN = 'dGVzdDp0ZXN0';
const SCHOOL_ID = 'U2Nob29sLTE0OTU=';

const client = new GraphQLClient('https://www.ratemyprofessors.com/graphql', {
headers: {
authorization: `Basic ${AUTH_TOKEN}`
}
});

// for searchProfessor and getProfessor, use a self hosted proxy to bypass CORS restrictions
const searchProfessor = async (name, schoolID) => {
const response = await client.request(searchProfessorQuery, {
text: name,
schoolID
});

if (response.newSearch.teachers === null) {
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 === null) {
return [];
}

return response.newSearch.teachers.edges.map((edge) => edge.node);
return json.data.newSearch.teachers.edges.map((edge) => edge.node);
};


const getProfessor = async (id) => {
const response = await client.request(getProfessorQuery, {id});
return response.node;
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 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 professor's name to match the format used by RMP's GraphQL API
// (the source of all of my pain)
const normalizedName = professorName.normalize('NFKD');
const normalizedName = professorName.normalize("NFKD");
const professors = await searchProfessor(normalizedName, SCHOOL_ID);

if (professors.length === 0) {
return { error: 'Professor not found' };
// 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_ID);

if (modifiedProfessors.length === 0) {
return { error: "Professor not found" };
}

const professorID = modifiedProfessors[0].id;
const professor = await getProfessor(professorID);
console.log(professor);
return professor;
}

return { error: "Professor not found" };
}

const professorID = professors[0].id;
const professor = await getProfessor(professorID);
console.log(professor);
return professor;
}

Expand Down
12 changes: 6 additions & 6 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"manifest_version": 2,
"manifest_version": 3,
"name": "YorkRMP",
"description": "RMP extension for YorkU's course page.",
"version": "1.0.1",
"version": "1.1.0",
"icons": { "128": "assets/icon128.png" },
"permissions": [
"activeTab", "webRequest", "https://w2prod.sis.yorku.ca/*", "https://www.ratemyprofessors.com/*"
],
"permissions": ["activeTab", "webRequest", "https://w2prod.sis.yorku.ca/*", "https://www.ratemyprofessors.com/*"],
"optional_permissions": ["webNavigation", "tabs"],
"background": {
"scripts": ["background-packed.js"]
"service_worker": "background.js"
},
"event_page": "event.html",
"content_scripts": [
{
"matches": ["https://w2prod.sis.yorku.ca/*"],
Expand Down
Loading

0 comments on commit 4311a36

Please sign in to comment.