-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcbt.js
171 lines (154 loc) · 6.12 KB
/
cbt.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
162
163
164
165
166
167
168
169
170
171
// an interval and counter for detecting if the user is on the page that shows the calendar
let findCalendarPageInterval = setInterval(findCalendarPage, 2000);
let findCalendarPageCount = 0;
// a interval that detects whether the user has opened the calendar or not
let listenForCalendarInterval = null;
let calendarOpen = false;
// the courses that the user is registered in, loaded dynamically
let courseData = null;
// detect if the user is on the page that shows the calendar
function findCalendarPage() {
const bodyText = document.body.innerText;
const regex = /(View My Courses|View as Course Calendar)/;
if (bodyText.match(regex) != null) {
listenForCalendarInterval = setInterval(listenForCalendar, 2000);
clearInterval(findCalendarPageInterval);
}
if (findCalendarPageCount >= 100) {
clearInterval(findCalendarPageInterval); // stop after a while
}
findCalendarPageCount++;
}
// detect if the calendar is opened
function listenForCalendar() {
const bodyText = document.body.innerHTML;
const regex = />View as Course Calendar</; // angle brackets because text on its own is in the html when calendar not open
const textMatched = bodyText.match(regex) != null
if (textMatched && !calendarOpen) {
calendarOpen = true;
injectButtons();
parseCourseData();
} else if (!textMatched && calendarOpen) {
calendarOpen = false;
}
}
function injectButtons() {
const calendarToolbar = document.querySelector("[data-automation-id='calendarToolbar']")
const extensionToolbar = `
<div class='cbt-toolbar'>
<a role='button' id='cbt-allButton' class='cbt-button cbt-active'>All</a>
<a role='button' id='cbt-termOneButton' class='cbt-button'>Term 1</a>
<a role='button' id='cbt-termTwoButton' class='cbt-button'>Term 2</a>
</div>
`;
if (calendarToolbar) {
calendarToolbar.innerHTML += extensionToolbar;
const allButton = document.getElementById("cbt-allButton");
const termOneButton = document.getElementById("cbt-termOneButton");
const termTwoButton = document.getElementById("cbt-termTwoButton");
allButton.addEventListener("click", function() {
resetAll();
allButton.classList.add("cbt-active");
termOneButton.classList.remove("cbt-active");
termTwoButton.classList.remove("cbt-active");
});
termOneButton.addEventListener("click", function() {
chooseTermOne();
termOneButton.classList.add("cbt-active");
allButton.classList.remove("cbt-active");
termTwoButton.classList.remove("cbt-active");
});
termTwoButton.addEventListener("click", function() {
chooseTermTwo();
termTwoButton.classList.add("cbt-active");
allButton.classList.remove("cbt-active");
termOneButton.classList.remove("cbt-active");
});
}
}
// reset calendar back to workday default
function resetAll() {
console.log("resetAll");
getAllCalendarCourses().forEach((elm) => {
elm.classList.remove("cbt-hidden");
elm.parentElement.parentElement.classList.remove("cbt-forceFullWidth");
elm.parentElement.parentElement.classList.remove("cbt-forcePosition");
})
}
function chooseTermOne() {
resetAll();
getAllCalendarCourses().forEach((elm) => {
const sectionTitle = elm.innerText.split("\n")[0];
const sectionData = findSection(sectionTitle);
if (!(sectionData.startMonth === 9 || sectionData.startMonth === 5)) { // if the course doesnt start on January or May, hide it
elm.classList.add("cbt-hidden");
} else {
elm.parentElement.parentElement.classList.add("cbt-forceFullWidth");
if (isCourseElementOffset(elm)) {
elm.parentElement.parentElement.classList.add("cbt-forcePosition");
}
}
})
}
function chooseTermTwo() {
resetAll();
getAllCalendarCourses().forEach((elm) => {
const sectionTitle = elm.innerText.split("\n")[0];
const sectionData = findSection(sectionTitle);
if (!(sectionData.endMonth === 4 || sectionData.endMonth === 8)) { // if the course doesnt end on August or April, hide it
elm.classList.add("cbt-hidden");
} else {
elm.parentElement.parentElement.classList.add("cbt-forceFullWidth");
if (isCourseElementOffset(elm)) {
elm.parentElement.parentElement.classList.add("cbt-forcePosition");
}
}
})
}
function getAllCalendarCourses() {
return document.querySelectorAll(".WLSC.WMSC.WMUC.WMVC"); // workday puts these classes for courses in the calendar
}
function findSection(section) {
const found = courseData.filter((course) => course.section === section);
return found[0];
}
// some elements are offset if the course times overlap across terms, detect them here and use the forcePosition css class
function isCourseElementOffset(elm) {
const conditions = ["7.", "21.", "35.", "50", "64.", "78.", "92."];
return conditions.some((cond) => elm.parentElement.parentElement.style.left.startsWith(cond));
}
// fetch user course data from the html table
function parseCourseData() {
const courseTables = document.querySelectorAll("[data-automation-id='table']");
let courses = []
courseTables.forEach((table) => {
courses = courses.concat(tableToJson(table));
})
courseData = transformCourseJson(courses);
}
// only extract needed course info from the full json
function transformCourseJson(cousesJson) {
const transformedCourses = cousesJson.map((course) => {
let newCourseObj = {
section: course.section.split(" - ")[0], // remove unneccessary info
startMonth: new Date(Date.parse(course.startDate)).getMonth() + 1, // January is 1 here
endMonth: new Date(Date.parse(course.endDate)).getMonth() + 1
};
return newCourseObj;
});
return transformedCourses;
}
// convert the html table to json format for data access
function tableToJson(table) {
var data = [];
var headers = ["searchbar", "title", "credits", "grading", "section", "format", "delivery", "pattern", "status", "instructor", "startDate", "endDate"];
for (var i=2; i<table.rows.length; i++) {
var tableRow = table.rows[i];
var rowData = {};
for (var j=0; j<tableRow.cells.length; j++) {
rowData[ headers[j] ] = tableRow.cells[j].innerText;
}
data.push(rowData);
}
return data;
}