@@ -989,6 +989,7 @@ function sepiaFW_build_ui_cards(){
989989 //LINK
990990
991991 var currentLinkItemId = 0 ;
992+ var youTubeMessageListenerExists = false ;
992993
993994 function buildLinkElement ( cardElementInfo ) {
994995 var newId = ( "sepiaFW-card-id-" + Cards . currentCardId ++ ) ;
@@ -1035,28 +1036,67 @@ function sepiaFW_build_ui_cards(){
10351036 //linkCardEle.setAttribute('data-element', JSON.stringify(cardElementInfo));
10361037 cardBody . appendChild ( linkCardEle ) ;
10371038
1038- //Experimenting with Spotify Web Player
1039- if ( ! SepiaFW . ui . isMobile && data . type && data . type == "musicSearch" && data . brand == "Spotify" && linkUrl ) {
1040- if ( Cards . allowWebPlayer ) {
1039+ //Experimenting with web players
1040+ if ( Cards . allowWebPlayer || data . embedded ) {
1041+ //Spotify
1042+ if ( data . type && data . type == "musicSearch" && data . brand == "Spotify" && linkUrl ) {
10411043 var webPlayerDiv = document . createElement ( 'DIV' ) ;
1042- webPlayerDiv . className = "spotifyWebPlayer cardBodyItem fullWidthItem"
1043- var contentUrl = "https://" + linkUrl . replace ( "spotify:" , "open.spotify.com/embed/" ) . replace ( ":play" , "" ) . replace ( / : / g, "/" ) . trim ( ) ;
1044+ webPlayerDiv . className = "spotifyWebPlayer cardBodyItem fullWidthItem" ;
1045+ var contentUrl = linkUrl . replace ( "spotify:" , "https:// open.spotify.com/embed/" ) . replace ( ":play" , "" ) . replace ( / : / g, "/" ) . trim ( ) ;
10441046 webPlayerDiv . innerHTML = '<iframe src="' + contentUrl + '" width="100%" height="80" frameborder="0" allowtransparency="true" allow="encrypted-media"></iframe>' ;
10451047 cardBody . appendChild ( webPlayerDiv ) ;
1046- }
1047- } else if ( ! SepiaFW . ui . isMobile && data . type && data . type == "musicSearch" && data . brand == "Apple Music" && linkUrl ) {
1048- if ( Cards . allowWebPlayer ) {
1048+ //Apple Music
1049+ } else if ( data . type && data . type == "musicSearch" && data . brand == "Apple Music" && linkUrl ) {
10491050 var webPlayerDiv = document . createElement ( 'DIV' ) ;
1050- webPlayerDiv . className = "appleMusicWebPlayer cardBodyItem fullWidthItem"
1051+ webPlayerDiv . className = "appleMusicWebPlayer cardBodyItem fullWidthItem" ;
10511052 webPlayerDiv . innerHTML = '<iframe '
10521053 + 'allow="autoplay *; encrypted-media *;" frameborder="0" height="150" '
10531054 + 'style="width:100%;max-width:660px;overflow:hidden;background:transparent;" '
10541055 + 'sandbox="allow-forms allow-popups allow-same-origin allow-scripts '
10551056 + ( ( SepiaFW . ui . isSafari ) ? 'allow-storage-access-by-user-activation' : '' )
10561057 + ' allow-top-navigation-by-user-activation" '
1057- + 'src="' + linkUrl . replace ( "itunes." , "embed.music." ) + '">'
1058+ + 'src="' + linkUrl . replace ( / ^ h t t p s : \/ \/ . * ? \/ / , "https:// embed.music.apple.com/ " ) + '">'
10581059 + '</iframe>' ;
10591060 cardBody . appendChild ( webPlayerDiv ) ;
1061+ //YouTube - TODO: improve server-side to give brand and correct URL and return data.embedded
1062+ } else if ( linkUrl . indexOf ( 'youtube' ) ) {
1063+ var webPlayerDiv = document . createElement ( 'DIV' ) ;
1064+ var playerId = currentLinkItemId ++ ;
1065+ webPlayerDiv . className = "youTubeWebPlayer cardBodyItem fullWidthItem"
1066+ var f = document . createElement ( 'iframe' ) ;
1067+ f . id = 'youTubeWebPlayer-' + playerId ;
1068+ f . allow = "autoplay; encrypted-media;" ;
1069+ f . frameBorder = 0 ;
1070+ f . style . width = "100%" ; f . style . height = "280px" ; f . style . overflow = "hidden" ;
1071+ f . style . border = "4px solid" ; f . style . borderColor = "#212121" ;
1072+ f . onload = function ( ) {
1073+ //API
1074+ if ( ! youTubeMessageListenerExists ) {
1075+ youTubeMessageListenerExists = true ;
1076+ window . addEventListener ( 'message' , function ( e ) {
1077+ if ( e . origin == "https://www.youtube.com" && e . data && typeof e . data == "string" && e . data . indexOf ( "{" ) == 0 ) {
1078+ var data = JSON . parse ( e . data ) ;
1079+ if ( data && data . id ) {
1080+ //f.contentWindow.postMessage(JSON.stringify({event:'command', func:'stopVideo'}), "*"); //playVideo, paus.., stop.., next..,
1081+ if ( data . event == 'onReady' ) {
1082+ $ ( '#' + data . id ) [ 0 ] . contentWindow . postMessage ( JSON . stringify ( { event :'command' , func :'playVideo' } ) , "*" ) ;
1083+ } else if ( data . event == 'infoDelivery' && data . info && data . info . playerState == - 1 ) {
1084+ //console.log(JSON.stringify(data));
1085+ //TODO: sometimes fails AND should not end up in an endless loop!
1086+ if ( data . info . availableQualityLevels . length == 0 ) {
1087+ $ ( '#' + data . id ) [ 0 ] . contentWindow . postMessage ( JSON . stringify ( { event :'command' , func :'nextVideo' } ) , "*" ) ;
1088+ }
1089+ }
1090+ }
1091+ }
1092+ } ) ;
1093+ }
1094+ f . contentWindow . postMessage ( JSON . stringify ( { event :'listening' , id : f . id } ) , "*" ) ;
1095+ } ;
1096+ //https://www.youtube.com/results?search_query=purple+haze%2C+jimi+hendrix
1097+ f . src = "https://www.youtube.com/embed?autoplay=1&enablejsapi=1&listType=search&list=" + linkUrl . replace ( / .* ?s e a r c h _ q u e r y = / , "" ) . trim ( ) ;
1098+ cardBody . appendChild ( webPlayerDiv ) ;
1099+ webPlayerDiv . appendChild ( f ) ;
10601100 }
10611101 }
10621102
@@ -1348,7 +1388,7 @@ function sepiaFW_build_ui_cards(){
13481388 var $scrollContainer = $container . closest ( '.sepiaFW-results-container' ) ;
13491389 var y = $scrollContainer . scrollTop ( ) ;
13501390 $scrollContainer . animate ( { scrollTop : y + 60 } , animTime ) ;
1351- } , animTime ) ;
1391+ } , animTime + 50 ) ;
13521392 }
13531393 }
13541394 }
0 commit comments