-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrontend.js
334 lines (261 loc) · 7.88 KB
/
frontend.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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
var resumer = {
player: null,
current_vid: "",
enabled: false,
toggle_button: null,
seek_to: 0,
waiting: false, // waiting for new video
waitTimer: null,
watchTimer: null,
watch_seconds: 5,
STATE_UNSTARTED: -1,
STATE_PLAYING: 1,
STATE_PAUSED: 2,
STATE_CUED: 5,
cookie_prefix: "w1_resume",
cookie_life: 60*60*24*30, // seconds
use_localStorage: false,
// -----------------------------------------
// START HERE
// -----------------------------------------
init: function(){
if(typeof(Storage) !== "undefined"){
this.use_localStorage = true;
console.log("<Using LocalStorage>");
}
else {
this.use_localStorage = false;
console.log("<Using Cookies>");
}
if(!this.loadPlayer()){
console.log("Invalid player element");
return;
}
this.addPlayerUI();
this.player.addEventListener(
"onStateChange",
this.onVideoStateChange.bind(this)
);
// manually execute first-time video loading
this.waitForNewVideo();
},
// -----------------------------------------
// VIDEO PLAYER
// -----------------------------------------
// FIND THE VIDEO ELEMENT
// javascript shouts when the player is invalid, although we check the element.
// try-catch calm it down.
loadPlayer: function(){
try {
var check_player = document.getElementById("movie_player");
if(typeof(check_player.getPlayerState) === 'function'){
this.player = check_player;
}
}
catch (e){
return false;
}
return true;
},
// GET CURRENT VIDEO ID
getVideoId: function(){
return this.player.getVideoData().video_id;
},
// ADD RESUMER UI TO YOUTUBE PLAYER
addPlayerUI: function(){
var controls = document.querySelector("#movie_player .ytp-left-controls");
toggle_button = document.createElement("a");
toggle_button.innerHTML = "RESUME";
toggle_button.className = "ytp-button";
toggle_button.style = "padding: 0 5px; vertical-align: top; font-weight: bold; width: auto;"; /* doesnt support full screen:: width: 55px; line-height: 35px; */
toggle_button.onclick = this.toggleAutoSave.bind(this);
controls.appendChild(toggle_button);
},
// WHEN VIDEO STATE IS CHANGED...
onVideoStateChange: function(newState){
//console.log("new state: " + newState);
if(newState == this.STATE_UNSTARTED){
this.stopWatchTimer();
this.waitForNewVideo();
}
if(this.enabled && !this.waiting){
if(newState == this.STATE_PLAYING){
if(this.seek_to != 0){
this.player.seekTo(this.seek_to);
this.seek_to = 0;
}
this.startWatchTimer();
}
else {
if(newState == this.STATE_PAUSED){
this.saveCurrentTime();
}
this.stopWatchTimer();
}
}
},
// VIDEO IS CHANGING, WAIT UNTIL NEW VIDEO DATA IS AVAILABLE
// (I added this because the video-data is changed in different player-state in each time)
// [IDEA TO DO: check new video-id on STATE_PLAYING, and then if new video available, seek to position immediately]
waitForNewVideo: function(){
this.waiting = true;
// check for new video-id
var new_vid = this.getVideoId();
//console.log("waiting... [" + new_vid + "]");
if(new_vid == this.current_vid){
if(this.waitTimer){
clearTimeout(this.waitTimer);
}
this.waitTimer = setTimeout(
this.waitForNewVideo.bind(this),
200
);
return;
}
// update new video-id
this.current_vid = new_vid;
this.waiting = false;
// update video data
this.videoLoaded();
// update player if state changed
var current_state = this.player.getPlayerState();
if(current_state != this.STATE_UNSTARTED){
this.onVideoStateChange( current_state );
}
},
// WHEN NEW VIDEO IS LOADED...
videoLoaded: function(){
console.log("new video: " + this.current_vid);
var saved_time = this.getSavedTime(this.current_vid);
if(saved_time !== false){
// we will seek to position when player start playing the video,
// otherwise we get an error from youtube (maybe bug in their side)
this.seek_to = saved_time;
console.log("saved time: " + saved_time);
this.enableAutoSave();
}
else {
this.disableAutoSave();
}
},
// -----------------------------------------
// TIMERS
// -----------------------------------------
// START TIMER
startWatchTimer: function(){
this.stopWatchTimer();
this.watchTimer = setInterval(
this.saveCurrentTime.bind(this),
this.watch_seconds * 1000
);
},
// STOP TIMER
stopWatchTimer: function(){
if(this.watchTimer){
clearInterval(this.watchTimer);
}
},
// -----------------------------------------
// AUTO-SAVE STATE
// -----------------------------------------
// TOGGLE AUTO-SAVE STATE
toggleAutoSave: function(){
if(this.enabled){
this.disableAutoSave();
}
else {
this.enableAutoSave();
this.onVideoStateChange( this.player.getPlayerState() );
}
},
// ENABLE AUTO-SAVE
enableAutoSave: function(){
this.enabled = true;
toggle_button.style.color = "#00FF00";
},
// DISABLE AUTO-SAVE
disableAutoSave: function(){
this.enabled = false;
toggle_button.style.color = "";
this.stopWatchTimer();
this.clearSavedTime(this.current_vid);
},
// -----------------------------------------
// SAVED TIME
// -----------------------------------------
// SAVE CURREMT VIDEO TIME
saveCurrentTime: function(){
if(this.use_localStorage){
localStorage.setItem(this.getVideoCookieName(this.current_vid), this.player.getCurrentTime());
}
else {
this.setCookie(this.getVideoCookieName(this.current_vid) , this.player.getCurrentTime() , this.cookie_life , "");
}
//console.log(this.player.getPlayerState() + " :: " + this.player.getCurrentTime());
},
// GET SAVED TIME OF VIDEO
getSavedTime: function(vid){
var saved_time = "";
var vid_key = this.getVideoCookieName(vid);
// even if we use localStorage, load cookie for upgrading
var saved_cookie = this.getCookie(vid_key);
if(this.use_localStorage){
saved_time = localStorage.getItem(vid_key);
// UPGRADE DATA STORAGE (from cookies to localStorage)
if(saved_time == null && saved_cookie != ""){
//console.log("upgrading cookie");
localStorage.setItem(vid_key, saved_cookie);
saved_time = saved_cookie;
this.removeCookie(vid_key);
}
}
else {
saved_time = saved_cookie;
}
if(saved_time != "" && saved_time != null){
return saved_time;
}
return false;
},
// REMOVE SAVED TIME FOR VIDEO
clearSavedTime: function(vid){
localStorage.removeItem(this.getVideoCookieName(vid));
this.removeCookie(this.getVideoCookieName(vid));
},
// -----------------------------------------
// COOKIES
// -----------------------------------------
getVideoCookieName: function(vid){
return this.cookie_prefix + "[" + vid + "]";
},
// SAVE COOKIE
setCookie: function(name , value , ttl , path){
if(isNaN(ttl)) ttl = 0;
if(path == "") path = "/";
var d = new Date();
d.setTime( d.getTime() + (ttl * 1000) );
document.cookie = name + "=" + value + "; expires=" + d.toUTCString() + "; path=" + path;
return true;
},
// REMOEV COOKIE
removeCookie: function(name){
return this.setCookie(name , "" , -60*60*24*30 , "");
},
// GET SAVED COOKIE
getCookie: function(name){
var cookies = document.cookie.split(';');
var search = name + "=";
for(var c = 0; c < cookies.length; c++){
var cookie = cookies[c];
while (cookie.charAt(0) == ' '){
cookie = cookie.substring(1);
}
if (cookie.indexOf(search) == 0){
return cookie.substring(search.length,cookie.length);
}
}
return "";
}
}
console.log("- Youtube Resumer / by Matan Mizrachi (while1.co.il) -");
resumer.init();