Facebook HD Video Downloader

Adds a download link for Facebook videos. Works for HD videos as of July 2014. Fork from styfle.

2014-07-21 يوللانغان نەشرى. ئەڭ يېڭى نەشرىنى كۆرۈش.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name			Facebook HD Video Downloader
// @description		Adds a download link for Facebook videos. Works for HD videos as of July 2014. Fork from styfle.
// @author			EThaiZone
// @include     http://facebook.com/photo.php*
// @include     http://*.facebook.com/photo.php*
// @include     https://facebook.com/photo.php*
// @include     https://*.facebook.com/photo.php*
// @include     http://facebook.com/video/*
// @include     http://*.facebook.com/video/*
// @include     https://facebook.com/video/*
// @include     https://*.facebook.com/video/*
// @version 0.0.1.20140721131359
// @namespace https://greasyfork.org/users/3747
// ==/UserScript==

document.addEventListener ("readystatechange", renderFBDownloader, true);

function renderFBDownloader() { 

    // Get the side bar so we can append to it later
    var sidebar = document.getElementById('fbPhotoPageActions');

    // Get all the <embed> elements
    var embedElements = document.querySelectorAll('embed[flashvars]');

    // Flag if we found the video url or not
    var found = false;

    for (var i = 0; i < embedElements.length; i++) {
        // Get the flashvars attribute and decode it
        var flashvars = decodeURIComponent(embedElements[i].getAttribute('flashvars'));

        // Check if this string contains the code we're looking for
        var hd_src_index = flashvars.indexOf('hd_src');
        var p_width_index = flashvars.indexOf('&width=');
        if (hd_src_index > -1 && p_width_index > -1) {
            // This string contains the payload we are looking for so parse it
            var obj = JSON.parse(flashvars.slice(7, p_width_index));
            var video_data = obj.video_data[0];

            // High Def
            var hd_link = document.createElement('a');
            hd_link.href = video_data.hd_src;
            hd_link.innerHTML = 'Download HD Video';
            hd_link.className = 'fbPhotosPhotoActionsItem';
            hd_link.download = video_data.video_id + '_hd.mp4';

            // Low Def
            var sd_link = document.createElement('a');
            sd_link.href = video_data.sd_src;
            sd_link.innerHTML = 'Download SD Video';
            sd_link.className = 'fbPhotosPhotoActionsItem';
            sd_link.download = video_data.video_id + '_sd.mp4';

            // Append both links
            sidebar.appendChild(hd_link);
            sidebar.appendChild(sd_link);

            found = true;
        } // end if

    } // end loop

    if (!found) {
        var not_found = document.createElement('span');
        not_found.innerHTML = 'No download link :(';
        sidebar.appendChild(not_found);
    }
 
}