-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
189 lines (176 loc) · 5.49 KB
/
main.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
// Add the YouTube API to the page.
function addAPITag()
{
// Create a <script> tag and add it before the first one in the markup
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
}
// The API calls this function when the iFrame is ready
function onYouTubeIframeAPIReady() {
var player = new YT.Player('player', {
height: '566',
width: '960',
videoId: getURLParams().v,
events: {
'onReady': onPlayerReady
}
});
// Add event listeners to the HTML form
$('loop-times').addEventListener('submit', updateTimes(player));
$('reset-times').addEventListener('click', function() {
resetTimes(player);
updateTimes(player)();
});
// Start/End set buttons
$('loop-set-start').addEventListener('click', function() {
var currTime = player.getCurrentTime();
$('startTime').value = secondsToTime(currTime);
});
$('loop-set-end').addEventListener('click', function() {
var currTime = player.getCurrentTime();
$('endTime').value = secondsToTime(currTime);
});
// Show/hide QR code
$('qr-button').addEventListener('click', function() {
updateQRCode();
$('qrcontainer').style.opacity = 1;
$('qrcontainer').style.visibility = 'visible';
});
$('qrcontainer').addEventListener('click', function() {
if($('qrcontainer').style.visibility == 'visible')
{
$('qrcontainer').style.opacity = 0;
$('qrcontainer').style.visibility = 'hidden';
}
});
// Update the loop times once before the video runs
updateTimes(player)();
}
// Reset the loop endpoints
function resetTimes(player) {
var p = getURLParams();
if(p.startTime)
{
$("startTime").value = p.startTime;
} else {
$("startTime").value = secondsToTime(0);
}
if(p.endTime)
{
$("endTime").value = p.endTime;
} else {
$("endTime").value = secondsToTime(player.getDuration());
}
}
// Start the playback loop
function startLoop(player) {
var start = timeToSeconds($("startTime").value);
if(player.seekTo)
player.seekTo(start, true);
}
// Shamelessly stolen from jQuery
function $(id) {
return document.getElementById(id);
}
// The API will call this function when the video player is ready.
function onPlayerReady(event) {
event.target.playVideo();
// Get start and end times out of the URL parameters
resetTimes(event.target)
// Start the video clock
setInterval(videoTick(event.target), 150);
}
// Keep track of our position in the video and restart playback at the correct time
function videoTick(player)
{
// Use a closure to avoid having a global video player object
return function()
{
var t = player.getCurrentTime();
var end = timeToSeconds($("endTime").value);
var start = timeToSeconds($("startTime").value);
// Return to the beginning of the loop period
if(t >= end && end > start)
{
startLoop(player);
}
// Make sure the times are valid
if(end > player.getDuration())
{
$("endTime").value = secondsToTime(player.getDuration());
}
if(start >= end || start < 0) {
$("startTime").value = 0;
}
}
}
// Update the loop times and seek to the start of the loop
function updateTimes(player)
{
// Closure over the player variable
return function() {
var startTime = timeToSeconds($('startTime').value);
var endTime = timeToSeconds($('endTime').value);
var duration = 86400; // 24 hours is a good maximum
if(player.getDuration)
duration = player.getDuration();
// Start time must be non-negative and < video length
if(startTime < 0 || startTime >= duration)
startTime = 0;
$("startTime").value = secondsToTime(startTime);
// End time must be after start time and <= video length
if(endTime <= startTime || endTime > duration)
endTime = duration;
$("endTime").value = secondsToTime(endTime);
startLoop(player)
};
}
// Update the QR code
function updateQRCode() {
var videoID = getURLParams().v;
var startTime = timeToSeconds($('startTime').value);
var endTime = timeToSeconds($('endTime').value);
var url = 'https://www.youtubeencore.com/watch?v=' + videoID + '&startTime=' + secondsToTime(startTime) + '&endTime=' + secondsToTime(endTime);
var qrType = Math.ceil(url.length/18);
var qr = qrcode(qrType, 'M');
qr.addData(url);
qr.make();
qr.drawOnCanvas($('qrcanvas'), 8);
}
// Convert a number of seconds to an mm:ss string
function secondsToTime(s)
{
var minutes = Math.floor(s/60);
var seconds = Math.floor((s - minutes*60)*100)/100; // Round seconds to the nearest .01
if(seconds < 10)
{
seconds = "0" + seconds;
}
return minutes + ":" + seconds;
}
// Convert a time string (minutes:seconds) to a number of seconds
function timeToSeconds(t)
{
var colonPos = t.indexOf(":");
if(colonPos > -1)
return t.slice(0,colonPos) * 60 + t.slice(colonPos+1) * 1;
return t * 1;
}
// Get GET parameters
function getURLParams()
{
var prmstr = window.location.search.substr(1);
var prmarr = prmstr.split ("&");
var params = {};
// Iterate over the array of parameters, splitting them on '=' to get names and values
for (var i = 0; i < prmarr.length; i++)
{
var tmparr = prmarr[i].split("=");
params[tmparr[0]] = tmparr[1];
}
return params;
}
// Add the API
addAPITag();