Skip to content

Commit 87d5934

Browse files
committed
media-player widgets (wip, alpha2)
1 parent 81ac54e commit 87d5934

File tree

3 files changed

+44
-102
lines changed

3 files changed

+44
-102
lines changed

www/css/sepiaFW-cards.css

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,12 +419,19 @@
419419
left: 0;
420420
width: 100%;
421421
height: 100%;
422-
background: #eee;
422+
margin: 5px 5px 0 5px;
423+
width: calc(100% - 10px);
424+
height: calc(100% - 5px);
425+
background: #dfdfdf;
423426
color: #000;
424427
display: flex;
425428
justify-content: center;
426429
align-items: center;
427430
}
431+
.dark-skin .sepiaFW-cards-flexSize-container .cardBodyItem.embeddedWebPlayer .cardItemOverlay {
432+
background: #212327;
433+
color: #eee;
434+
}
428435
.sepiaFW-cards-flexSize-container .cardBodyItem.embeddedWebPlayer .cardItemOverlay p {
429436
animation: sepiaFW-animation-blink 3s infinite;
430437
}

www/scripts/sepiaFW.ui.cards.embed.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ function sepiaFW_build_ui_cards_embed(){
1212
*/
1313
var playerWidgets = {
1414
default: "<assist_server>/widgets/mp-default.html",
15+
embedded: "<assist_server>/widgets/mp-default.html",
1516
spotify: "<assist_server>/widgets/mp-spotify.html",
1617
apple_music: "<assist_server>/widgets/mp-apple-music.html",
1718
youtube: "<assist_server>/widgets/mp-youtube.html"
@@ -50,19 +51,26 @@ function sepiaFW_build_ui_cards_embed(){
5051
});
5152

5253
//Create media player DOM element
53-
function createMediaPlayerDomElement(id, contentUrl, onLoadHandler){
54+
function createMediaPlayerDomElement(id, contentUrl, isTrusted, onLoadHandler){
5455
//card
5556
var mediaPlayerDiv = document.createElement('div');
5657
mediaPlayerDiv.id = id;
5758
mediaPlayerDiv.className = "embeddedWebPlayer cardBodyItem fullWidthItem";
5859
//iframe
60+
var allowIframe;
61+
//Info: https://developer.mozilla.org/en-US/docs/Web/HTTP/Feature_Policy/Using_Feature_Policy#the_iframe_allow_attribute
62+
if (isTrusted){
63+
allowIframe = 'autoplay *; encrypted-media *;';
64+
}else{
65+
allowIframe = 'encrypted-media *;';
66+
}
5967
var iframe = document.createElement("iframe");
6068
iframe.src = contentUrl;
6169
iframe.width = "100%";
6270
iframe.height = 50; //can be set via postMessage interface
6371
iframe.allowtransparency = true;
6472
iframe.onload = onLoadHandler;
65-
//iframe.allow = ...;
73+
iframe.allow = allowIframe;
6674
//iframe.sandbox = ...;
6775
mediaPlayerDiv.appendChild(iframe);
6876
//loading overlay
@@ -82,6 +90,7 @@ function sepiaFW_build_ui_cards_embed(){
8290
return;
8391
}
8492
var thisPlayer = this;
93+
console.error("TEST", "embedWebPlayer", options); //DEBUG
8594

8695
//ID
8796
var playerId = getNewMediaPlayerId();
@@ -90,12 +99,20 @@ function sepiaFW_build_ui_cards_embed(){
9099
}
91100

92101
//Widget URL
93-
var widgetUrl = options.widgetUrl || playerWidgets[options.widget];
102+
var widgetUrl = (options.widgetUrl || playerWidgets[options.widget]).trim();
94103
widgetUrl = SepiaFW.config.replacePathTagWithActualPath(widgetUrl);
95-
console.error("URL", widgetUrl); //DEBUG
104+
var widgetIsSameOrigin = SepiaFW.tools.isSameOrigin(widgetUrl);
105+
var widgetIsSepiaFileHost = SepiaFW.config.urlIsSepiaFileHost(widgetUrl);
106+
var widgetIsRemote = (widgetUrl.indexOf("http:") == 0) || (widgetUrl.indexOf("https:") == 0) || (widgetUrl.indexOf("ftp:") == 0);
107+
var widgetIsTrusted = widgetIsSameOrigin || widgetIsSepiaFileHost || !widgetIsRemote;
108+
109+
widgetUrl = SepiaFW.tools.setParameterInURL(widgetUrl, "skinStyle", SepiaFW.ui.getSkinStyle());
110+
widgetUrl = SepiaFW.tools.setParameterInURL(widgetUrl, "skinId", SepiaFW.ui.getSkin());
111+
112+
console.error("URL", widgetUrl, "isTrusted", widgetIsTrusted); //DEBUG
96113

97114
//Create card (DOM element)
98-
var mpObj = createMediaPlayerDomElement(playerId, widgetUrl, function(){
115+
var mpObj = createMediaPlayerDomElement(playerId, widgetUrl, widgetIsTrusted, function(){
99116
//on-load
100117
console.error("on-load", playerId); //DEBUG
101118
sendEvent({text: "World Hello"});
@@ -127,7 +144,7 @@ function sepiaFW_build_ui_cards_embed(){
127144
if (ev.size && ev.size.height){
128145
thisPlayer.iframe.style.height = ev.size.height;
129146
}
130-
setTimeout(function(){ $(mpObj.overlay).remove(); }, 500);
147+
setTimeout(function(){ $(mpObj.overlay).hide(); }, 500);
131148
}else if (ev.state == 2){
132149
//on-play
133150
state = ev.state;

www/scripts/sepiaFW.ui.cards.js

Lines changed: 13 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,102 +1266,14 @@ function sepiaFW_build_ui_cards(){
12661266
cardBody.appendChild(linkCardEle);
12671267

12681268
//Experimenting with web players - note: use data.embedded ?
1269-
console.error("TEST", "embedWebPlayer", data); //DEBUG
12701269
var embedWebPlayer = data.type && (data.type == "musicSearch" || data.type == "videoSearch")
12711270
&& data.typeData && Cards.canEmbedWebPlayer(data.typeData.service);
12721271
if (embedWebPlayer){
1273-
var allowIframe;
1274-
if (isSafeSource){
1275-
allowIframe = 'autoplay *; encrypted-media *;';
1276-
}else{
1277-
allowIframe = 'encrypted-media *;';
1278-
}
12791272
//Embedded player
1280-
if (data.brand == "default"){
1281-
addEmbeddedPlayerToCard(cardBody, data.type, data.typeData);
1282-
1283-
//Spotify
1284-
}else if (data.brand == "Spotify"){
1285-
var webPlayerDiv = document.createElement('DIV');
1286-
webPlayerDiv.className = "spotifyWebPlayer embeddedWebPlayer cardBodyItem fullWidthItem";
1287-
var contentUrl = "https://" + linkUrl.replace("spotify:", "open.spotify.com/embed/").replace(":play", "").replace(/:/g, "/").trim();
1288-
webPlayerDiv.innerHTML = '<iframe '
1289-
+ 'src="" width="100%" height="80" frameborder="0" allowtransparency="true" '
1290-
+ 'allow="' + allowIframe + '" '
1291-
+ 'sandbox="allow-forms allow-popups allow-same-origin allow-scripts" ' + '>'
1292-
+ '</iframe>';
1293-
$(webPlayerDiv).find("iframe").attr("src", contentUrl);
1294-
cardBody.appendChild(webPlayerDiv);
1295-
//Apple Music
1296-
}else if (data.brand == "Apple Music"){
1297-
var webPlayerDiv = document.createElement('DIV');
1298-
webPlayerDiv.className = "appleMusicWebPlayer embeddedWebPlayer cardBodyItem fullWidthItem";
1299-
var contentUrl;
1300-
if (linkUrl.indexOf("/artist/") > 0){
1301-
contentUrl = linkUrl.replace(/^https:\/\/.*?\//, "https://geo.itunes.apple.com/");
1302-
}else{
1303-
contentUrl = linkUrl.replace(/^https:\/\/.*?\//, "https://embed.music.apple.com/");
1304-
}
1305-
webPlayerDiv.innerHTML = '<iframe '
1306-
+ 'allow="' + allowIframe + '" '
1307-
+ 'frameborder="0" height="150" '
1308-
+ 'style="width:100%;max-width:660px;overflow:hidden;background:transparent;" '
1309-
+ 'sandbox="allow-forms allow-popups allow-same-origin allow-scripts '
1310-
+ ((SepiaFW.ui.isSafari)? 'allow-storage-access-by-user-activation' : '')
1311-
+ ' allow-top-navigation-by-user-activation" '
1312-
+ 'src="">'
1313-
+ '</iframe>';
1314-
$(webPlayerDiv).find("iframe").attr("src", contentUrl);
1315-
cardBody.appendChild(webPlayerDiv);
1316-
//YouTube
1317-
}else if (data.brand == "YouTube"){
1318-
//console.log('YouTube'); //DEBUG
1319-
var webPlayerDiv = document.createElement('DIV');
1320-
var playerId = currentLinkItemId++;
1321-
webPlayerDiv.className = "youTubeWebPlayer embeddedWebPlayer cardBodyItem fullWidthItem"
1322-
var f = document.createElement('iframe');
1323-
f.id = 'youTubeWebPlayer-' + playerId;
1324-
f.allow = allowIframe;
1325-
f.sandbox = "allow-same-origin allow-scripts allow-presentation allow-popups";
1326-
f.frameBorder = 0;
1327-
f.style.width = "100%"; f.style.height = "280px"; f.style.overflow = "hidden";
1328-
f.style.border = "4px solid"; f.style.borderColor = "#212121";
1329-
if (data.autoplay){
1330-
//console.log('add controls'); //DEBUG
1331-
if (isSafeSource){
1332-
//stop all previous audio first
1333-
if (SepiaFW.client.controls){
1334-
SepiaFW.client.controls.media({
1335-
action: "stop",
1336-
skipFollowUp: true
1337-
});
1338-
}else{
1339-
SepiaFW.audio.stop();
1340-
}
1341-
//add controls and start
1342-
addYouTubeControls(f, true);
1343-
}else{
1344-
//add controls only
1345-
addYouTubeControls(f, false);
1346-
}
1347-
}else{
1348-
//add controls only
1349-
addYouTubeControls(f, false);
1350-
}
1351-
if (linkUrl.indexOf("/embed") < 0){
1352-
//convert e.g.: https://www.youtube.com/results?search_query=purple+haze%2C+jimi+hendrix
1353-
f.src = "https://www.youtube.com/embed?autoplay=1&enablejsapi=1&playsinline=1&iv_load_policy=3&fs=1&listType=search&list="
1354-
+ linkUrl.replace(/.*?search_query=/, "").trim();
1355-
}else{
1356-
f.src = linkUrl; //TODO: fix URL security
1357-
}
1358-
if (!isSafeSource){
1359-
//remove autoplay flag
1360-
f.src = f.src.replace("autoplay=1", "autoplay=0");
1361-
}
1362-
cardBody.appendChild(webPlayerDiv);
1363-
webPlayerDiv.appendChild(f);
1364-
}
1273+
addEmbeddedPlayerToCard(cardBody, data.type, data.typeData, isSafeSource, {
1274+
brand: data.brand,
1275+
autoplay: data.autoplay
1276+
});
13651277
}
13661278

13671279
//link button(s)
@@ -2167,12 +2079,18 @@ function sepiaFW_build_ui_cards(){
21672079
});
21682080
cardBody.appendChild(cardEle);
21692081
}
2170-
function addEmbeddedPlayerToCard(cardBody, type, data){
2171-
//cardBody class should be -> 'sepiaFW-embedded-player-container'
2082+
function addEmbeddedPlayerToCard(cardBody, type, data, isSafeSource, options){
2083+
if (!options) options = {};
2084+
//type: musicSearch, videoSearch (currently same)
2085+
var widget = data.service? data.service.split("_")[0] : "default";
21722086
var player = new SepiaFW.ui.cards.embed.MediaPlayer({
21732087
parentElement: cardBody,
2174-
widget: "default"
2088+
widget: widget,
2089+
autoplay: options.autoplay,
2090+
safeRequest: isSafeSource, //came from assistant or private channel?
2091+
brand: options.brand
21752092
});
2093+
return player;
21762094
}
21772095

21782096
//-- Plot cards --

0 commit comments

Comments
 (0)