-
Notifications
You must be signed in to change notification settings - Fork 0
/
RateMyBull.js
119 lines (102 loc) · 4.12 KB
/
RateMyBull.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
chrome.runtime.onMessage.addListener(function(msg, sender, sendResponse) {
if (msg === 'url-update') {
setTimeout(grabProfs, 1000);
setTimeout(runExtension, 1000);
}
});
//Grabs the names of the professor an element and adds a class name to the element
function grabProfs(){
var professorElementNode;
var tableElementNode = document.querySelectorAll('.section-detail-grid.table-bordered-wrap>tbody>tr>td>div');
for (var i = 0; i < tableElementNode.length; i++) {
if(tableElementNode[i].children.length < 1 && tableElementNode[i].innerText != 'Not Assigned') {
tableElementNode[i].className = 'professor';
}
}
}
function runExtension(){
var professorsArray = [];
var professorElementNode = document.querySelectorAll('.professor');
for (var i = 0; i < professorElementNode.length; i++) {
var professorName = professorElementNode[i].innerText;
professorName = professorName.trim();
professorName = professorName.replace(/,/g, '');
professorName = professorName.split([' ']);
var firstName = professorName[1];
var lastName = professorName[0];
professorsArray[i] = {
first: firstName,
last: lastName,
};
$('.rating').remove();
$('.review').remove();
}
// Adds an element and inserts text for the rating and review count underneath the professor element
for (var i = 0; i < professorElementNode.length; i++) {
var rating;
var wouldTakeAgain;
var difficulty;
const parentNode = professorElementNode[i].parentNode;
const ratingElement = document.createElement('a');
const tooltiptext = document.createElement('span')
const reviewElement = document.createElement('a');
parentNode.appendChild(ratingElement);
parentNode.appendChild(reviewElement);
reviewElement.className = 'review';
ratingElement.className = 'rating';
var profUrl = 'https://search.mtvnservices.com/typeahead/suggest/?solrformat=true&rows=20&q=' + professorsArray[i].first +'+'+ professorsArray[i].last + '+AND+schoolid_s%3A1262&defType=edismax&qf=teacherfirstname_t%5E2000+teacherlastname_t%5E2000+teacherfullname_t%5E2000+autosuggest&bf=pow(total_number_of_ratings_i%2C2.1)&sort=total_number_of_ratings_i+desc&siteName=rmp&rows=20&start=0&fl=pk_id+teacherfirstname_t+teacherlastname_t+total_number_of_ratings_i+averageratingscore_rf+schoolid_s&fq=';
Get(profUrl, function(err, data) {
const pkID = data.response.docs.map(doc => doc.pk_id);
if (pkID[0] == null){
reviewElement.innerText = "Professor not found";
}
var ratingCount = data.response.docs.map(doc => doc.total_number_of_ratings_i);
Get('https://www.ratemyprofessors.com/ShowRatings.jsp?tid='+ pkID[0], function(err, data){
let dom = document.createElement('html');
dom.innerHTML = data;
let elements = Array.from(dom.getElementsByClassName('grade'));
elements.length = 3;
rating = elements[0].innerHTML;
wouldTakeAgain = elements[1].innerHTML;
difficulty = elements[2].innerHTML;
ratingElement.innerText = rating;
ratingElement.appendChild(tooltiptext);
tooltiptext.className = 'tooltiptext';
tooltiptext.innerText = 'Would take again: '+ wouldTakeAgain + ' Level of difficulty: ' +difficulty ;
reviewElement.href = 'https://www.ratemyprofessors.com/ShowRatings.jsp?tid=' + pkID[0];
ratingElement.href = 'https://www.ratemyprofessors.com/ShowRatings.jsp?tid=' + pkID[0];
reviewElement.innerText = ' - ' + ratingCount[0] + ' reviews';
if (rating >= 3.5 && rating <= 5){
ratingElement.style.color = '#b4d235';
} else if (rating >= 2.5 && rating[0] < 3.5){
ratingElement.style.color = '#f7cc20';
} else if (rating >= 1 && rating < 2.5){
ratingElement.style.color = '#df3d5f';
}
if (typeof ratingCount == 'undefined'){
reviewElement.innerText = "Professor not found";
ratingElement.innerText = "Professor not Found";
}
});
});
}
function Get(url, callback) {
var xhr = new XMLHttpRequest();
if (url.indexOf('ratemyprofessors.com') > -1){
xhr.responseType = 'text';
}
else {
xhr.responseType = 'json';
}
xhr.onload = function() {
var status = xhr.status;
if (status === 200) {
callback(null, xhr.response);
} else {
callback(status, xhr.response);
}
};
xhr.open('GET', url, true);
xhr.send();
};
}