Last active
April 26, 2025 11:28
-
-
Save aneelkkhatri/cc3818cf94a896a47056 to your computer and use it in GitHub Desktop.
Script for downloading Facebook videos
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
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); |
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.
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
I've created a simple
API
and acommand line tool
for the same thing in Node.Sharing it, just in case if anyone cares -
fbvideos
api
fbdl
cli