-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontent_script.js
More file actions
104 lines (97 loc) · 3.21 KB
/
Copy pathcontent_script.js
File metadata and controls
104 lines (97 loc) · 3.21 KB
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
'use strict';
/*
* RateMyProfessor Chrome Extension
* Not for public distribution
* Dev: Jonathan Cady
* Version: 1.2
*/
checkDivs();
/*
* Checks the professor names and cleans them so they can be checked with
* RateMyProfessor.com
*/
async function checkDivs() {
console.log('checking...');
let instructorNames = [];
let divs = document.querySelectorAll(".course-section-instructor");
let count = 0;
while (divs.length == 0 && count < 20) {
await sleep(200);
divs = document.querySelectorAll(".course-section-instructor");
count++;
}
if (divs.length > 0) {
divs.forEach(function (el) {
let instructorName = el.children[0];
if (instructorName != null) {
let insideText = instructorName.innerText;
if (insideText.toLowerCase().includes("view syllabus")) {
instructorName.innerText = insideText.substr(0, insideText.indexOf("View syllabus"));
insideText = instructorName.innerText;
}
if (!insideText.includes("--")) {
let splitIt = (insideText).split(" ");
if (splitIt[1].includes(".") || splitIt[1].length == 1 | splitIt[2] != null) {
let cleanName = splitIt[0] + " " + splitIt[2];
instructorName.innerText = cleanName;
}
instructorNames.push(instructorName);
}
}
});
for (let i = 0; i < instructorNames.length; i++) {
getScores(instructorNames[i].innerText, instructorNames[i]);
}
} else {
console.log("Unable to find professors on this page.");
}
}
/*
* Sleep function used to allow for the browser to load the course information
*/
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
/*
* Loads creates the HTML elements that will be inserted into the page based
* on the returned content
*/
function createElements(message, originalEl) {
if (message.status == "success") {
let quality = message.overallQuality;
let takeAgain = message.takeAgain;
let difficulty = message.difficulty;
let link = message.url;
let qualityEl = $(`<p>Overall Quality: ${quality}</p>`);
let takeAgainEl = $(`<p>Would Take Again: ${takeAgain}</p>`);
let difficultyEl = $(`<p>Level of Difficulty: ${difficulty}</p>`);
let linkEl = $(`<a href=${link} target='_blank'>Link</a>`);
let overallDiv = $("<div></div>");
$(originalEl).css('text-align', 'center');
let upperEl = $(originalEl).parents('.course-section-instructor');
overallDiv.append(qualityEl, takeAgainEl, difficultyEl, linkEl);
overallDiv.addClass("rmp-info");
$(originalEl).children('.rmp-loading').remove();
upperEl.append(overallDiv);
} else {
$(originalEl).children('.rmp-loading').remove();
let failedDiv = $('<p>No RMP data!</p>');
let upperEl = $(originalEl).parents('.course-section-instructor');
upperEl.append(failedDiv);
}
}
/*
* Sends the professor names to be checked and returns a response with content
* that will be injected into the page
*/
function getScores(name, element) {
$(element).append($("<p class='rmp-loading'>Loading!</p>"));
let cleanedName = name.replace(" ", "+");
let url = `https://students.washington.edu/joncady/projects/ratemyprofessor/rmp.php?name=${cleanedName}`;
var el = element;
fetch(url).then(function (reponse) {
return reponse.json();
}).then(function (myJson) {
createElements(myJson, el);
});
}