Greasy Fork

Shined up real nice.

Facebook HD Video Downloader (Not working!)

(Not working! Read more at Additional info.) Adds a download link for Facebook videos. Works for HD videos.

After trying this script, you can ask a question about it, review it, or report it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// ==UserScript==
// @name            Facebook HD Video Downloader (Not working!)
// @description     (Not working! Read more at Additional info.) Adds a download link for Facebook videos. Works for HD videos.
// @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.8
// @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 1s, max-width 1s ease-in-out;\
    -moz-transition: opacity 1s, max-width 1s ease-in-out;\
    -webkit-transition: opacity 1s, max-width 1s ease-in-out;\
    color: black !important;\
    text-decoration: none;\
    display: block;\
    height: 1.2em;\
    overflow: hidden;\
    text-align: center;\
    max-width: 1em;\
    white-space: nowrap;\
}\
span[display=\"inline\"] .dl_container {\
    margin: 5px 30px 0 0;\
    display: none !important;\
}\
.dl_container:hover {\
    max-width: 140px;\
    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');
                    insertAfter(link[0], videoElements[i]);
                } 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);
    }
})();