Skip to content

Instantly share code, notes, and snippets.

@aneelkkhatri
Last active September 22, 2024 02:41
Show Gist options
  • Save aneelkkhatri/cc3818cf94a896a47056 to your computer and use it in GitHub Desktop.
Save aneelkkhatri/cc3818cf94a896a47056 to your computer and use it in GitHub Desktop.
Script for downloading Facebook videos
var LINK_TYPE_SD = 'sd_src_no_ratelimit';
var LINK_TYPE_HD = 'hd_src_no_ratelimit';
(function downloadVideo(type) {
function getMyObject(doc) {
var scriptsCollection = doc.getElementsByTagName("script");
var scripts = [];
var regExp = /video_ids/i;
for (var i = scriptsCollection.length - 1; i >= 0; i--) {
var script = scriptsCollection[i].innerHTML;
if (regExp.test(script)) {
scripts.push(script);
};
};
var videoData = scripts[0].match(/"?videoData"?:(\[\{[^}]*\}\])/g).map(function(d) {
return eval(d.match(/"?videoData"?:(\[\{[^}]*\}\])/)[1])[0];
});
var paramsObject = {
videoData: videoData
}
return {
paramsObjects: [paramsObject],
};
};
function getDownloadLink(doc, video_id, type) {
var myObject = getMyObject(doc);
var dwLinks = myObject.paramsObjects[0].videoData.filter(function(video){return video.video_id == video_id;})[0];
return dwLinks[type];
};
function download(type) {
var videoId = document.location.href.match(/https?:\/\/www\.facebook\.com\/[^/]+\/videos\/([^/]+)/)[1];
var link = getDownloadLink(document, videoId, type);
var a = document.createElement('a');
a.href = link;
a.setAttribute('download', 'download');
a.click();
};
return download(type);
})(LINK_TYPE_SD);
@jasp402
Copy link

jasp402 commented Jun 16, 2021

VM57852:16 Uncaught TypeError: Cannot read property 'match' of undefined
at getMyObject (:16:36)
at getDownloadLink (:29:24)
at download (:36:20)
at downloadVideo (:44:12)
at :45:3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment