project Websites / videos.bentasker.co.uk avatar

Websites / videos.bentasker.co.uk: 9d199e2e




VID-10 Implemented token acquisition support in the embed javascript

VID-10 Implemented token acquisition support in the embed javascript

When the player is first instantiated, it places a request to /token_endpoint, providing the referrer and the video to be played

If a token is not acquired, then the player is not loaded, and an image is instead written into the page to report that the video could not be played.

If a token is acquired, then the token (and specified expiry time) are appended to the video URL as a query string

Commit 9d199e2e.

Authored 2019-04-15T12:37:56.000+01:00 by Ben Tasker in project Websites / videos.bentasker.co.uk

+65 lines -2 lines

Changes

diff --git a/resources/embed/embed.js b/resources/embed/embed.js
--- a/resources/embed/embed.js
+++ b/resources/embed/embed.js
# @@ -65,7 +65,58 @@ function embedBensPlayer(vidurl,vidtype){
#
#
# // We then trigger the next steps in stages to make sure everything that needs to be loaded, is
# - loadVJS(vidid,playerSettings);
# + getBensToken(vidid,playerSettings);
# +}
# +
# +
# +function getBensToken(vidid,playerSettings){
# + // VID-10 Tokenisation support
# +
# + var ref = encodeURIComponent(window.location.href);
# + var vurl = encodeURIComponent(playerSettings['vidurl']);
# + var url = "https://videos.bentasker.co.uk/token_endpoint?ref="+ref+"&vidpath="+vurl;
# +
# +
# + var xmlhttp;
# + if (window.XMLHttpRequest){
# + // code for IE7+, Firefox, Chrome, Opera, Safari
# + xmlhttp=new XMLHttpRequest();
# + }else{
# + // code for IE6, IE5 (why am I still supporting these? Die... Die.... Die....
# + xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
# + }
# +
# + xmlhttp.onreadystatechange=function(){
# + if (xmlhttp.readyState==4){
# + if(xmlhttp.status==200){
# + // OK, we've got a token back, so now we need to deal with it
# + var j = JSON.parse(xmlhttp.responseText);
# + if (!j['status']){
# + console.log("Failed to retrieve token for video");
# + embedFailedNotif(vidid)
# + return false;
# + }
# +
# + // Append the token onto the URL
# + playerSettings['vidurl'] += '?t=' + encodeURIComponent(j['token']) + '&e=' + encodeURIComponent(j['exp']);
# +
# + // Trigger the next stage of loading
# + loadVJS(vidid,playerSettings);
# +
# + }else{
# + console.log("Failed to retrieve token for video");
# + // Should probably write an error code into the player rather than just dying
# + embedFailedNotif(vidid)
# + return false;
# + }
# +
# + }
# +
# + }
# +
# + xmlhttp.open("GET",url,true);
# + xmlhttp.send();
# +
# }
#
#
# @@ -112,7 +163,7 @@ function writePlayer(vidid,playerSettings){
# var s = cE('script');
# s.setAttribute('type','text/javascript');
# ss = "window.player" + vidid +" = videojs('BensVideo" + vidid + "',\"{'fluid':true}\", function onPlayerReady(){console.log('Player Ready');});";
# - ss += "window.player" + vidid + ".on('playing', function() { var i = document.createElement('img'); i.src = 'https://piwik.bentasker.co.uk/piwik.php?idsite=10&rec=1&new_visit=1&url=' + encodeURIComponent(window.player" + vidid+ ".currentSrc()) + '&urlref=' + encodeURIComponent(window.location.href); i.style='border:0';document.body.appendChild(i);});";
# + ss += "window.player" + vidid + ".on('playing', function() { var i = document.createElement('img'); i.src = 'https://piwik.bentasker.co.uk/piwik.php?idsite=10&rec=1&new_visit=1&url=' + encodeURIComponent(window.player" + vidid+ ".currentSrc().split('?')[0]) + '&urlref=' + encodeURIComponent(window.location.href); i.style='border:0';document.body.appendChild(i);});";
# s.innerHTML = ss;
#
# c.appendChild(s);
# @@ -174,6 +225,18 @@ function fetchPage(url,callback,errcallback,storage){
# }
#
#
# +function embedFailedNotif(vidid){
# + s = cE('img');
# + s.setAttribute('src','https://videos.bentasker.co.uk/resources/embed/failed.png');
# +
# + var c = document.getElementById('BensplayerWrapper'+vidid);
# + var width = c.parentNode.clientWidth
# +
# + s.setAttribute('style','max-width: ' + width + 'px');
# + c.appendChild(s);
# +}
# +
# +
# function BenswriteSchemaOrg(text,storage,url){
#
#
#
diff --git a/resources/embed/failed.png b/resources/embed/failed.png
--- a/resources/embed/failed.png
+++ b/resources/embed/failed.png
# Binary files /dev/null and b/resources/embed/failed.png differ
#