-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmyScript.js
More file actions
508 lines (460 loc) · 17.9 KB
/
myScript.js
File metadata and controls
508 lines (460 loc) · 17.9 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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
var o_peepTube = new peepTube();
// initialize the content modal
console.log('initialize peepTube!');
o_peepTube.initModal();
var currentURL = '';
// Listen the background script msg
//$(function(){
chrome.runtime.onMessage.addListener(function(msg, sender, sendResponse){
console.log('receive msg from background scripts');
//prevent duplicate button append
if(currentURL != msg.url){
if(msg.url.indexOf('www.youtube.com/results') != -1){
console.info('[peepTube]add btn on search result page');
o_peepTube.pauseYTPlayer();// prevent voice keep play (Youtube bug!)
o_peepTube.appendBtn('search_results');
}
else if (msg.url.indexOf('www.youtube.com/watch') != -1){
console.info('[peepTube]add btn on watch video page');
o_peepTube.appendBtn('watch_video',msg.url);
}
else if(msg.url == 'http://www.youtube.com/' || msg.url == 'https://www.youtube.com/'){
console.info('[peepTube]add btn on watch main page');
o_peepTube.pauseYTPlayer(); // prevent voice keep play (Youtube bug!)
o_peepTube.appendBtn('main_page');
}
// Add "load modal" button event
$('.peepButton').off('click').on('click',function(){
var clickedBtn = this;
o_peepTube.renderModal(clickedBtn);
});
}
currentURL = msg.url;
init_peepStoryboard(currentURL);
});
// peepTube Class
function peepTube(){
var peepTubeComponent = this;
// Public methods
peepTubeComponent.appendBtn = function(page,url){
url = typeof url !== 'undefined' ? url : '';
if (page == 'watch_video'){
initYTShrink(url);
$('.related-list-item').each(function(idx,e){
var currentDOM = e;
var resultUrl = $(currentDOM).find('a').attr('href');
if(!$(currentDOM).prev().hasClass('peepButton')){
if (resultUrl.indexOf('/watch?v=') != -1){
$(currentDOM).wrap('<div class="peepRow peepRow-related"></div>');
$(currentDOM).before( "<button class='peepButton peepBtn-related glyphicon glyphicon-eye-open btn btn-default'></button>" );
}
}
});
//Pause the Youtube video if peep suggestion video
$('.peepBtn-related').on('click',function(){
//console.log('click suggestion video!');
peepTubeComponent.pauseYTPlayer();
});
}
else{
//Add peep button
$('.yt-lockup').each(function(idx,e){
var currentDOM = e;
var resultUrl = $(currentDOM).find('a').attr('href');
//prevent duplicate button append (again)
if(!$(currentDOM).prev().hasClass('peepButton')){
if (resultUrl.indexOf('/watch?v=') != -1){
$(currentDOM).wrap('<div class="peepRow"></div>');
switch(page){
case 'search_results':
$(currentDOM).before( "<button class='peepButton peepBtn-results glyphicon glyphicon-eye-open btn btn-default'></button>" );
break;
case 'main_page':
$(currentDOM).before( "<button class='peepButton peepBtn-main glyphicon glyphicon-eye-open btn btn-default'></button>" );
break;
}
}
}
});
}
};
peepTubeComponent.initModal = function(){
// Create "poopContainer"
$('#content').before("<div id='peepContainer'></div>");
// Add "close modal" event
$('#peepContainer').on('click',function(){
if($(this).hasClass('defaultContainerView')){
peepTubeComponent.closeModal();
}
});
//Create "close btn"
$('#peepContainer').append("<button id='peepClose-btn' class='glyphicon glyphicon-remove'></button>");
// Add "close btn" event
$('#peepClose-btn').on('click',function(){
peepTubeComponent.closeModal();
});
//Create "redirect btn"
$('#peepContainer').append("<button id='peepRedirect-btn' class='glyphicon glyphicon-plus'></button>");
$('#peepRedirect-btn').on('click',function(event){
//peepTubeComponent.redirectPage();
event.stopPropagation(); // prevent click #peepContainer to close modal
peepTubeComponent.peepMode();
});
//Create shrink btn
$('#content').before("<button id='peepShrinkBtn' class='glyphicon glyphicon-import btn btn-default'></button>");
};
peepTubeComponent.renderModal = function(clickedBtn){
//Clear the iframe in container first
$('#peepContainer').find('iframe').remove();
$('body').css({"overflow":'hidden'});
var previewUrl = $(clickedBtn).next().eq(0).find('a').attr('href');
var splitToken = '/watch?v=';
var embedVideoID = previewUrl.split(splitToken)[1].replace('&list','?list');
//console.log(previewUrl);
var iframeObj = iframeObjGenerator(embedVideoID);
initDefaultView(); // initialize the container css for modal
$('#peepContainer').fadeIn(function(){
$('#peepContainer').append(iframeObj);
});
$('#peepShrinkBtn').fadeIn();
};
peepTubeComponent.peepMode = function(){
$('body').css({"overflow":'hidden'});
// Setting iframe position
var topOffset = $(document).scrollTop() + 120; // add 20px top offset
var contentAreaWidth = $('body').width(); // Get content area width
var baseWidth = 560.0;
var baseHeight = 315.0;
var ratio = (contentAreaWidth/baseWidth) * 0.65;
var realWidth = baseWidth * ratio;
var realHeight = baseHeight * ratio;
// destroy the drag event handler & initialize the container position
$('.shrinkContainerView').draggable('destroy');
$('#peepContainer').css({'top': '0px', 'left': '0px'})
.find('iframe').attr({width: realWidth, height: realHeight})
.css({top: topOffset, position:'absolute', right: '15%',left: ''});
// change class
$('#peepContainer').removeClass('shrinkContainerView').addClass('defaultContainerView');
// Show shrinkBtn
$('#peepShrinkBtn').fadeIn();
// Add "shrink peep" button event
$('#peepShrinkBtn').on('click',function(){
peepTubeComponent.shinkPeepView();
});
};
peepTubeComponent.closeModal = function(){
$('body').css({"overflow":'auto'});
$('#peepContainer').fadeOut(function(){
$('#peepContainer').find('iframe').remove();
});
$('#peepShrinkBtn').fadeOut();
};
peepTubeComponent.shinkPeepView = function(){
initShrinkView(); // initialize the container css for shink view
$('#peepContainer').find('iframe').attr({width:'360px', height: '200px'})
.css({top: '20px', left: '0px', 'position': 'absolute', 'z-index': '1000'});
$('body').css({"overflow":'auto'});
$('#peepShrinkBtn').fadeOut();
};
// This method has been deprecated
peepTubeComponent.redirectPage = function(){
var videoID = $('#YTplayer').attr('src').split('/v/')[1].split('?')[0];
peepTubeComponent.closeModal();
console.log(videoID);
window.location = 'http://www.youtube.com/watch?v=' + videoID;
};
// Private methods
var iframeObjGenerator = function(embedVideoID, startTime){
startTime = typeof startTime !== 'undefined' ? startTime : 0;
// Setting iframe position
var topOffset = $(document).scrollTop() + 120; // add 20px top offset
var contentAreaWidth = $('body').width(); // Get content area width
var baseWidth = 560.0;
var baseHeight = 315.0;
var ratio = (contentAreaWidth/baseWidth) * 0.65;
var realWidth = baseWidth * ratio;
var realHeight = baseHeight * ratio;
var iframeObj = '<iframe id="YTplayer" width="' + realWidth + '" height="'+ realHeight + '" \
style="top:' + topOffset + 'px; position: absolute; right: 15%;" \
src="//www.youtube.com/v/' + embedVideoID + '?version=3&autoplay=1&start='+ startTime +'" frameborder="0" enablejsapi="1"></iframe>';
//console.log(iframeObj);
return iframeObj;
};
var initDefaultView = function(){
// destroy the drag event handler & initialize the container position
$('.shrinkContainerView').draggable('destroy');
$('#peepContainer').css({'top': '0px', 'left': '0px'});
// change class
$('#peepContainer').removeClass('shrinkContainerView').addClass('defaultContainerView')
.find('iframe').remove();
// Add "shrink peep" button event
$('#peepShrinkBtn').on('click',function(){
peepTubeComponent.shinkPeepView();
});
};
var initShrinkView = function(){
$('#peepContainer').removeClass('defaultContainerView').addClass('shrinkContainerView');
// Add "shrink peep" dragable event & setting the container position
$('.shrinkContainerView').draggable();
$('#peepContainer').css({'right': '20px', 'bottom': '0px', 'top': '', 'left': ''});
};
//Pause the youtube video & render shrinked window start with pause time
var initYTShrink = function(url){
var videoID = url.split('v=')[1]; // Get the video ID
console.log(videoID);
//console.log(ytplayer);
// video player is AS3 or AS2
if($('#movie_player').is('embed')){
var ytplayer = document.getElementById('movie_player');
$('#watch7-user-header').append( "<button id='shrinkMovie-btn' class='glyphicon glyphicon-import btn btn-default'></button>" );
$('#shrinkMovie-btn').on('click',function(){
ytplayer.pauseVideo(); // Pause the video
var pauseTime = ytplayer.getCurrentTime(); // Get the pause time
pauseTime = Math.floor(pauseTime);
console.log(ytplayer.getCurrentTime());
var iframeObj = iframeObjGenerator(videoID, pauseTime);
// Render the shrinked modal
initShrinkView();
$('#peepContainer').fadeIn(function(){
$(this).find('iframe').remove();
$(this).append(iframeObj).find('iframe').attr({width:'360px', height: '200px'})
.css({top: '20px', left: '0px', 'position': 'absolute', 'z-index': '1000'});
});
});
}
// video player is html5
else{
$('#watch7-user-header').append( "<button id='shrinkMovie-btn' class='glyphicon glyphicon-import btn btn-default'></button>" );
$('#shrinkMovie-btn').on('click',function(){
var ytplayer = $('#movie_player');
var pauseTime = 0;
if($(ytplayer).hasClass('playing-mode')){
$(ytplayer).find('.ytp-button-pause').click(); // pause the video if playing
}
var currentTime_tmp = $(ytplayer).find('.ytp-time-current').html();
pauseTime = parseInt(currentTime_tmp.split(':')[0])*60 + parseInt(currentTime_tmp.split(':')[1])
console.log(pauseTime);
var iframeObj = iframeObjGenerator(videoID,pauseTime);
// Render the shrinked modal
initShrinkView();
$('#peepContainer').fadeIn(function(){
$(this).find('iframe').remove();
$(this).append(iframeObj).find('iframe').attr({width:'360px', height: '200px'})
.css({top: '20px', left: '0px', 'position': 'absolute', 'z-index': '1000'});
});
});
}
};
// Pause the video in Youtube
peepTubeComponent.pauseYTPlayer = function(){
if($('#movie_player').is('embed')){
var ytplayer = document.getElementById('movie_player');
ytplayer.pauseVideo(); // Pause the video
}
else{
var ytplayer = $('#movie_player');
if($(ytplayer).hasClass('playing-mode')){
$(ytplayer).find('.ytp-button-pause').click(); // pause the video if playing
}
}
};
};
// Initialize function for peepStoryboard
function init_peepStoryboard(currentWebPage){
// Create peepStoryboard Object
var o_storyboard = new peepStoryboard();
o_storyboard.currentWebPage = currentWebPage;
// Storyboard for suggestion videos
if(currentWebPage.indexOf('www.youtube.com/watch') !== -1){
o_storyboard.pageType = 'suggestItem';
addListner_peepStoryboard('related-list-item', o_storyboard);
}
//searching results
else if(currentWebPage.indexOf('www.youtube.com/results') != -1){
o_storyboard.pageType = 'searchResult';
addListner_peepStoryboard('yt-lockup', o_storyboard);
}
// Storyboard for main page
else{
o_storyboard.pageType = 'mainPage';
addListner_peepStoryboard('yt-lockup', o_storyboard);
}
}
function addListner_peepStoryboard(targetClassName, o_storyboard){
var prev_videoURL = NaN;
$('.' + targetClassName).on('mouseover',function(){
var $currentDOM = $(this);
var videoURL = $currentDOM.find('a').attr('href');
o_storyboard.$targetDOM = $currentDOM;
// Doing ajax only when the mouseovering video is different than before
if(prev_videoURL != videoURL){
o_storyboard.getInfo(videoURL);
}
prev_videoURL = videoURL;
});
// Playing storyboard frames
$('.' + targetClassName).on('mousemove',function(e){
o_storyboard.play(e);
})
// Stop playing storyboard when mouseout
$('.' + targetClassName).on('mouseout',function(){
var $currentDOM = $(this);
o_storyboard.$targetDOM = $currentDOM;
o_storyboard.end();
});
}
// peepStoryboard Class
function peepStoryboard(){
var stbComponent = this;
stbComponent.imgs = [];
stbComponent.currentWebPage = NaN; // the current web url => to identify the rowWidth, init as NaN
stbComponent.pageType = NaN; // [ 'searchResult', 'suggestItem','mainPage']
stbComponent.$targetDOM = NaN; // the mouseover ".yt-lockup" , init as NaN
stbComponent.ajaxStatus = false; // set as true if doing ajax
// Current DOM infomation, init as NaN
var rowWidth = NaN;
var thumbnailWidth = NaN;
var thumbnailHeight = NaN;
// Storyboard CSS, init as NaN
var backgroundWidth = NaN;
var backgroundHeight = NaN;
var backgroundSize = NaN;
var backgroundPosition = [];
/*
@ Get storyboard imgs (by ajax) & current DOM information (rowWidth, thumbnailWidth, etc.)
*/
stbComponent.getInfo = function(url){
stbComponent.ajaxStatus = true; // Ajax start, set status as true
//Get storyboard imgs via ajax
$.get(url,function(data){
//console.log(data);
var videoID = url.split('watch?v=')[1];
// If can't extract "sigh" => the video has no storyboard frames
try{
var sigh = data.split('storyboard_spec')[1].split(',')[0].split('|')[3].split('#M$M#')[1].replace(/"/g,'');
//console.log(sigh);
// So far, fix the img number as 5
for(i=0; i < 5; i++){
stbComponent.imgs[i] = "//i1.ytimg.com/sb/" + videoID + "/storyboard3_L2/M" + i + ".jpg?sigh=" + sigh;
}
// Remove the unavailable img url
for(i=0; i< 5; i++){
$.ajax({
url: stbComponent.imgs[i],
type: 'HEAD',
error: function(){
var index = stbComponent.imgs.indexOf(this.url.split(':')[1]);
if (index > -1)
stbComponent.imgs.splice(index, 1);
}
});
}
}
catch(err){
stbComponent.imgs = [];
}
stbComponent.ajaxStatus = false; // Ajax complete, set status as false
});
// Identify the available mouse playing area
if(stbComponent.currentWebPage.indexOf('www.youtube.com/results') != -1)
rowWidth = stbComponent.$targetDOM.width() - stbComponent.$targetDOM.prev().width();
else
rowWidth = stbComponent.$targetDOM.width();
// Get the thumbnail size
switch (stbComponent.pageType){
case 'suggestItem':
thumbnailHeight = stbComponent.$targetDOM.height();
thumbnailWidth = stbComponent.$targetDOM.find('.yt-uix-simple-thumb-wrap').width();
break;
case 'searchResult':
thumbnailHeight = stbComponent.$targetDOM.find('.video-thumb > img').height();
thumbnailWidth = stbComponent.$targetDOM.find('.video-thumb > img').width();
break;
case 'mainPage':
thumbnailHeight = stbComponent.$targetDOM.find('.yt-thumb-clip > img').height();
thumbnailWidth = stbComponent.$targetDOM.find('.yt-thumb-default').width();
break;
}
//console.log('===================');
//console.log('thumbnailWidth: ' + thumbnailWidth);
//console.log('thumbnailHeight: ' + thumbnailHeight);
//console.log('===================');
// Storyboard background CSS
backgroundWidth = thumbnailWidth*5;
backgroundHeight = thumbnailHeight*5;
backgroundSize = backgroundWidth + 'px ' + backgroundHeight + 'px';
for(i=0; i < 25; i++){
var x_position = -((i%5)*thumbnailWidth);
var y_position = -(parseInt(i/5)*thumbnailHeight);
backgroundPosition[i] = x_position + 'px ' + y_position + 'px';
}
};
/*
@ Start playing storyboard frames
@ Parameters: mouseEventObj => mouseover event object
rowWidth => The width of detectable row area(".yt-lockup" - "peepButton" )
*/
stbComponent.play = function(mouseEventObj){
if((stbComponent.$targetDOM.prev().hasClass('peepButton') || stbComponent.pageType === 'suggestItem')
&& stbComponent.imgs.length > 0 && stbComponent.ajaxStatus == false) {
var mouseOffsetX = mouseEventObj.offsetX;
var leftRatio = (mouseOffsetX*stbComponent.imgs.length)/rowWidth;
var storyboardImg = stbComponent.imgs[Math.floor(leftRatio)];
var positionIdx = Math.floor((leftRatio - Math.floor(leftRatio))*25);
switch (stbComponent.pageType){
case 'suggestItem':
stbComponent.$targetDOM.find('.yt-uix-simple-thumb-wrap')
.css({
'width' : thumbnailWidth + 'px',
'margin': '0px 1px',
'background-size' : backgroundSize,
'background-image': 'url(' + storyboardImg + ')',
'background-position': backgroundPosition[positionIdx]
});
//Hide the original thumbnail image
stbComponent.$targetDOM.find('.yt-uix-simple-thumb-wrap').find('img').addClass('visibility-hidden');
break;
case 'searchResult':
stbComponent.$targetDOM.find('.video-thumb')
.css({
'width' : thumbnailWidth + 'px',
'margin': '0px 1px',
'background-size' : backgroundSize,
'background-image': 'url(' + storyboardImg + ')',
'background-position': backgroundPosition[positionIdx]
});
stbComponent.$targetDOM.find('.video-thumb').find('img').addClass('visibility-hidden');
break;
case 'mainPage':
stbComponent.$targetDOM.find('.yt-thumb-default')
.css({
'width' : thumbnailWidth + 'px',
'margin': '0px 1px',
'background-size' : backgroundSize,
'background-image': 'url(' + storyboardImg + ')',
'background-position': backgroundPosition[positionIdx]
});
//Hide the original thumbnail image
stbComponent.$targetDOM.find('.yt-thumb-clip').addClass('visibility-hidden');
break;
}
}
};
/*
@ Set the css as the defult of Youtube (hide storyboard & show thumbnail)
@ Trigger: mouseout on ".yt-lockup"
*/
stbComponent.end = function(){
switch (stbComponent.pageType){
case 'suggestItem':
stbComponent.$targetDOM.find('.yt-uix-simple-thumb-wrap').find('img').removeClass('visibility-hidden');
break;
case 'searchResult':
stbComponent.$targetDOM.find('.video-thumb').find('img').removeClass('visibility-hidden');
break;
case 'mainPage':
stbComponent.$targetDOM.find('.yt-thumb-clip').removeClass('visibility-hidden');
break;
}
}
}