-
Notifications
You must be signed in to change notification settings - Fork 0
/
content_script.js
162 lines (141 loc) · 6.99 KB
/
content_script.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
157
158
159
160
161
const url = chrome.runtime.getURL('ProfTids.txt');
fetch(url)
.then((response) => response.json())
.then((profJson) => {
let inlineOverallSortButton = createButton("Sort by Overall Rating", "inline");
let inlineDiffSortButton = createButton("Sort by Difficulty Rating", "inline");
let outlineOverallSortButton = createButton("Sort by Overall Rating", "outline");
let outlineDiffSortButton = createButton("Sort by Difficulty Rating", "outline");
addInlineSortButtonToView(inlineOverallSortButton);
addInlineSortButtonToView(inlineDiffSortButton);
addOutlineSortButton(outlineOverallSortButton);
addOutlineSortButton(outlineDiffSortButton);
inlineOverallSortButton.onclick = () => {
sortByOverallRating(profJson, inlineOverallSortButton)
}
inlineDiffSortButton.onclick = () => {
sortByDiffRating(profJson, inlineDiffSortButton)
}
outlineOverallSortButton.onclick = () => {
sortByOverallRating(profJson, outlineOverallSortButton);
}
outlineDiffSortButton.onclick = () => {
sortByDiffRating(profJson, outlineDiffSortButton)
}
});
function createButton(buttonInnerText, buttonId){
var sortRatingButton = document.createElement('BUTTON');
sortRatingButton.id = buttonId;
sortRatingButton.innerText = buttonInnerText;
sortRatingButton.style.marginRight = "8px";
return sortRatingButton;
}
function addInlineSortButtonToView(inlineOverallSortButton) {
let inlineSearchBar = document.getElementsByClassName("course-search-crn-title-container")[0];
inlineSearchBar.appendChild(inlineOverallSortButton);
}
function addOutlineSortButton(outlineOverallSortButton) {
let outlineSearchBar = document.getElementById("CoursesSearch").getElementsByClassName("modal-body")[0].getElementsByClassName("course-search-container")[0].getElementsByClassName("align-center")[0];
outlineSearchBar.appendChild(outlineOverallSortButton)
}
function sortByOverallRating(profJson, sortButton) {
sortButton.innerText = "Loading...";
let listOfProfDivs = document.getElementsByClassName("results-instructor");
let profRatings = []
for(i = 0; i < listOfProfDivs.length; i++) {
let profName = listOfProfDivs[i].getElementsByTagName("a")[0].innerHTML;
let profTid = profJson[profName.replace(". ", "_")];
const indexOfProf = i;
profRatings.push(new Promise((resolve, reject) => {
chrome.runtime.sendMessage({tid: "" + profTid}, async function(response) {
var parser = new DOMParser();
var doc = parser.parseFromString(response.returned_text, "text/html");
var overallRatingDiv = doc.getElementsByClassName("RatingValue__Numerator-qw8sqy-2 liyUjw")[0];
var diffRatingDiv = doc.getElementsByClassName("FeedbackItem__FeedbackNumber-uof32n-1 kkESWs")[1];
let overallRating = undefined;
let diffRating = undefined;
if(typeof overallRatingDiv != 'undefined' && overallRatingDiv.innerHTML != 'N/A'){
overallRating = overallRatingDiv.innerHTML;
} else {
overallRating = -1;
}
if(typeof diffRatingDiv != 'undefined' && diffRatingDiv.innerHTML != 'N/A'){
diffRating = diffRatingDiv.innerHTML;
} else {
diffRating = -1;
}
resolve([indexOfProf, overallRating, diffRating]);
});
}));
}
let overallRatings = [];
Promise.all(profRatings).then((profRatings) => {
for(let i = 0; i < profRatings.length; i++) {
overallRatings.push({"OrigIndex": profRatings[i][0], "OverallRating" : profRatings[i][1]});
}
overallRatings.sort((firstProf, secondProf) => {
if(firstProf["OverallRating"] < secondProf["OverallRating"]) {
return 1;
} else {
return -1;
}
});
sortButton.innerText = "Sort By Overall Rating";
let oldCourseOrder = document.getElementsByClassName("course-container");
let newSortedProfsHtml = []
for(let classIndex = 0; classIndex < oldCourseOrder.length; classIndex++){
console.log("For " + classIndex + ", the overall ratings: " + overallRatings[classIndex]["OrigIndex"] + ", " + overallRatings[classIndex]["OverallRating"]);
newSortedProfsHtml.push(oldCourseOrder[overallRatings[classIndex]["OrigIndex"]].innerHTML);
}
for(let classIndex = 0; classIndex < oldCourseOrder.length; classIndex++){
oldCourseOrder[classIndex].innerHTML = newSortedProfsHtml[classIndex];
}
});
}
function sortByDiffRating(profJson, sortButton) {
sortButton.innerText = "Loading...";
let listOfProfDivs = document.getElementsByClassName("results-instructor");
let profRatings = []
for(i = 0; i < listOfProfDivs.length; i++) {
let profName = listOfProfDivs[i].getElementsByTagName("a")[0].innerHTML;
let profTid = profJson[profName.replace(". ", "_")];
const indexOfProf = i;
profRatings.push(new Promise((resolve, reject) => {
chrome.runtime.sendMessage({tid: "" + profTid}, async function(response) {
var parser = new DOMParser();
var doc = parser.parseFromString(response.returned_text, "text/html");
var diffRatingDiv = doc.getElementsByClassName("FeedbackItem__FeedbackNumber-uof32n-1 kkESWs")[1];
let diffRating = undefined;
if(typeof diffRatingDiv != 'undefined' && diffRatingDiv.innerHTML != 'N/A'){
diffRating = diffRatingDiv.innerHTML;
} else {
diffRating = 6;
}
resolve([indexOfProf, diffRating]);
});
}));
}
let diffRatings = [];
Promise.all(profRatings).then((profRatings) => {
for(let i = 0; i < profRatings.length; i++) {
diffRatings.push({"OrigIndex": profRatings[i][0], "DiffRating" : profRatings[i][1]});
}
diffRatings.sort((firstProf, secondProf) => {
if(firstProf["DiffRating"] > secondProf["DiffRating"]) {
return 1;
} else {
return -1;
}
});
sortButton.innerText = "Sort By Difficulty Rating";
let oldCourseOrder = document.getElementsByClassName("course-container");
let newSortedProfsHtml = []
for(let classIndex = 0; classIndex < oldCourseOrder.length; classIndex++){
console.log("For " + classIndex + ", the diff ratings: " + diffRatings[classIndex]["OrigIndex"] + ", " + diffRatings[classIndex]["DiffRating"]);
newSortedProfsHtml.push(oldCourseOrder[diffRatings[classIndex]["OrigIndex"]].innerHTML);
}
for(let classIndex = 0; classIndex < oldCourseOrder.length; classIndex++){
oldCourseOrder[classIndex].innerHTML = newSortedProfsHtml[classIndex];
}
});
}