@ -1,14 +1,4 @@
'use strict' ;
var video _data = JSON . parse ( document . getElementById ( 'video_data' ) . textContent ) ;
var spinnerHTML = '<h3 style="text-align:center"><div class="loading"><i class="icon ion-ios-refresh"></i></div></h3>' ;
var spinnerHTMLwithHR = spinnerHTML + '<hr>' ;
String . prototype . supplant = function ( o ) {
return this . replace ( /{([^{}]*)}/g , function ( a , b ) {
var r = o [ b ] ;
return typeof r === 'string' || typeof r === 'number' ? r : a ;
} ) ;
} ;
function toggle _parent ( target ) {
var body = target . parentNode . parentNode . children [ 1 ] ;
@ -21,18 +11,6 @@ function toggle_parent(target) {
}
}
function toggle _comments ( event ) {
var target = event . target ;
var body = target . parentNode . parentNode . parentNode . children [ 1 ] ;
if ( body . style . display === 'none' ) {
target . textContent = '[ − ]' ;
body . style . display = '' ;
} else {
target . textContent = '[ + ]' ;
body . style . display = 'none' ;
}
}
function swap _comments ( event ) {
var source = event . target . getAttribute ( 'data-comments' ) ;
@ -43,36 +21,6 @@ function swap_comments(event) {
}
}
function hide _youtube _replies ( event ) {
var target = event . target ;
var sub _text = target . getAttribute ( 'data-inner-text' ) ;
var inner _text = target . getAttribute ( 'data-sub-text' ) ;
var body = target . parentNode . parentNode . children [ 1 ] ;
body . style . display = 'none' ;
target . textContent = sub _text ;
target . onclick = show _youtube _replies ;
target . setAttribute ( 'data-inner-text' , inner _text ) ;
target . setAttribute ( 'data-sub-text' , sub _text ) ;
}
function show _youtube _replies ( event ) {
var target = event . target ;
var sub _text = target . getAttribute ( 'data-inner-text' ) ;
var inner _text = target . getAttribute ( 'data-sub-text' ) ;
var body = target . parentNode . parentNode . children [ 1 ] ;
body . style . display = '' ;
target . textContent = sub _text ;
target . onclick = hide _youtube _replies ;
target . setAttribute ( 'data-inner-text' , inner _text ) ;
target . setAttribute ( 'data-sub-text' , sub _text ) ;
}
var continue _button = document . getElementById ( 'continue' ) ;
if ( continue _button ) {
continue _button . onclick = continue _autoplay ;
@ -208,111 +156,6 @@ function get_reddit_comments() {
} ) ;
}
function get _youtube _comments ( ) {
var comments = document . getElementById ( 'comments' ) ;
var fallback = comments . innerHTML ;
comments . innerHTML = spinnerHTML ;
var url = '/api/v1/comments/' + video _data . id +
'?format=html' +
'&hl=' + video _data . preferences . locale +
'&thin_mode=' + video _data . preferences . thin _mode ;
var onNon200 = function ( xhr ) { comments . innerHTML = fallback ; } ;
if ( video _data . params . comments [ 1 ] === 'youtube' )
onNon200 = function ( xhr ) { } ;
helpers . xhr ( 'GET' , url , { retries : 5 , entity _name : 'comments' } , {
on200 : function ( response ) {
comments . innerHTML = ' \
< div > \
< h3 > \
< a href = "javascript:void(0)" > [ − ] < / a > \
{ commentsText } \
< / h 3 > \
< b > \
< a href = "javascript:void(0)" data - comments = "reddit" > \
{ redditComments } \
< / a > \
< / b > \
< / d i v > \
< div > { contentHtml } < / d i v > \
< hr > ' . supplant ( {
contentHtml : response . contentHtml ,
redditComments : video _data . reddit _comments _text ,
commentsText : video _data . comments _text . supplant ( {
// toLocaleString correctly splits number with local thousands separator. e.g.:
// '1,234,567.89' for user with English locale
// '1 234 567,89' for user with Russian locale
// '1.234.567,89' for user with Portuguese locale
commentCount : response . commentCount . toLocaleString ( )
} )
} ) ;
comments . children [ 0 ] . children [ 0 ] . children [ 0 ] . onclick = toggle _comments ;
comments . children [ 0 ] . children [ 1 ] . children [ 0 ] . onclick = swap _comments ;
} ,
onNon200 : onNon200 , // declared above
onError : function ( xhr ) {
comments . innerHTML = spinnerHTML ;
} ,
onTimeout : function ( xhr ) {
comments . innerHTML = spinnerHTML ;
}
} ) ;
}
function get _youtube _replies ( target , load _more , load _replies ) {
var continuation = target . getAttribute ( 'data-continuation' ) ;
var body = target . parentNode . parentNode ;
var fallback = body . innerHTML ;
body . innerHTML = spinnerHTML ;
var url = '/api/v1/comments/' + video _data . id +
'?format=html' +
'&hl=' + video _data . preferences . locale +
'&thin_mode=' + video _data . preferences . thin _mode +
'&continuation=' + continuation ;
if ( load _replies ) url += '&action=action_get_comment_replies' ;
helpers . xhr ( 'GET' , url , { } , {
on200 : function ( response ) {
if ( load _more ) {
body = body . parentNode . parentNode ;
body . removeChild ( body . lastElementChild ) ;
body . insertAdjacentHTML ( 'beforeend' , response . contentHtml ) ;
} else {
body . removeChild ( body . lastElementChild ) ;
var p = document . createElement ( 'p' ) ;
var a = document . createElement ( 'a' ) ;
p . appendChild ( a ) ;
a . href = 'javascript:void(0)' ;
a . onclick = hide _youtube _replies ;
a . setAttribute ( 'data-sub-text' , video _data . hide _replies _text ) ;
a . setAttribute ( 'data-inner-text' , video _data . show _replies _text ) ;
a . textContent = video _data . hide _replies _text ;
var div = document . createElement ( 'div' ) ;
div . innerHTML = response . contentHtml ;
body . appendChild ( p ) ;
body . appendChild ( div ) ;
}
} ,
onNon200 : function ( xhr ) {
body . innerHTML = fallback ;
} ,
onTimeout : function ( xhr ) {
console . warn ( 'Pulling comments failed' ) ;
body . innerHTML = fallback ;
}
} ) ;
}
if ( video _data . play _next ) {
player . on ( 'ended' , function ( ) {
var url = new URL ( 'https://example.com/watch?v=' + video _data . next _video ) ;