Facebook HD Video Downloader

Adds a download link for Facebook videos. Works for HD videos. NEW!! Work on all page.

当前为 2016-07-11 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name            Facebook HD Video Downloader
// @description     Adds a download link for Facebook videos. Works for HD videos. NEW!! Work on all page.
// @author          EThaiZone
// @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/video/*
// @include     http://*.facebook.com/video/*
// @include     https://facebook.com/video/*
// @include     https://*.facebook.com/video/*
// @include     http://facebook.com/*/videos/*
// @include     http://*.facebook.com/*/videos/*
// @include     https://facebook.com/*/videos/*
// @include     https://*.facebook.com/*/videos/*
// @include     https://*.facebook.com/*
// @version 0.1.6.5
// @namespace https://greasyfork.org/users/3747
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// ==/UserScript==


// My code
(function () {

    function insertAfter(newNode, referenceNode) {
        referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
    }
    
    if (typeof jQuery == 'undefined') {
        alert('[Facebook HD Video Downloader]\n\nYour greasemonkey or tampermonkey is too old. Please update.');
        return;
    }

    var patternfbh = /facebook\.com\/(.*?)/;
    if (! document.URL.match(patternfbh)) {
        return;
    }

    jQuery('head').append('<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css">');

var css = "\
.dl {\
    display: inline-block;\
    width: 1em;\
    height: 1em;\
}\
.dl_container {\
    z-index: 9999;\
    position: relative;\
    float: right;\
    margin: 5px 5px 0 0;\
    padding: 2px 4px 2px 4px;\
    background: white;\
    border-radius: 4px;\
    opacity: 0.5;\
    transition: opacity .25s ease-in-out;\
    -moz-transition: opacity .25s ease-in-out;\
    -webkit-transition: opacity .25s ease-in-out;\
    color: black !important;\
    text-decoration: none;\
    display: block;\
}\
span[display=\"inline\"] .dl_container {\
    margin: 5px 30px 0 0;\
    display: none !important;\
}\
.dl_container:hover {\
    opacity: 1;\
    text-decoration: none;\
}\
";

    jQuery('head').append('<style type="text/css">' + css + '</style>');

    var iconDownload = jQuery('<div/>');
    iconDownload = jQuery('<i class="fa fa-download dl"></i>');
    
    function renderFBDownloader(counter) {

        // Get the base so we can append to it later
        var base = jQuery('.comment_link').parent('span').parent('div');
        if (base.length === 0) {
            base = jQuery('.UFILikeLink').parent('div').parent('span').parent('div'); // check link like 
        }
        if (base.length === 0) {
            base = jQuery('.userContent'); // or else I will attach to userContent.
        }
        base = base[0];

        /*
         * NOTE: Don't use jquery on <video>, it will broke them.
         */

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

        for (var i = 0; i < videoElements.length; i++) {

            var embed = videoElements[i].querySelectorAll('embed[flashvars]');

            if (embed.length === 0) {
                continue;
            }

            videoElements[i].setAttribute('fb_download_link_ethaizone', '1');

            embed = embed[0];

            // Get the flashvars attribute and decode it
            var flashvars = decodeURIComponent(embed.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));

                if (typeof obj.video_data.progressive == 'undefined') {
                    log('Something wrong in obj.');
                    console.log(obj);
                    continue;
                }
                var video_data = obj.video_data.progressive;

                //console.log(video_data);
                
                if (typeof video_data.video_id == 'undefined') {
                    console.log(video_data);
                } else {
                    var title = video_data.video_id;
                }
                //var title = document.querySelectorAll('h2.uiHeaderTitle')[0].innerText;

                // thank css style from fork of nhtera.
                var link = jQuery('<a/>');
                link.addClass('dl_container');
                link.addClass('fbPhotosPhotoActionsItem');
                link.addClass('fb_download_link_ethaizone');
                link.attr('fb_download_link_ethaizone');
                link.append(iconDownload[0].outerHTML);
                link.append('&nbsp;');

                var labelText = null;
                var downloadUrl = null;
                var filename = null;

                // High Def
                if (typeof video_data.hd_src != 'undefined' && video_data.hd_src) {
                    labelText = 'Download (HD)';
                    filename = title + '_hd.mp4';
                    downloadUrl = video_data.hd_src;
                } else if (typeof video_data.sd_src != 'undefined' && video_data.sd_src) {
                    labelText = 'Download (SD)';
                    filename = title + '_sd.mp4';
                    downloadUrl = video_data.sd_src;
                }

                if (downloadUrl) {
                    link.append('<span class="status">' + labelText + '</span>');
                    link.prop('download', filename);
                    link.prop('href', downloadUrl);
                    link.prop('target', '_blank');
                    // link.hide();
                    // insertAfter(link[0], videoElements[i]);
                    // videoElements[i].parentElement.addEventListener("mouseover", function(){
                    //     link.show();
                    // });
                    // videoElements[i].parentElement.addEventListener("mouseout", function(){
                    //     link.hide();
                    // });
                } else {
                    log('Something wrong in video_data.');
                    console.log(video_data);
                    continue;
                }

                log('Success.');
            } // end if

        } // end loop

        return videoElements.length;
    }

    var counter = 0;
    var delay = 1500;
    function doExec() {
        if (jQuery('#mainContainer').length < 1) {
            // log('No maincontaimer.');
            return false;
        }
        counter++;
        try {
            var found = renderFBDownloader(counter);
            // if (found !== 0 && delay == 1000) {
            //     delay = 3000;
            //     log('First found. Decrease delay. ('+ delay +')');
            // }

            // if (counter > 10 && delay == 1000) {
            //     delay = 5000;
            //     log('Too long and not found anything. Decrease delay. ('+ delay +')');
            // }
            // if (counter < 30) {
            //     setTimeout(doExec, delay);
            // }
            setTimeout(doExec, delay);
            // log('Check!! No:'+counter+' Found: ' + renderFBDownloader(counter));
        } catch(e) {
            log("Found error!");
            console.log(e);
            //setTimeout(doExec, 1000);
        }

        return true;
    }

    function log(msg) {
        //alert(msg);
        console.log("[FB Video Downloader] " + msg);
    }
    if (doExec()) {
        var myVersion = GM_info.script.version;
        log("First start. Version "+myVersion);
    }
})();