-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvutt.js
188 lines (165 loc) · 5.53 KB
/
vutt.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
var parseTT = function() {
var plusAWeek = function(date, count) {
let newDate = date.valueOf() + count * 604800000;
newDate = new Date(newDate);
return(newDate);
}
var range = function(begin, end) {
let numbers = [];
for (let i = begin; i <= end; i++) {
numbers.push(i);
}
return(numbers);
}
var parseRange = function(rangeString) {
let [begin, end] = rangeString.split("-");
begin = parseInt(begin);
end = parseInt(end);
if (isNaN(end)) {
return([begin]);
} else {
return(range(begin, end));
}
}
var parseRanges = function(rangeString) {
let ranges = rangeString.split(",");
let thisYear = [];
let nextYear = [];
for (let i = 0; i < ranges.length; i++) {
let numbers = parseRange(ranges[i]);
if (i == 0 || numbers[0] > thisYear[0])
thisYear = thisYear.concat(numbers);
else
nextYear = nextYear.concat(numbers);
}
return([thisYear, nextYear]);
}
var icsFormatter = function() {
'use strict';
var SEP = '\r\n';
var CALSEP = '\\n';
var calendarEvents = [];
var calendarStart = [
'BEGIN:VCALENDAR',
'PRODID:-//Mahmood Shafeie Zargar//VU🐓//EN',
'VERSION:2.0',
'BEGIN:VTIMEZONE',
'TZID:Europe/Amsterdam',
'BEGIN:DAYLIGHT',
'TZOFFSETFROM:+0100',
'RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU',
'DTSTART:19810329T020000',
'TZNAME:GMT+2',
'TZOFFSETTO:+0200',
'END:DAYLIGHT',
'BEGIN:STANDARD',
'TZOFFSETFROM:+0200',
'RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU',
'DTSTART:19961027T030000',
'TZNAME:GMT+1',
'TZOFFSETTO:+0100',
'END:STANDARD',
'END:VTIMEZONE'
].join(SEP);
var calendarEnd = SEP + 'END:VCALENDAR';
return {
'events': function() {
return calendarEvents;
},
'calendar': function() {
return calendarStart + SEP + calendarEvents.join(SEP) + calendarEnd;
},
'addEvent': function(args) {
let [code, date, weeks, begin, end, title, description, type, locations, staff, comment] = args;
title = title + " (" + type + ")";
let [day, month, year] = date.split("/");
day = ('00' + day).substring(day.length);
month = ('00' + month).substring(month.length);
year = '20' + ('00' + year).substring(year.length);
date = year + month + day;
let [bhour, bmin] = begin.split(":");
bhour = ('00' + bhour).substring(bhour.length);
bmin = ('00' + bmin).substring(bmin.length);
begin = date + "T" + bhour + bmin + "00";
let [ehour, emin] = end.split(":");
ehour = ('00' + ehour).substring(ehour.length);
emin = ('00' + emin).substring(emin.length);
end = date + "T" + ehour + emin + "00";
let nextyear = (Number.parseInt(year) + 1).toString();
let until = nextyear + "01" + "01" + "T" + "00" + "00" + "00";
description = [
code == "" ? "" : "Code: " + code,
staff == "" ? "" : CALSEP + "Staff: " + staff,
description == "" ? "" : CALSEP + "Description: " + description,
comment == "" ? "" : CALSEP + "Comment: " + comment
].join("");
let stamp = new Date();
stamp = stamp.toISOString().replace(/([-:]|\.\d{3})/g, "");
weeks = weeks.replace(/\s/g, "");
var weekRanges = parseRanges(weeks);
for (let i = 0 ; i < weekRanges.length; i++) {
if (weekRanges[i].length > 0) {
var calendarEvent = [
'BEGIN:VEVENT',
'DTSTAMP:' + stamp,
'DTEND;TZID=Europe/Amsterdam:' + end,
'LOCATION:' + locations,
'DESCRIPTION:' + description,
'SUMMARY:' + title,
'DTSTART;TZID=Europe/Amsterdam:' + begin,
(weekRanges[i].length == 1 ? "" : 'RRULE:FREQ=YEARLY;BYWEEKNO=' + weekRanges[i].toString() + ';UNTIL=' + until + SEP) + 'END:VEVENT'
].join(SEP);
calendarEvents.push(calendarEvent);
}
}
return calendarEvent;
},
'download': function(filename) {
if (calendarEvents.length < 1) {
return false;
}
var calendar = calendarStart + SEP + calendarEvents.join(SEP) + calendarEnd;
var blob = new Blob([calendar], {
type: "text/calendar;charset=utf-8;"
});
if (window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveBlob(blob, filename);
} else {
var elem = window.document.createElement('a');
elem.href = window.URL.createObjectURL(blob);
elem.download = filename;
document.body.appendChild(elem);
elem.click();
document.body.removeChild(elem);
window.URL.revokeObjectURL(elem.href);
}
}
}
}
var cal = icsFormatter();
var rows = document.querySelectorAll("table.spreadsheet tr:not(.columnTitles)");
rows.forEach(
function(row) {
var fields = [];
row.querySelectorAll("td").forEach(function(cell) {
fields.push(cell.textContent.replace(/\s+/g, " ").trim());
});
cal.addEvent(fields);
}
);
if (cal.events().length == 0) {
if (window.location.hostname != "rooster.vu.nl") {
alert("VUTt is unable to find the timetable information on the current page, and you don't seem to be on the Rooster website! Note that VU🐓 only works with VU's Rooster web app.");
} else {
alert("Although you seem to be on the Rooster website, VU🐓 can not find any timetable information on the current page! Either there is no timetable on this page, or you have found a bug. Please try once more and if you can't make it work, file a bug report.");
}
} else {
var calname = document.querySelector("span.header-2-0-1");
calname = (calname != null) ? calname.textContent.replace(/[^A-Za-z0-9]/g, " ").trim().replace(/\s+/g, "_").toLowerCase() : "";
if (calname.length > 0) {
cal.download(calname + ".ics");
} else {
cal.download("calendar.ics");
}
}
};