-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit-lecture.js
More file actions
120 lines (102 loc) · 4.04 KB
/
split-lecture.js
File metadata and controls
120 lines (102 loc) · 4.04 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
/*
split-lecture.js -- a simple way to synchronise YouTube videos and Jupyter Notebooks.
Copyright (c) 2020 Stefan Guettel
The MIT License (MIT)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
// This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
// This function creates an <iframe> (and YouTube player) after the API code downloads.
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
videoId: ytVideoId,
modestbranding: 1,
rel: 0,
playsinline: 1,
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
// The API will call this function when the video player is ready.
function onPlayerReady(event) {
event.target.playVideo();
}
// The API calls this function when the player's state changes. The function indicates when playing a video (state=1).
var syncTimer;
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING) {
autoSync();
syncTimer = setInterval(autoSync, 2000);
}
else clearInterval(syncTimer);
}
var lastanchor = '';
function autoSync() {
ct = player.getCurrentTime();
m = Math.floor(ct/60);
s = Math.round(ct - m*60);
if (m < 10) m = '0'+String(m);
if (s < 10) s = '0'+String(s);
cts = String(m) + ':' + String(s); // current timestamp
console.log('cts: '+cts);
var anchor = '';
for (var i = 0; i < tslist.length; i++) {
ts = tslist[i].substring(0,5);
if (cts >= ts) anchor = tslist[i].substring(6);
else break;
}
console.log('anchor: ' + anchor);
if (lastanchor != anchor) {
lastanchor = anchor;
scrollToAnchor(anchor);
}
}
function scrollToAnchor(anchor) {
try {
offs = $('#nb').contents().find('a[href="#'+anchor+'"]').offset().top - 30;
$("#nb").contents().find("html,body").animate({ scrollTop: offs}, 2000);
$('#linklist a').removeClass('active');
var anchorid = anchor.replace(/[^a-z]+|\s+/gmi, '');
$('#'+anchorid).addClass('active');
console.log(offs);
} catch(err) {
console.log('didnt manage to scroll to '+anchor);
}
}
function seek(anchor,tsp) {
try {
scrollToAnchor(anchor);
player.seekTo(tsp,1);
} catch(err) {
console.log('didnt manage to seek to '+anchor);
}
}
$(document).ready(function() {
// make panel resizable
$(".panel-left").resizable({
handleSelector: ".splitter",
resizeHeight: false
});
// deal with linklist
$('#nb').ready(function() {
$('#nb').attr('src', notebookUrl);
for (var i = 0; i < tslist.length; i++) {
var ts = tslist[i].substring(0,6).trim();
tsp = ts.split(':');
tsp = 60*parseInt(tsp[0]) + parseInt(tsp[1]);
var anchor = tslist[i].substring(6).trim();
var anchortext = anchor.replace(/-/g," ");
anchortext = anchortext.replace(/%22/g,""");
var anchorid = anchor.replace(/[^a-z]+|\s+/gmi, '');
$( "#linklist" ).append('<a href="javascript:seek(\''+anchor+'\','+tsp+');" id="'+anchorid+'">'+ts+' '+anchortext+'</a>');
}
});
});