forked from ChineseElectricPanda/canvas-video-enhancer
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplaylist.js
More file actions
234 lines (196 loc) · 7.58 KB
/
playlist.js
File metadata and controls
234 lines (196 loc) · 7.58 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
var playlist = [];
var weekday = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
var month = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
var semester = ['Summer School', '', 'Quarter 1', 'Semester 1', 'Quarter 2', 'Semester 2', 'Quarter 3', '', 'Quarter 4', ''];
const API_ROOT_URL = 'https://canvasvideoenhancer.azurewebsites.net';
function init() {
httpGetAsync(API_ROOT_URL + '/api/v1/playlist?course=' + info.courseCode + '&semester_code=' + info.semesterCode, function (data) {
data = JSON.parse(data);
// Check if the current video is in the list of videos retrieved
var found = false;
for (var i = 0; i < data.length; i++) {
var v = data[i];
if (v.year == parseInt(info.year)
&& v.month == parseInt(info.month)
&& v.day == parseInt(info.day)
&& v.hour == parseInt(info.hour)
&& v.minute == parseInt(info.minute)) {
found = true;
break;
}
}
// Add the current video to the list of videos if it isn't there
if (!found) {
data.push({
video_id: 1,
url: window.location.pathname.replace(/\.preview$/, ''),
year: info.year,
month: info.month,
day: info.day
});
}
// Sort playlist by date
data.sort(playlistEntryComparator);
playlist = data;
playlistList = document.querySelector('#playlist-list');
var currentItem = null;
for (var i = 0; i < data.length; i++) {
var v = data[i];
v.date = new Date(v.year, v.month - 1, v.day, v.hour, v.minute);
var playlistItem = document.createElement('div');
playlistItem.className = 'playlist-item';
playlistItem.setAttribute('onclick', 'selectVideo(this, true)');
playlistItem.dataset.id = v.video_id;
playlistItem.dataset.url = toMediastoreUrl(v)
playlistItem.innerText = weekday[v.date.getDay()] + ' ' + v.day + ' ' + month[v.month - 1];
// Add the playlist entry to the playlist list
playlistList.appendChild(playlistItem);
// If this video in the playlist is the one on the current page, select it
if (currentItem == null && window.location.href.indexOf(playlistItem.dataset.url) != -1) {
currentItem = playlistItem;
}
}
selectVideo(currentItem, false);
var courses = parseCourseNames(info.courseCode);
var coursesField = document.querySelector('#course-name');
coursesField.innerText = courses[0];
for (var i = 1; i < courses.length; i++) {
coursesField.innerText += '\n' + courses[i];
}
document.querySelector('#semester').innerText = semester[data[0].semester_code % 10] + ', ' + v.course_year;
document.querySelector('#total-videos').innerText = playlist.length;
var playlistItems = document.querySelector('.playlist-item');
});
selectTab(localStorage.getItem('canvasVideoEnhancerSelectedTab') || 'playlist');
}
window.onpopstate = function (event) {
selectVideo(document.querySelector('.playlist-item[data-id="' + event.state.id + '"]'), false);
}
function selectVideo(el, pushState) {
// Unselect all playlist elements
document.querySelectorAll('.playlist-item').forEach(function (e) {
e.className = e.className.replace(/ ?selected/, '');
});
// Select the current item
el.className += ' selected';
var url = el.dataset.url;
urlRoot = 'https://mediastore.auckland.ac.nz' + url;
video.src = urlRoot;
//Set the title of the video and page
info = parseUrl(urlRoot);
var title = info.courseName + ' ' + info.courseNumber + ' ' + info.year + '-' + info.month + '-' + info.day;
document.title = title;
document.querySelector('#title').innerText = title;
// Set the playlist progress number
for (var i = 0; i < playlist.length; i++) {
if (playlist[i].video_id == el.dataset.id) {
document.querySelector('#current-video').innerText = (i + 1);
break;
}
}
if (pushState) {
// Change the url
history.pushState({id: el.dataset.id}, '', el.dataset.url + '.preview');
}
// Load default/saved values
setQuality(localStorage.getItem('canvasVideoEnhancerQuality') || 'high');
setVolume(localStorage.getItem('canvasVideoEnhancerVolume') || 1);
lastInputVolume = setVolume(localStorage.getItem('canvasVideoEnhancerVolume') || 1);
video.currentTime = startTime;
playVideo();
}
function playVideo() {
document.querySelector('#play-button i').click();
}
function toMediastoreUrl(v) {
if ('url' in v) {
return v.url;
}
var url = '/' +
v.course_year + '/' +
v.semester_code + '/' +
v.course + '/' +
v.infix + '/' +
v.prefix + v.year + v.month.padLeft(2) + v.day.padLeft(2) + v.hour.padLeft(2) + v.minute.padLeft(2) + '.' +
v.suffix;
return url;
}
Number.prototype.padLeft = function (n, str) {
return Array(n - String(this).length + 1).join(str || '0') + this;
};
function getUrlParameter(sParam) {
var sPageURL = decodeURIComponent(window.location.search.substring(1)),
sURLVariables = sPageURL.split('&'),
sParameterName,
i;
for (i = 0; i < sURLVariables.length; i++) {
sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] === sParam) {
return sParameterName[1] === undefined ? true : sParameterName[1];
}
}
return null;
};
function parseCourseNames(str) {
var courses = str.match(/[A-z]+?\d+?[A-z]?[A-z]\d+?[A-z]/g);
for (var i = 0; i < courses.length; i++) {
var c = courses[i];
var subject = c.match(/[A-z]+?(?=\d)/)[0];
var stream = c.match(/[A-z]\d+?[A-z]$/)[0];
var courseCode = c.replace(subject, '').replace(stream, '');
courses[i] = subject + ' ' + courseCode + ' ' + stream;
}
return courses;
}
function selectTab(name) {
// Unselect all tabs
document.querySelectorAll('.tab').forEach(function (el) {
el.className = el.className.replace(/ ?selected/, '');
});
// Hide all tables
document.querySelectorAll('.tab-content-container').forEach(function (el) {
el.style.display = 'none';
});
// Select the given tab
document.querySelector('#' + name + '-tab-header').className += ' selected';
document.querySelector('#' + name + '-container').style.display = 'flex';
localStorage.setItem('canvasVideoEnhancerSelectedTab', name);
}
function httpGetAsync(theUrl, callback) {
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
callback(xmlHttp.responseText);
}
xmlHttp.open("GET", theUrl, true); // true for asynchronous
xmlHttp.send(null);
}
function playlistEntryComparator(v1, v2) {
if (v1.year < v2.year) {
return -1;
} else if (v1.year > v2.year) {
return 1;
}
if (v1.month < v2.month) {
return -1;
} else if (v1.month > v2.month) {
return 1;
}
if (v1.day < v2.day) {
return -1;
} else if (v1.day > v2.day) {
return 1;
}
if (v1.hour < v2.hour) {
return -1;
} else if (v1.hour > v2.hour) {
return 1;
}
if (v1.minute < v2.minute) {
return -1;
} else if (v1.minte > v2.minute) {
return 1;
}
return 0;
}
init();