Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bbarrows/028e8484447b541914b9a02820714a0c to your computer and use it in GitHub Desktop.
Save bbarrows/028e8484447b541914b9a02820714a0c to your computer and use it in GitHub Desktop.
Downloads an embedded Vimeo video
downloadEmbededVideos();
/**
* Finds and downloads all embeded Vimeo videos.
*/
function downloadEmbededVideos() {
// Find Vimeo embed frame
var embedFrames = document.querySelectorAll('iframe[src*="player.vimeo.com"]');
// No embed frames found?
if (!embedFrames.length) {
console.error("Failed to identify embeded video frame");
return;
}
// Retrieve embed source
for (var i = 0; i < embedFrames.length; i++) {
console.log('Requesting source for embedded video: ', embedFrames[i]);
getSource(embedFrames[i].src, function(response) {
var mp4Url = findMp4Url(response);
console.log('Downloading "' + mp4Url + '"...');
downloadFile(mp4Url);
});
}
}
/**
* Retrieves content source at given URL.
* @param {string} url URL from which content source will be retrieved
* @param {function} callback Called with the retrieved source on successful retrieval
*/
function getSource(url, callback) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
callback(xhr.responseText);
}
};
xhr.open("GET", url, true);
xhr.send(null);
};
/**
* Finds mp4 URL within Vimeo embed source.
* @param {string} source Source to search for mp4 URL
* @return {string} The mp4 URL
*/
function findMp4Url(source) {
return source.match(/"url"\s*:\s*"(https?:\/\/[^"]+\.vimeocdn\.com\/[^"]+\.mp4(\?([^"]+=[^"]+;?)+)?)"/)[1];
}
/**
* Downloads the file at a given URL.
* @param {string} url The URL of the file to download
*/
function downloadFile(url) {
var filename = url.substring(url.lastIndexOf("/") + 1).split("?")[0];
var xhr = new XMLHttpRequest();
xhr.responseType = 'blob';
xhr.onload = function() {
var a = document.createElement('a');
a.href = window.URL.createObjectURL(xhr.response);
a.download = filename;
a.style.display = 'none';
document.body.appendChild(a);
a.click();
delete a;
};
xhr.open('GET', url);
xhr.send();
}
javascript: (function(){function b(){var a=document.querySelectorAll('iframe[src*="player.vimeo.com"]');if(!a.length)return void console.error("Failed to identify embeded video frame");for(var b=0;b<a.length;b++)console.log("Requesting source for embedded video: ",a[b]),c(a[b].src,function(a){var b=d(a);console.log('Downloading "'+b+'"...'),e(b)})}function c(a,b){var c=new XMLHttpRequest;c.onreadystatechange=function(){4==c.readyState&&200==c.status&&b(c.responseText)},c.open("GET",a,!0),c.send(null)}function d(a){return a.match(/"url"\s*:\s*"(https?:\/\/[^"]+\.vimeocdn\.com\/[^"]+\.mp4(\?([^"]+=[^"]+;?)+)?)"/)[1]}function e(a){var b=a.substring(a.lastIndexOf("/")+1).split("?")[0],c=new XMLHttpRequest;c.responseType="blob",c.onload=function(){var a=document.createElement("a");a.href=window.URL.createObjectURL(c.response),a.download=b,a.style.display="none",document.body.appendChild(a),a.click(),delete a},c.open("GET",a),c.send()}b()})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment