-
Notifications
You must be signed in to change notification settings - Fork 1
/
videoserver.user.js
executable file
·102 lines (80 loc) · 3.31 KB
/
videoserver.user.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
// ==UserScript==
// @name VUT FIT video server addon
// @namespace https://github.com/KrKOo/VUT-FIT-Video-server-extension/
// @version 1.0.4
// @description VUT FIT video server addon for showing lecture titles and week numbering
// @author KrKOo & Kiznoh
// @match https://video1.fit.vutbr.cz/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=video1.fit.vutbr.cz
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Returns the ISO week of the date.
Date.prototype.getWeek = function () {
var date = new Date(this.getTime());
date.setHours(0, 0, 0, 0);
// Thursday in current week decides the year.
date.setDate(date.getDate() + 3 - (date.getDay() + 6) % 7);
// January 4 is always in week 1.
var week1 = new Date(date.getFullYear(), 0, 4);
// Adjust to Thursday in week 1 and count number of weeks from date to week1.
return 1 + Math.round(((date.getTime() - week1.getTime()) / 86400000
- 3 + (week1.getDay() + 6) % 7) / 7);
}
const getLectureTitles = (course) => {
return fetch(`https://lesson-titles-videoserver-fit-vut.cunt.cz/lecture-titles/${course}`)
.then(response => response.json())
.catch(e => {
console.error(e);
})
}
const getDateFromLectureString = (str) => {
const regex = /,\s([0-9]+).\s([0-9]+).\s([0-9]+)/mg;
let m;
let result = [];
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
result.push(match);
});
}
return result.slice(1, 4);
}
const insertLectureNumbering = async (course) => {
const lectureTitles = await getLectureTitles(course) || [];
// get the last <ul> on the page
const lectureList = Array.from(document.querySelectorAll("ul")).pop();
const lectures = lectureList.children;
let prevWeek = -1;
let firstSchoolWeek = -1;
Array.from(lectures).forEach(lecture => {
const lectureText = lecture.innerText;
const splitDate = getDateFromLectureString(lectureText);
// (Y, M - 1, D) - JS counts months from 0
const date = new Date(splitDate[2], splitDate[1] - 1, splitDate[0]);
const week = date.getWeek();
if (firstSchoolWeek === -1) {
firstSchoolWeek = week;
};
const schoolWeek = week - firstSchoolWeek + 1;
if (prevWeek !== week && !isNaN(week)) {
let titleElement = document.createElement("h2")
titleElement.textContent = "Week " + schoolWeek + ((lectureTitles[schoolWeek - 1]) ? " - " + lectureTitles[schoolWeek - 1] : "");
lecture.parentElement.insertBefore(titleElement, lecture);
prevWeek = week;
}
});
}
const pageNavigation = document.querySelectorAll('tbody tr:nth-child(3) td:nth-child(2) > a');
const pageNavigationLevel = pageNavigation.length;
if (pageNavigationLevel == 3) {
const courseName = document.querySelector('tbody tr:nth-child(3) td:nth-child(2) > b').innerText;
const courseID = courseName.substring(0, courseName.indexOf(' '));
insertLectureNumbering(courseID);
}
})();