-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
281 lines (254 loc) · 12.7 KB
/
main.js
File metadata and controls
281 lines (254 loc) · 12.7 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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
function termFind() {
//getting season and year to calculate the term number
var term = document.getElementsByClassName("PABOLDTEXT")[0].innerText.split("|")[0].split(" ")
var season = term[0]
var year = term[1]
//using the following as a base:
//Winter 2018 is 1181
//Spring 2018 is 1185
//Fall 2018 is 1189
//Winter 2019 is 1191
var seasonTerm = {Winter: 1181, Spring: 1185, Fall: 1189}
var multiplier = year - 2018
//this should be the pattern, but not 100%
return seasonTerm[season] + 10 * multiplier
}
function getReq(url){
//GET request
var xhr = new XMLHttpRequest()
xhr.open("GET", url, false)
xhr.send()
return xhr.responseText
}
function amPmChecker(time, start = false){
//since site doesn't list in 24h and doesn't specify AM or PM, have to make a guess
//earliest class is at 0830 so any times between 0000 and 0829 should be in PM
//likewise, if the class starts in pm, will end in pm; so a class starting at "0630" and ending at "0920"
//should actually be 1830 and 2120, even though 0830 < 0920 in this case
if(start || time < 830){
return parseInt(time) + 1200
} else {
return time
}
}
function dayExtracter(days){
//converting days of the week to proper iCal format
var daysArray = []
if (days.includes("M")){
daysArray.push("MO")
}
if (days.includes("W")){
daysArray.push("WE")
}
if (days.includes("F")){
daysArray.push("FR")
}
//luckily doesn't have to be in order to work
if (days.includes("Th")){
daysArray.push("TH")
//to make it easy for Tuesday searching "T"
days = days.replace("Th","")
}
if (days.includes("T")){
daysArray.push("TU")
}
return daysArray.join(",")
}
function constructDateTime(date, time){
//getting the iCal date and time format
return date.getFullYear() + ("0" + (date.getMonth() + 1)).slice(-2) + ("0" + date.getDate()).slice(-2) + "T" + time + "00"
}
function download(filename, text) {
var element = document.createElement('a')
element.setAttribute("href", "data:text/plain;charset=utf-8," + encodeURIComponent(text))
element.setAttribute("download", filename)
element.style.display = "none"
document.body.appendChild(element)
element.click()
document.body.removeChild(element)
}
function icalCreate(){
var term = termFind()
var counter = 0
//look for the checked boxes only
var checkbox = document.getElementsByClassName("tickerbox")
var ical = "BEGIN:VCALENDAR\n" +
"PRODID:-//gitCommitWiL//UW Alter Schedule Creator//EN\n" +
"VERSION:2.0\n"
var checkbox = document.getElementsByClassName("tickerbox")
//skip all non LEC and TUT
var skipSec = 0
//count the number of TUT (used later on to properly search for TUT sections)
var tutCount = 0
//look for the checked boxes only
for (var i = 0; i < checkbox.length; i++){
if(!checkbox[i].checked){
continue
}
//have to get the section number of the lec to prevent overlap, get start/ end date as well to use for later
//so I'm not entirely sure about this, but I think the first one should be the LEC; I think TUT and TST come later
//so I saw one case where TST is actually the first row, so need to change that
//below usually runs once, but not sure if there are any cases out there that need this loop
while(true){
//in addtion to check for LEC andc TUT, also check that the box ID matches up (i.e. skip unchecked boxes)
if((document.getElementById("MTG_COMP$" + (i + skipSec)).innerText != "LEC" && document.getElementById("MTG_COMP$" + (i + skipSec)).innerText != "TUT") || !( document.getElementById("MTG_COMP$" + (i + skipSec)).innerHTML.includes("checkbox" + i))){
skipSec+=1
} else {
break
}
}
//there's a space after the section #, as per the site
var origSection = document.getElementById("MTG_COMP$" + (i + skipSec)).innerText + " " + document.getElementById("MTG_SECTION$" + (i + skipSec)).innerText + " "
var startEndDate = document.getElementById("MTG_DATES$" + (i + skipSec)).innerText.split(" - ")
var startDate = startEndDate[0].split("/")
//var startDateProper = startDate[2] + startDate[0] + startDate[1]
//did you know that Javascript has its months zero indexed? I didn't know that. Until after an hour of pulling my hair out. WHY WOULD YOU DO THIS
var startD = new Date(startDate[2], startDate[0] - 1, startDate[1])
//starting date is 1 day before actual start date; if starting day is a T but class is every MWF this makes it easier to exclude that T so start on W
startD.setDate(startD.getDate() - 1 )
var endDate = startEndDate[1].split("/")
var endD = new Date(endDate[2], endDate[0] - 1, endDate[1])
//similar logic with end date
endD.setDate(endD.getDate() + 1 )
//ignore online "lectures"
//there's a space after the section #, as per the site
var lecSecs = {'LEC 081 ': true}
lecSecs[origSection] = true
var classes = document.getElementsByClassName("PAGROUPDIVIDER")
//if include tutorial, it will mess up the i count sometimes
//here's a messy way to deal with it, assuming lectures are always listed before tutorials
if(checkbox[i].parentNode.innerText == "TUT"){
tutCount += 1
}
var j = i - tutCount
var text = classes[j].innerText
text = text.split("-")
var course = text[0].split(" ")
var description = text[1]
var subject = course[0]
var cournum = course[1]
var url = "https://info.uwaterloo.ca/cgi-bin/cgiwrap/infocour/salook.pl?sess=" + term + "&level=under&subject=" + subject + "&cournum=" + cournum
var resp = getReq(url)
//turn into html object so can grab required info
var htmlObject = document.createElement("div")
htmlObject.innerHTML = resp;
//everything is in a simple table so not much to use; get rows for the info
var tr = htmlObject.getElementsByTagName("tr")
//for each TR search to see if childrenElementCount is 13 AND see if innerText contains LEC AND Comp Sec is not in lecSecs (this prevents curious cases like CS 245 and CS 245E; they share the same Comp Sec, most of the time)
for (var j = 0; j < tr.length; j++){
//13 is for full list, but sometimes instructor is missing for TUT, so in that case 12 is acceptable
if(((tr[j].childElementCount == 13 && (document.getElementById("MTG_COMP$" + (i + skipSec)).innerText == "LEC" || document.getElementById("MTG_COMP$" + (i + skipSec)).innerText == "TUT")) || (tr[j].childElementCount == 12 && document.getElementById("MTG_COMP$" + (i + skipSec)).innerText == "TUT")) && tr[j].innerText.includes(document.getElementById("MTG_COMP$" + (i + skipSec)).innerText) && lecSecs[tr[j].children[1].innerText] == null){
var classNum = tr[j].children[0].innerText
var compSec = tr[j].children[1].innerText
var campLoc = tr[j].children[2].innerText
var date = tr[j].children[10].innerText.split("\n")[0].split("-")
//splitting on "\n" will get rid of those Laurier clasess that have both the start/end date and times
//if TBA date, skip as not useful
if(date == "TBA" || date == ""){
continue
}
var startTime = date[0].replace(":","")
startTime = amPmChecker(startTime)
var endTime = date[1].substring(0,5).replace(":","")
var startBool = false
//if start is in PM, need to add 1200 to end, in particular for classes that start at 6:30pm
if (startTime > 1200){
startBool = true
}
endTime = amPmChecker(endTime, startBool)
var days = date[1].substring(5)
//for whatever reason, times are not listed in 24 hours; so to prevent an AM/ PM mixup
//earliest class should be 8:30 AM, so any number less than 0830, add 1200 to it
var classDays = dayExtracter(days)
var room = tr[j].children[11].innerText
//if TBA room, skip as not useful
if(room== "TBA" || room == ""){
continue
}
if (tr[j].children[12] != null){
var instructor = tr[j].children[12].innerText
} else {
var instructor = "TBA"
}
//add section so that (hopefully) won't run into CS 245 and CS 245E case
lecSecs[tr[j].children[1].innerText] = true
//construct ical event entry
var iCalEvent = "BEGIN:VEVENT\n" +
"DESCRIPTION:" +
"Section: " + compSec + "\\n" +
"Instructor: " + instructor + "\\n" +
"Class Number: " + classNum + "\\n\n" +
"DTEND;TZID=America/Toronto:" + constructDateTime(startD, endTime) + "\n" +
"DTSTART;TZID=America/Toronto:" + constructDateTime(startD, startTime) + "\n" +
"LOCATION:" + room + "\n" +
"RRULE:FREQ=WEEKLY;UNTIL=" + constructDateTime(endD, endTime) + "Z;BYDAY=" + classDays + "\n" +
"EXDATE;TZID=America/Toronto:" + constructDateTime(startD, startTime) + "\n" +
"SUMMARY:" + subject + " " + cournum + " - " + description + " (" + document.getElementById("MTG_COMP$" + (i + skipSec)).innerText + ")" + "\n" +
"TRANSP:TRANSPARENT\n" +
"END:VEVENT\n"
ical += iCalEvent
counter++
}
}
}
ical += "END:VCALENDAR\n"
console.log(ical)
if (counter == 0){
alert("No additional sections found")
} else {
/*if (counter == 1) {
alert("Found 1 other lecture!")
} else {
alert("Found " + counter + " other lectures!")
}*/
var term = document.getElementsByClassName("PABOLDTEXT")[0].innerText.split("|")[0].split(" ").join("")
var name = document.getElementById("DERIVED_SSTSNAV_PERSON_NAME").innerText.replace(" ","")
var file = name + term + "AlterSchedule.ics"
download(file, ical)
}
}
var intervalCheck
function check(){
//stop the interval checks
clearInterval(intervalCheck)
var title = document.getElementsByClassName("PATRANSACTIONTITLE")[0]
//check if on right page
if(document.getElementById("DERIVED_SSS_SCT_SSR_PB_GO") == null && title != null && title.innerText == "My Class Schedule" && document.getElementsByClassName("PSRADIOBUTTON")[0].checked){
var classes = document.getElementsByClassName("PAGROUPDIVIDER")
var checkboxId = 0
//add checkboxes
for (var k = 0; k < classes.length * 2; k++){
if (document.getElementById("MTG_COMP$" + k) != null)
if (document.getElementById("MTG_COMP$" + k).innerText == "LEC"){
document.getElementById("MTG_COMP$" + k).innerHTML += "<input type='checkbox' checked='true' class='tickerbox' id='checkbox" + checkboxId + "'>"
checkboxId += 1
} else if (document.getElementById("MTG_COMP$" + k).innerText == "TUT"){
//by default, TUT are unchecked
document.getElementById("MTG_COMP$" + k).innerHTML += "<input type='checkbox' unchecked='true' class='tickerbox' id='checkbox" + checkboxId + "'>"
checkboxId += 1
}
}
//add hyperlink
title.innerHTML += " (<a id='dLSched' href='#' >Download Alter Schedule [uncheck component to exclude]</a>)"
var sched = document.getElementById("dLSched")
sched.addEventListener('click', function(){
icalCreate()
})
} else{
//this is only for "Select a term" page. Clicking continue after selecting a term was being a little weird
//the solution I came up for it was just to cheeck every 1.5 seconds to see if a term was selected
if (title != null && title.innerText == "My Class Schedule" && document.getElementById("DERIVED_SSS_SCT_SSR_PB_GO") != null) {
interCheck()
}
}
}
function interCheck(){
intervalCheck = setInterval(check, 1500)
}
if (document.readyState !== 'loading'){
check()
} else {
document.addEventListener("DOMContentLoaded", function(){
check()
}, false)
}