Last active
August 29, 2015 14:08
-
-
Save jellelicht/c216a4b49edad8fdeca4 to your computer and use it in GitHub Desktop.
Download-button generating userscript for CR@TU-Delft
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name TUDelft collegerama downloader | |
// @namespace http://your.homepage/ | |
// @version 0.1 | |
// @description enter something useful | |
// @author You | |
// @match https://collegerama.tudelft.nl/Mediasite/Play/* | |
// @require http://code.jquery.com/jquery-latest.js | |
// @grant none | |
// ==/UserScript== | |
$(document).ready(function() { | |
main(generateMessage(document.location.href)); | |
}); | |
function make_button(href){ | |
$('body').append('<a href="' + href + '"><input type="button" value="Download as WMV!" id="CP"></a>'); | |
$("#CP").css("position", "fixed").css("top", 0).css("left", 0); | |
} | |
// Helper functions to generate the correct POST-api link | |
var extractResourceId, extractQueryString; | |
(function(){ | |
var parser = document.createElement('a'); | |
extractResourceId = function(url){ | |
parser.href = url; | |
ph = parser.pathname.split('/'); | |
return ph.slice(-1)[0]; | |
} | |
extractQueryString = function(url){ | |
parser.href = url; | |
return parser.search; | |
} | |
})(); | |
// Generate the correct PlayerOptions request message | |
function generateMessage(url){ | |
rid = extractResourceId(url); | |
qs = extractQueryString(url); | |
var msg = { | |
getPlayerOptionsRequest: { | |
ResourceId: rid, | |
QueryString: qs | |
} | |
}; | |
return JSON.stringify(msg); | |
} | |
// Extract the video location from the PlayerOptions Response | |
function parseRequest(json_data){ | |
var vid_location = json_data.d.Presentation.Streams[0].VideoUrls[1].Location; | |
return vid_location; | |
} | |
// Place request and place button with parsed response | |
function main (message) { | |
var client = new XMLHttpRequest(); | |
client.open("POST", "https://collegerama.tudelft.nl/Mediasite/PlayerService/PlayerService.svc/json/GetPlayerOptions"); | |
client.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); | |
client.onload = function () { | |
var response = JSON.parse(this.responseText); | |
var vid_location = parseRequest(response); | |
make_button(vid_location); | |
}; | |
client.send(message); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment