CryptalEquine / Lecturio Video File Downloader

// ==UserScript==
// @name          Lecturio Video File Downloader
// @namespace     ld
// @description   Creates a download link for direct video files. Right click and Save As to save the video file itself.
// @include       *lecturio.com*
// @require       https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js
// @require       https://gist.githubusercontent.com/BrockA/2625891/raw/9c97aa67ff9c5d56be34a55ad6c18a314e5eb548/waitForKeyElements.js
// @version       1.0.2
// @run-at 		   document-end
// @noframes	   true
// @grant         unsafeWindow
// @grant    	   GM_log
// @license       MIT
// @updateURL     https://openuserjs.org/meta/CryptalEquine/Lecturio_Video_File_Downloader.meta.js
// ==/UserScript==


// Helper functions
String.prototype.contains = function(e) { return this.indexOf(e) > -1; };


// Video page
waitForKeyElements("#lecturio_player_media video", function(e)
{
   var videoID = e.attr('src') || null;
   var title = $('h1.page-heading').contents().not($('h1.page-heading').children()).text().trim();
   
   if (videoID)
   {
      var downloadLinkElement = '<a download="' + title + '.mp4" id="downloadVideoFile">Direct video file link</a>';
      var videoLink = videoID.replace('500.mp4', '700.mp4'); //.match(/[A-Za-z0-9\-\:\\\/\.\_]+/g)[0] to get the non tokenised URL
      $('#lecture-player').append(downloadLinkElement);
      $("#downloadVideoFile").attr('href', videoLink);
   }
}, true);


// Course outline page
waitForKeyElements("ul#course-content", function(e)
{
   var subjectField = location.href.replace('.course#tab/videos', '').split('/').slice(-1)[0];
   var videoFileLinks = [], jsonRequests = [];
   var videoLinks = $(e).find('li');
   
   function getNumber(n, l)
   {
      n = n + 1;
      return n < 10 && l > 9 ? (l > 99 ? '00' : '0') + n : n;
   }

   for (var i = 0; i < videoLinks.length; i++)
   {
      var v = videoLinks.eq( i ).attr( 'data-url' ).split( '/' ).slice( -1 )[ 0 ].replace( '.lecture', '' ).replace( '?autostart=1', '' );
      videoFileLinks.push({ name: videoLinks.eq(i).find('a span').eq(0).text() });
      
      (function(index) {
         jsonRequests.push(
            $.ajax({
               url: 'https://app.lecturio.com/api/en/v7/html5/course/' 
                     + subjectField + '/lecture/' + v.replace( '.lecture', '' ) + '.json'
               ,method: 'GET'
               ,contentType: 'application/json'
            }).done( function(data)
            {
               videoFileLinks[index].url = data.content.media[1].file;
            })
         );
      })(i);
   }

   $.when.apply(undefined, jsonRequests).then(function()
   {
      for (var i = 0; i < videoFileLinks.length; i++)
      {
         var fsName = getNumber(i, videoFileLinks.length) + '. ' + videoFileLinks[i].name.replace(/\:/g, ' -');
         videoFileLinks[i].filesafeName = fsName;
         
         var dl = '<a download="' + fsName + '.mp4" id="dlvf' + i + '">Download video file</a>';
         videoLinks.eq(i).append(dl);
         $("#dlvf" + i).attr('href', videoFileLinks[i].url);
      }
      
      console.log(JSON.stringify(videoFileLinks));
   });
   
}, true);