This repository has been archived by the owner on Oct 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
449 lines (357 loc) · 12.2 KB
/
content.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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
class Period {
constructor(day, start, end) {
this.day = day;
this.start = Period.parse(start);
this.end = Period.parse(end);
this.length = this.end - this.start;
}
static parse(s) {
const a = s.split(':');
return +a[0] * 60 + +a[1];
}
overlaps(other) {
return (
this.day === other.day &&
Math.max(this.start, other.start) < Math.min(this.end, other.end)
);
}
equals(other) {
return (
this.day === other.day &&
this.start === other.start &&
this.end === other.end
);
}
}
class Lesson {
constructor(unit, activity, period, venue) {
this.unit = unit;
this.activity = activity;
this.period = period;
this.venue = venue;
}
same(other) {
return this.unit === other.unit && this.activity === other.activity;
}
equals(other) {
return (
this.same(other) &&
this.period.equals(other.period) &&
this.venue === other.venue
);
}
}
class Schedule {
constructor(lessons, travel) {
this.lessons = lessons;
this.travel = travel;
this.calculate();
}
calculate() {
// TODO: can be made more concise
this.distance = 0;
const days = {};
this.lessons.forEach((lesson) => {
if (!(lesson.period.day in days)) {
days[lesson.period.day] = {
length: 0,
start: 24,
end: 0,
};
}
days[lesson.period.day].length += lesson.period.length;
if (lesson.period.start < days[lesson.period.day].start) {
days[lesson.period.day].start = lesson.period.start;
}
if (lesson.period.end > days[lesson.period.day].end) {
days[lesson.period.day].end = lesson.period.end;
}
});
this.distance = this.travel * 2 * Object.keys(days).length;
Object.keys(days).forEach((day) => {
this.distance += days[day].end - days[day].start - days[day].length;
});
}
conflicts() {
// TODO: can be made more concise
let conflict = false;
const periods = [];
for (let i = 0; i < this.lessons.length - 1; i += 1) {
for (let j = i + 1; j < this.lessons.length; j += 1) {
periods.push([this.lessons[i].period, this.lessons[j].period]);
}
}
let i = 0;
while (!conflict && i < periods.length) {
conflict = periods[i][0].overlaps(periods[i][1]);
i += 1;
}
return conflict;
}
}
function scrapeDocument() {
const lessons = [];
$('table.unitList tbody tr').each((i, trElement) => {
const tdElements = $(trElement).find('td span[title]');
const unit = $(trElement)
.find('td a[title]')
.text();
const activity = $(tdElements[1]).text();
const day = $(tdElements[3]).text();
const start = $(tdElements[4]).text();
const end = $(tdElements[5]).text();
const venue = $(tdElements[8]).text();
const period = new Period(day, start, end);
const lesson = new Lesson(unit, activity, period, venue);
lessons.push(lesson);
});
return lessons;
}
function lessonFilter(lesson, blockedPeriods) {
// TODO: clean this up
let valid = lesson.venue !== 'NO.VENUE'; // filter out online lectures
// filter out lessons during blocked periods
let i = 0;
while (valid && i < blockedPeriods.length) {
valid = !lesson.period.overlaps(blockedPeriods[i]);
i += 1;
}
return valid;
}
function findBestSchedules(lessons, travelTime) {
// TODO: rename function, separate and decouple
const groups = [];
lessons.forEach((lesson) => {
let found = false;
let i = 0;
while (!found && i < groups.length) {
found = groups[i][0].same(lesson);
i += 1;
}
if (found) {
groups[i - 1].push(lesson);
} else {
groups.push([lesson]);
}
});
// create all permuations
// https://stackoverflow.com/questions/12303989/cartesian-product-of-multiple-arrays-in-javascript/50631472#50631472
const permutations = groups.reduce((acc, curr) =>
acc.flatMap((c) => curr.map((n) => [].concat(c, n)))
);
const schedules = [];
permutations.forEach((permutation) => {
const schedule = new Schedule(permutation, travelTime);
if (!schedule.conflicts()) {
schedules.push(schedule);
}
});
return schedules;
}
function cleanPage() {
$('.instructions').remove();
$('.domTT').remove();
}
function createTravelTimeElement() {
$(`<div id="travelTimeContainer" style="padding: 20px 40px; background-color: #eee; border-radius: 5px; margin: 10px">
<span style="font-size: 18px">How long does it take you to travel to university?</span>
<div style="padding: 20px">
<label for="travelTime">Travel Time: </label>
<input type="number" id="travelTime">
<span> minutes</span>
<button type="button" id="updateTravelTime">Update</button>
</div>
</div>`).insertAfter('#timetable_gridbyunit_legend');
}
function createBlockedPeriodsElement() {
$(`<div id="blockedPeriodsContainer" style="padding: 20px 40px; background-color: #eee; border-radius: 5px; margin: 10px">
<span style="font-size: 18px">Which times would you like to block?</span>
<div>
<label for="blockedPeriodDay">Day: </label>
<select id="blockedPeriodDay">
<option value="Monday">Monday</option>
<option value="Tuesday">Tuesday</option>
<option value="Wednesday">Wednesday</option>
<option value="Thursday">Thursday</option>
<option value="Friday">Friday</option>
<option value="Saturday">Saturday</option>
<option value="Sunday">Sunday</option>
</select>
<label for="blockedPeriodStart">Start: </label>
<input type="time" id="blockedPeriodStart">
<label for="blockedPeriodEnd">End: </label>
<input type="time" id="blockedPeriodEnd">
<button type="button" id="addBlockedPeriod">Add</button>
<ul id="blockedPeriodsList" style="font-size: 14px;">
</ul>
</div>
</div>`).insertAfter('#timetable_gridbyunit_legend');
}
function createSchedulesElement() {
$(
`<ul id="schedules" style="">
</ul>`
).insertBefore('#timetableByUnits\\:timetable_gridbyunit');
}
function calculateSchedules(blockedPeriods, travelTime) {
let lessons = scrapeDocument();
lessons = lessons.filter((lesson) => lessonFilter(lesson, blockedPeriods));
const schedules = findBestSchedules(lessons, travelTime);
const sorted = schedules.sort((a, b) => a.distance - b.distance);
let i = 10;
let same = true;
while (same && i < sorted.length) {
same = sorted[i].distance === sorted[9].distance;
i += 1;
}
const chosen = sorted.slice(0, i - 1);
console.log(chosen);
console.log(sorted);
return chosen;
}
function updateTimetable(schedule) {
$('table.unitList input[type="checkbox"]').each((i, checkbox) => {
$(checkbox).prop('checked', false);
});
// check lessons from schedule
// TODO: clean this up
$('table.unitList tbody tr').each((i, trElement) => {
const tdElements = $(trElement).find('td span[title]');
const unit = $(trElement)
.find('td a[title]')
.text();
const activity = $(tdElements[1]).text();
const day = $(tdElements[3]).text();
const start = $(tdElements[4]).text();
const end = $(tdElements[5]).text();
const venue = $(tdElements[8]).text();
const period = new Period(day, start, end);
const lesson = new Lesson(unit, activity, period, venue);
let j = 0;
const found = false;
while (!found && j < schedule.lessons.length) {
if (schedule.lessons[j].equals(lesson)) {
$(trElement)
.find('input')
.prop('checked', true);
}
j += 1;
}
});
// click 'Update Timetable' button
$('input.formSubmit').click();
// scroll to top of schedules
$(document).scrollTop(
$('#timetableByUnits\\:timetable_gridbyunit').offset().top
);
}
function updateSchedulesList(schedules) {
// remove current schedules
$('#schedules li').remove();
// add each schedule to list
schedules.forEach((schedule, index) => {
$('#schedules').append(
$(`<li id="${index}" style="list-style-type: none; text-align: center; padding: 20px 40px; float: left; border-radius: 3px;">
<span><strong>Schedule #${index}</strong></span><br>
<span>Distance: ${schedule.distance}</span>
</li>`)
);
});
// update schedule styling
$('#scheudles li')
.css('float', 'left')
.css('list-style-type', 'none')
.css('width', '20%')
.css('margin', '0')
.css('text-align', 'center')
.css('background-color', 'skyblue')
.css('padding', '10px 0');
// select best schedule
$('#schedules li#0').click();
}
function createInformationElement() {
$(`<div id="travelTimeContainer" style="padding: 20px 40px; background-color: #eee; border-radius: 5px; margin: 10px">
<span style="font-size: 18px">What is Curtin Final Distance?</span>
<div style="padding: 20px">
<span style="font-size: 14px">Curtin Final Distance is a web extension built to close the gaps in your university schedule. It includes the ability to factor in your commute time and block off certain times of the week, useful to ensure you don't schedule classes during work or other commitments. It creates every possible combinations of classes and lets you choose from the ten (or more) schedules with the lowest distance. Distance is a score given to a schedule that is calculated by adding your commute time multiplied by two for every day you must attend university that week, plus the minutes between each class on a day.</span>
</div>
</div>`).insertAfter('#timetable_gridbyunit_legend');
}
function f() {
let schedules = [];
let travelTime = 60;
const blockedPeriods = [];
// remove unecessary elements from page
cleanPage();
// create blocked periods element
createBlockedPeriodsElement();
// create travel time element
createTravelTimeElement();
// create info element
createInformationElement();
// create schedules element
createSchedulesElement();
// create travel time update listener
$('#updateTravelTime').on('click', () => {
travelTime = +$('#travelTime').val();
schedules = calculateSchedules(blockedPeriods, travelTime);
updateSchedulesList(schedules);
});
// create add blocked period listener
$('#addBlockedPeriod').on('click', () => {
const day = $('#blockedPeriodDay').val();
const start = $('#blockedPeriodStart').val();
const end = $('#blockedPeriodEnd').val();
const period = new Period(day, start, end);
blockedPeriods.push(period);
schedules = calculateSchedules(blockedPeriods, travelTime);
updateSchedulesList(schedules);
$('#blockedPeriodsList').append(
$(
`<li data-day="${day}" data-start="${start}" data-end="${end}" style="border-radius: 3px">${day}, ${start} to ${end}</li>`
)
);
});
// create blocked period remove listener
$('#blockedPeriodsList').on('click', 'li', (e) => {
const day = $(e.target).attr('data-day');
const start = $(e.target).attr('data-start');
const end = $(e.target).attr('data-end');
const period = new Period(day, start, end);
blockedPeriods.forEach((current) => {
if (period.equals(current)) {
blockedPeriods.splice(blockedPeriods.indexOf(current), 1);
}
});
$(e.target).remove();
schedules = calculateSchedules(blockedPeriods, travelTime);
updateSchedulesList(schedules);
});
// create blocked period hover listener
$('#blockedPeriodsList').on('mouseenter', 'li', (e) => {
$(e.target).css('background-color', '#eb6746');
});
// create blocked period un-hover listener
$('#blockedPeriodsList').on('mouseleave', 'li', (e) => {
$(e.target).css('background-color', '#eee');
});
// add schedule listener
$('#schedules').on('click', 'li', (e) => {
cleanPage();
$('#schedules li').css('background-color', '#eee');
let li;
if (e.target.tagName === 'LI') {
li = $(e.target);
} else {
li = $(e.target).parents('li');
}
const id = li.attr('id');
updateTimetable(schedules[id]);
li.css('background-color', '#2fc6e0');
});
// update travel time input value
$('#travelTime').val(travelTime);
schedules = calculateSchedules(blockedPeriods, travelTime);
updateSchedulesList(schedules);
}
f();