11var io = require ( 'socket.io-client' ) ;
22var Handlebars = require ( 'handlebars' ) ;
3- var parseAddress = require ( '../views/parseAddress.js' ) ;
3+ var parseAddress = require ( '../lib/parseAddress.js' ) ;
4+ var youtubeInfo = require ( '../lib/youtubeInfo.js' ) ;
45
56var socket = io ( ) ;
67
7- var serverTime = 0 ;
8+ // To have access in the templates (no better way?!)
9+ window . getParseAddress = function ( address ) {
10+ return parseAddress ( address ) ;
11+ }
12+ window . getYoutubeInfo = async function ( youtubeId , play ) {
13+ return await youtubeInfo ( youtubeId , play ) ;
14+ }
15+
816
917function addToList ( item ) {
1018 list = document . getElementById ( 'playlist' ) ;
1119 var div = document . createElement ( 'div' )
1220 div . setAttribute ( 'id' , 'item' ) ;
1321
1422 var title = document . createElement ( 'h3' ) ;
15- title . innerHTML = item . info . title ;
23+ title . innerHTML = item . title ;
1624 title . setAttribute ( 'id' , 'title' ) ;
1725
1826 var entry = document . createElement ( 'img' ) ;
19- var img = item . prev ;
27+ var img = item . thumbnail ;
2028 entry . setAttribute ( 'src' , img ) ;
2129 entry . setAttribute ( 'id' , 'img' ) ;
2230
@@ -27,14 +35,17 @@ function addToList(item) {
2735
2836function sendData ( ) {
2937 document . getElementById ( 'searchResults' ) . style . display = "none" ;
30- var songText = document . getElementById ( 'songText' ) ;
31- if ( parseAddress ( songText . value ) !== "invalid" ) {
32- var urlPre = 'https://pipedproxy.kavin.rocks/vi/' + parseAddress ( songText . value ) + '/maxresdefault.jpg?host=i.ytimg.com' ;
33- var data = { url : songText . value , prev : urlPre } ;
34- if ( data . url !== '' ) {
35- socket . emit ( 'add song' , data ) ;
36- songText . value = "" ;
37- }
38+ var videoUrl = document . getElementById ( 'videoUrl' ) ;
39+ var youtubeId = parseAddress ( videoUrl . value ) ;
40+ if ( youtubeId ) {
41+ const requestYoutubeInfo = async ( ) => {
42+ var data = await youtubeInfo ( youtubeId ) ;
43+ if ( data ) {
44+ socket . emit ( 'add video' , data ) ;
45+ videoUrl . value = "" ;
46+ }
47+ } ;
48+ requestYoutubeInfo ( ) ;
3849 }
3950}
4051
@@ -58,12 +69,10 @@ function searchData() {
5869 link . href = "https://piped.kavin.rocks" + item . url ;
5970 link . title = item . title ;
6071 link . onclick = function ( e ) {
61- e . preventDefault ;
62-
63- var data = { url : link . href , prev : item . thumbnail } ;
72+ var data = { url : link . href , thumbnail : item . thumbnail , title : item . title } ;
6473 if ( data . url !== '' ) {
65- socket . emit ( 'add song ' , data ) ;
66- songText . value = "" ;
74+ socket . emit ( 'add video ' , data ) ;
75+ document . getElementById ( 'videoUrl' ) . value = "" ;
6776 }
6877
6978 return false ;
@@ -105,9 +114,9 @@ document.addEventListener('DOMContentLoaded', function() {
105114 } ) ;
106115
107116 var send = document . getElementById ( 'sendButton' ) ;
108- var songText = document . getElementById ( 'songText ' ) ;
117+ var videoUrl = document . getElementById ( 'videoUrl ' ) ;
109118 send . addEventListener ( 'click' , sendData ) ;
110- songText . addEventListener ( 'keyup' , function ( evt ) {
119+ videoUrl . addEventListener ( 'keyup' , function ( evt ) {
111120 if ( evt . keyCode == 13 )
112121 sendData ( ) ;
113122 } ) ;
@@ -124,7 +133,7 @@ document.addEventListener('DOMContentLoaded', function() {
124133 addToList ( data ) ;
125134 } ) ;
126135
127- socket . on ( 'nextSong ' , function ( data ) {
136+ socket . on ( 'nextVideo ' , function ( data ) {
128137 var t = document . querySelector ( '#playlist' ) ;
129138 if ( t . children . length > 0 )
130139 t . removeChild ( t . children [ 0 ] ) ;
@@ -135,33 +144,28 @@ document.addEventListener('DOMContentLoaded', function() {
135144
136145 socket . on ( 'start' , function ( data ) {
137146 for ( var i = 0 , f ; f = data . playlist [ i ] ; ++ i ) {
138- if ( parseAddress ( f . url ) !== "invalid" )
139- window . data . push ( parseAddress ( f . url ) ) ;
147+ if ( parseAddress ( f . url ) )
148+ window . data . push ( f . url ) ;
140149 }
141150 if ( window . data . length > 0 ) {
142- fetchHls ( window . data . shift ( ) ) ;
151+ loadVideo ( window . data . shift ( ) ) ;
143152 }
144153 } ) ;
145154
146155 socket . on ( 'added' , function ( data ) {
147- if ( parseAddress ( data . url ) !== "invalid" ) {
148- window . data . push ( parseAddress ( data . url ) ) ;
149- if ( ! document . getElementById ( 'player-status' ) . getAttribute ( ' playing' ) ) {
150- fetchHls ( window . data . shift ( ) ) ;
156+ if ( parseAddress ( data . url ) ) {
157+ window . data . push ( data . url ) ;
158+ if ( ! window . playing ) {
159+ loadVideo ( window . data . shift ( ) ) ;
151160 }
152161 }
153162 } ) ;
154163
155- socket . on ( 'updated time' , function ( currentTime ) {
156- if ( serverTime < currentTime ) {
157- serverTime = currentTime ;
158- updatedTime ( currentTime ) ;
159- }
160- } ) ;
164+ socket . on ( 'updated time' , updatedTime ) ;
161165
162- socket . on ( 'nextSong ' , function ( data ) {
163- if ( document . getElementById ( 'player-status' ) . getAttribute ( 'current-url' ) != data ) {
164- fetchHls ( window . data . shift ( ) ) ;
166+ socket . on ( 'nextVideo ' , function ( data ) {
167+ if ( window . currentVideoUrl != data ) {
168+ loadVideo ( window . data . shift ( ) ) ;
165169 }
166170 } ) ;
167171 }
0 commit comments