This repository was archived by the owner on Aug 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRepeat_Video.js
197 lines (173 loc) · 5.98 KB
/
Repeat_Video.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
/*jshint multistr: true */
// https://developers.google.com/youtube/js_api_reference
(function () {
function update_start() {
var pos = window.RepeatYouTubeVideos.video.getCurrentTime();
jQuery( '#start_input' ).val(ms_format(pos));
window.RepeatYouTubeVideos.start = pos;
// move end to new start if necessary
if (pos > window.RepeatYouTubeVideos.end) {
jQuery( '#end_input' ).val(ms_format(pos));
window.RepeatYouTubeVideos.end = pos;
}
}
function update_end() {
var pos = window.RepeatYouTubeVideos.video.getCurrentTime();
jQuery( '#end_input' ).val(ms_format(pos));
window.RepeatYouTubeVideos.end = pos;
// move start to new end if necessary
if (pos < window.RepeatYouTubeVideos.start) {
jQuery( '#start_input' ).val(ms_format(pos));
window.RepeatYouTubeVideos.start = pos;
}
}
function update_repeat_status() {
if (jQuery( '#repeat_box' ).prop('checked') ) {
window.RepeatYouTubeVideos.interval = setInterval(
function(){check_reset();}, 500
);
} else {
clearInterval(window.RepeatYouTubeVideos.interval);
}
}
function check_reset() {
var player = window.RepeatYouTubeVideos.video;
// If the video has reached or gone past the end of the repeat interval and
// it is not paused, go to the beginning of the repeat interval
if( player.getCurrentTime() >= window.RepeatYouTubeVideos.end &&
player.getPlayerState() != 2) {
player.seekTo(window.RepeatYouTubeVideos.start, true);
player.playVideo();
}
// If the video is at a point before the start of the repeat interval and
// it is not paused, skip to the start of the repeat interval
if( player.getCurrentTime() < window.RepeatYouTubeVideos.start &&
player.getPlayerState() != 2) {
player.seekTo(window.RepeatYouTubeVideos.start, true);
player.playVideo();
}
}
function loadjsfile(src, callback) {
var script = document.createElement('script'),
loaded;
script.setAttribute('src', src);
if (callback) {
script.onreadystatechange = script.onload = function() {
if (!loaded) {
callback();
}
loaded = true;
};
}
document.getElementsByTagName('head')[0].appendChild(script);
}
function loadcssfile(filename){
var fileref = document.createElement('link');
fileref.setAttribute('rel', 'stylesheet');
fileref.setAttribute('type', 'text/css');
fileref.setAttribute('href', filename);
document.getElementsByTagName('head')[0].appendChild(fileref);
}
function ms_format(x){
var m = Math.floor(x/60.0);
var s = x - 60*m;
var ret = '';
ret += m.toString() + ':';
if (s < 10)
ret += '0';
ret += s.toString();
return ret;
}
function load_ui() {
// Load jQuery UI and then call load_content
jQuery.noConflict();
loadjsfile('https://code.jquery.com/ui/1.11.1/jquery-ui.js', load_content);
}
function load_content() {
// Add dialog box to DOM
jQuery(document.body).append(
'<div id="dialog" title="Bookmarklet">\
<form>\
<p>\
<input type="checkbox" id="repeat_box" checked="true" />\
Repeat\
</p>\
<p>\
<table style="width:300px">\
<tr>\
<td>Start position:</td>\
<td><input type="text" id="start_input" size="6" readonly></td>\
<td><button type="button" id="start_butt" \
style="cursor: pointer !important; color: blue !important">\
Use current position</button></td>\
</tr>\
<tr>\
<td>End position:</td>\
<td><input type="text" id="end_input" size="6" readonly></td>\
<td><button type="button" id="end_butt" \
style="cursor: pointer !important; color: blue !important">\
Use current position</button></td>\
</tr>\
</table> \
</p>\
</form>\
</div>'
);
// Handle UI events
jQuery( '#repeat_box' ).change(update_repeat_status);
jQuery( '#start_butt' ).click( update_start );
jQuery( '#end_butt' ).click( update_end );
// Init state of repetition
initInterval();
jQuery( '#repeat_box' ).prop('checked', false);
update_repeat_status(); //(setting 'checked' in code doesn't fire 'change')
// Point jQuery to the dialog box and show it
jQuery( '#dialog' ).dialog({
autoOpen: true,
width: 350,
position: { my: 'left top', at: 'left top' },
open: function(event, ui) {
//http://jwcooney.com/2013/03/21/setting-the-z-index-and-page-position-of-a-jquery-modal-dialog/
jQuery('.ui-dialog').css('z-index',2099999999);
jQuery('.ui-widget-overlay').css('z-index',2099999999);
}
});
// Listen for the video's onStateChange event to check if a new video loads
window.RepeatYouTubeVideos.video.addEventListener('onStateChange',
onytplayerStateChange);
}
function onytplayerStateChange (newState) {
// If the state is 'unstarted', a new video is loading
if (newState === -1) {
// Disable repetition, update interval
jQuery( '#dialog' ).dialog('close');
initInterval();
jQuery( '#repeat_box' ).prop('checked', false);
update_repeat_status();
}
}
//Set interval (start and end) values and UI elements
function initInterval () {
var player = window.RepeatYouTubeVideos.video;
window.RepeatYouTubeVideos.start = 0;
window.RepeatYouTubeVideos.end = player.getDuration();
jQuery( '#start_input' ).val(ms_format(window.RepeatYouTubeVideos.start));
jQuery( '#end_input' ).val(ms_format(window.RepeatYouTubeVideos.end));
}
// If the bookmarklet has not been loaded yet
if (!window.RepeatYouTubeVideos) {
// Create the RepeatYouTubeVideos namespace
window.RepeatYouTubeVideos = {};
window.RepeatYouTubeVideos.video = document.getElementById('movie_player');
// Load a jQuery UI CSS file
loadcssfile('https://code.jquery.com/ui/1.11.1/themes/smoothness/jquery-ui.css');
// Dumb: promises would be better
// Load jQuery and then call load_ui
loadjsfile('https://code.jquery.com/jquery-2.1.1.min.js', load_ui);
} else {
// open the window again if it was closed
if ( !jQuery( '#dialog' ).dialog('isOpen') ) {
jQuery( '#dialog' ).dialog( 'open' );
}
}
})();