Skip to content

Instantly share code, notes, and snippets.

@aneelkkhatri
Last active January 19, 2022 22:44
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • 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);
@Marcelo-Machado
Copy link

como usa este script?

@42RK
Copy link

42RK commented Jul 29, 2017

very nice, you should add the userscript header and post it on greasyfork

@CodeDotJS
Copy link

I've created a simpleAPI and a command line tool for the same thing in Node.
Sharing it, just in case if anyone cares -

@sp00n
Copy link

sp00n commented May 1, 2018

Hm, it doesn't seem to work anymore. At least it doesn't find any <script> that contains the "video_ids" string for me, so the regexp never evaluates.

@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