Skip to content

Instantly share code, notes, and snippets.

@ymichael
Last active November 16, 2015 03:56
Show Gist options
  • Save ymichael/9017485 to your computer and use it in GitHub Desktop.
Save ymichael/9017485 to your computer and use it in GitHub Desktop.
IVLE Webcast mp4 download links
1. Create a bookmark with url the contents of webcast-bookmarklet.
2. Click on the bookmark when you're on the ivle webcast page.
javascript:(function()%7Bfunction endsWith(str,suffix)%7Breturn str.indexOf(suffix,str.length-suffix.length)!%3D%3D-1%3B%7Dvar getLectureTitle%3Dfunction(row)%7Breturn row.find(%27b%27).html()%3B%7D%3Bvar getAudioUrl%3Dfunction(row)%7Bvar ret%3Brow.find(%27a%27).each(function(index,a)%7Bvar href%3D%24(a).attr(%27href%27)%3Bif(endsWith(href,%27.mp3%27))%7Bret%3Dhref%3Breturn false%3B%7D%7D)%3Breturn ret%3B%7D%3Bvar getLecturer%3Dfunction(row)%7Bvar ret%3Brow.find(%27span%27).each(function(index,span)%7Bvar id%3D%24(span).attr(%27id%27)%3Bif(endsWith(id,%27Name%27))%7Bret%3D%24(span).html()%3Breturn false%3B%7D%7D)%3Breturn ret%3B%7D%3Bvar getLectureDateTime%3Dfunction(row)%7Bvar ret%3Brow.find(%27span%27).each(function(index,span)%7Bvar id%3D%24(span).attr(%27id%27)%3Bif(endsWith(id,%27LectureDate%27))%7Bret%3D%24(span).html()%3Breturn false%3B%7D%7D)%3Breturn ret%3B%7D%3Bvar WebCast%3Dfunction(title,mp3_url,lecturer,datetime)%7Bthis.title%3Dtitle%3Bthis.mp3_url%3Dmp3_url%3Bthis.lecturer%3Dlecturer%3Bthis.datetime%3Ddatetime%3B%7D%3BWebCast.prototype.mp4%3Dfunction()%7Breturn this.mp3_url.replace(/.mp3%24/,%27.mp4%27)%3B%7D%3BWebCast.prototype.filename%3Dfunction()%7Bvar fn%3Dthis.title.replace(/%5Cs/g,%27_%27)%3Breturn fn%2B%27.mp4%27%3B%7D%3BWebCast.prototype.html%3Dfunction()%7Bvar button_styles%3D"cursor: pointer%3B display: inline-block%3B vertical-align: middle%3Bmin-height: 1em%3B outline: none%3B border: none%3B background-color: %23FAFAFA%3Bcolor: %23808080%3Bmargin: 0em%3Bpadding: 0.8em 1.5em%3Bfont-size: 1rem%3Btext-transform: uppercase%3Bline-height: 1%3Bfont-weight: bold%3Bfont-style: normal%3Btext-align: center%3Btext-decoration: none%3B background-image: linear-gradient(rgba(0, 0, 0, 0), rgba(0, 0, 0, 0.05))%3B -webkit-border-radius: 0.25em%3B -moz-border-radius: 0.25em%3B border-radius: 0.25em%3B box-shadow: 0px 0px 0px 1px rgba(0, 0, 0, 0.08) inset%3B"%3Bbutton_styles%2B%3D"position:absolute%3Btop:5px%3Bright:5px"%3Breturn%5B"<div style%3D%27padding:10px 0%3Bposition:relative%3Bborder-top: solid 1px %23eee%3B%27>","<strong>",this.title,"</strong>","<p>",this.datetime,"<br/>",this.lecturer,"</p>","<a style%3D%27",button_styles,"%27 href%3D%27",this.mp4(),"%27, download%3D%27",this.filename(),"%27>","Download MP4 %26%23x25BC","</a>","</div>",%5D.join(%27%27)%3B%7D%3B(function()%7Bif(window.location.host.toLowerCase()!%3D%3D"ivle.nus.edu.sg"%7C%7Cwindow.location.pathname.toLowerCase()!%3D%3D"/media/multimedia.aspx")%7Bconsole.log(%27asdf%27)%3Breturn%3B%7Dvar webcasts%3D%5B%5D%3B%24(".dataGridCtrl-Item, .dataGridCtrl-Alter").each(function(index,elem)%7Bvar %24elem%3D%24(elem)%3Bvar title%3DgetLectureTitle(%24elem)%3Bvar mp3_url%3DgetAudioUrl(%24elem)%3Bvar lecturer%3DgetLecturer(%24elem)%3Bvar datetime%3DgetLectureDateTime(%24elem)%3Bwebcasts.unshift(new WebCast(title,mp3_url,lecturer,datetime))%3B%7D)%3Bvar %24modal%3D%24("<div>")%3B%24modal.css(%7B%27position%27:%27fixed%27,%27padding%27:20,%27background-color%27:%27%23FFF%27,%27border%27:%27solid 1px %23ddd%27,%27top%27:%2710%25%27,%27overflow-y%27:%27auto%27,%27left%27:%2710%25%27,%27z-index%27:999,%27width%27:"80%25",%27max-height%27:%2780%25%27%7D)%3B%24modal.append("<h1>Webcasts</h1>")%3Bwebcasts.forEach(function(webcast)%7B%24modal.append(webcast.html())%3B%7D)%3B%24(%27body%27).append(%24modal)%3B%7D)()%3B%7D)()%3B
// http://stackoverflow.com/questions/280634/endswith-in-javascript
function endsWith(str, suffix) {
return str.indexOf(suffix, str.length - suffix.length) !== -1;
}
var getLectureTitle = function(row) {
return row.find('b').html();
};
var getAudioUrl = function(row) {
var ret;
row.find('a').each(function(index, a) {
var href = $(a).attr('href');
if (endsWith(href, '.mp3')) {
ret = href;
return false;
}
});
return ret;
};
var getLecturer = function(row) {
var ret;
row.find('span').each(function(index, span) {
var id = $(span).attr('id');
if (endsWith(id, 'Name')) {
ret = $(span).html();
return false;
}
});
return ret;
};
var getLectureDateTime = function(row) {
var ret;
row.find('span').each(function(index, span) {
var id = $(span).attr('id');
if (endsWith(id, 'LectureDate')) {
ret = $(span).html();
return false;
}
});
return ret;
};
var WebCast = function(title, mp3_url, lecturer, datetime) {
this.title = title;
this.mp3_url = mp3_url;
this.lecturer = lecturer;
this.datetime = datetime;
};
// http://slyj.blogspot.sg/2013/10/downloading-webcast-from-ivle.html
WebCast.prototype.mp4 = function() {
return this.mp3_url.replace(/.mp3$/, '.mp4');
};
WebCast.prototype.filename = function() {
var fn = this.title.replace(/\s/g, '_');
return fn + '.mp4';
};
WebCast.prototype.html = function() {
// Copied from semantic ui.
var button_styles = "cursor: pointer; display: inline-block; vertical-align: middle;min-height: 1em; outline: none; border: none; background-color: #FAFAFA;color: #808080;margin: 0em;padding: 0.8em 1.5em;font-size: 1rem;text-transform: uppercase;line-height: 1;font-weight: bold;font-style: normal;text-align: center;text-decoration: none; background-image: linear-gradient(rgba(0, 0, 0, 0), rgba(0, 0, 0, 0.05)); -webkit-border-radius: 0.25em; -moz-border-radius: 0.25em; border-radius: 0.25em; box-shadow: 0px 0px 0px 1px rgba(0, 0, 0, 0.08) inset;";
button_styles += "position:absolute;top:5px;right:5px";
return [
"<div style='padding:10px 0;position:relative;border-top: solid 1px #eee;'>",
"<strong>", this.title, "</strong>",
"<p>", this.datetime,
"<br/>",
this.lecturer, "</p>",
"<a style='",
button_styles,
"' href='", this.mp4(), "', download='", this.filename() ,"'>",
"Download MP4 &#x25BC",
"</a>",
"</div>",
].join('');
};
(function() {
// Do nothing if we are not on the correct page.
if (window.location.host.toLowerCase() !== "ivle.nus.edu.sg" ||
window.location.pathname.toLowerCase() !== "/media/multimedia.aspx") {
console.log('asdf');
return;
}
var webcasts = [];
$(".dataGridCtrl-Item, .dataGridCtrl-Alter").each(function(index, elem) {
var $elem = $(elem);
var title = getLectureTitle($elem);
var mp3_url = getAudioUrl($elem);
var lecturer = getLecturer($elem);
var datetime = getLectureDateTime($elem);
webcasts.unshift(new WebCast(title, mp3_url, lecturer, datetime));
});
var $modal = $("<div>");
$modal.css({
'position': 'fixed',
'padding': 20,
'background-color': '#FFF',
'border': 'solid 1px #ddd',
'top': '10%',
'overflow-y': 'auto',
'left': '10%',
'z-index': 999,
'width': "80%",
'max-height': '80%'
});
$modal.append("<h1>Webcasts</h1>");
webcasts.forEach(function(webcast) {
$modal.append(webcast.html());
});
$('body').append($modal);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment