Facebook HD Video Downloader

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

目前為 2015-04-25 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==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.php*
// @include     http://*.facebook.com/video.php*
// @include     https://facebook.com/video.php*
// @include     https://*.facebook.com/video.php*
// @include     http://facebook.com/photo/*
// @include     http://*.facebook.com/photo/*
// @include     https://facebook.com/photo/*
// @include     https://*.facebook.com/photo/*
// @include     http://facebook.com/video/*
// @include     http://*.facebook.com/video/*
// @include     https://facebook.com/video/*
// @include     https://*.facebook.com/video/*
// @version 0.0.1.20150425114929
// @namespace https://greasyfork.org/users/3747
// ==/UserScript==

(function () {

    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];
                
                //var title = video_data.video_id;
                var title = document.querySelectorAll('h2.uiHeaderTitle')[0].innerText;

                // High Def
                if (video_data.hd_src)
                {
                    var hd_link = document.createElement('a');
                    hd_link.href = video_data.hd_src.replace("https://", "http://");
                    hd_link.innerHTML = 'Download HD Video';
                    hd_link.className = 'fbPhotosPhotoActionsItem';
                    hd_link.download = title + '_hd.mp4';
                    sidebar.appendChild(hd_link);
                }

                // Low Def
                if (video_data.sd_src)
                {
                    var sd_link = document.createElement('a');
                    sd_link.href = video_data.sd_src.replace("https://", "http://");
                    sd_link.innerHTML = 'Download SD Video';
                    sd_link.className = 'fbPhotosPhotoActionsItem';
                    sd_link.download = title + '_sd.mp4';
                    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);
        }
    }

    function doExec() {
        try {
            log("Find flashvars.");
            var embedElements = document.querySelectorAll('embed[flashvars]');

            if (embedElements.length > 0) {
                renderFBDownloader();
                log("Video links rendered.");
            } else {
                setTimeout(doExec, 200);
                log("Try again.");
            }
        } catch(e) {
            //setTimeout(doExec, 1000);
        }
    }

    function log(msg) {
        // console.log("[FB Video Downloader] " + msg);
    }

    log("First Start.");
    doExec();
})();