Advertisement
Guest User

Vimeo jQuery Downloader v1.0 (Source)

a guest
Sep 5th, 2014
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********************************
  2.     Vimeo jQuery Downloader
  3.     v1.0
  4.     9/5/2014
  5.    
  6.     Gets download links from
  7.     Vimeo videos
  8. ********************************/
  9. var jq_config = {
  10.     jquery_url: "//ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js",
  11.     info: "Vimeo jQuery Downloader v1.0",
  12.    
  13.     vimeo_codec: 'h264',
  14.     video_types: {'hd': 'HD MP4', 'sd': 'SD MP4'},
  15.    
  16.     failed_tries: 0
  17. };
  18.  
  19. // Main function
  20. function main(){
  21.     if (window.location.hostname != 'vimeo.com'){
  22.         alert("This script will only work on vimeo.com!");
  23.         return;
  24.     }
  25.    
  26.     // Need to get the configUrl data from the page
  27.     // before it's removed from the DOM by Vimeo's scripts
  28.     $.ajax({
  29.         url: window.location.href,
  30.         type: 'GET',
  31.         async: true,
  32.         error: function(data){
  33.             modal.open("Error", "There was an error! Details printed in the browser console.");
  34.             console.log(data);
  35.             return;
  36.         },
  37.         success: function(data){
  38.             // If there's no return data, try again (10 tries max, .4 second intervals)
  39.             if (!data){
  40.                 jq_config.failed_tries++;
  41.                 console.log("Failed to get page. Retrying...");
  42.                 if (jq_config.failed_tries >= 10){
  43.                     modal.open("Failed to get page", "Couldn't get page info. Try again in a moment.");
  44.                     return;
  45.                 }
  46.                 setTimeout(function(){
  47.                     main();
  48.                 }, 400);
  49.                 return;
  50.             }
  51.            
  52.             var page = $(data);
  53.             var player = page.find('.player_container .player');
  54.             if (!player.length){
  55.                 modal.open("Something went wrong", "Couldn't find the video player on this page. Maybe the site updated.");
  56.                 return;
  57.             }
  58.            
  59.             // Get configUrl data
  60.             // Use this to try to get the download urls of the video
  61.             var config_url = player.data('configUrl');
  62.             $.ajax({
  63.                 url: config_url,
  64.                 success: function(data){
  65.                     // When the stars are right, we should get the URL data back
  66.                     // If not, display a failure message
  67.                     if (data.message){
  68.                         modal.open("Download Not Found", "Could not retrieve download URLs.");
  69.                     } else {
  70.                         var videos = data.request.files[jq_config.vimeo_codec];
  71.                        
  72.                         // Compile html code
  73.                         var video_urls = "";
  74.                         $.each(videos, function(k, v){
  75.                             if (jq_config.video_types[k]){
  76.                                 video_urls += "<a class='btn' target='_blank' href='"+v.url+"'><i class='fa fa-download'></i> "+jq_config.video_types[k]+"</a>";
  77.                             }
  78.                         });
  79.                        
  80.                         // Failsafe
  81.                         if (video_urls == ''){
  82.                             modal.open("Download Not Found", "Could not find download URLs.");
  83.                             return;
  84.                         }
  85.                        
  86.                         modal.open("Download Video", video_urls);
  87.                     }
  88.                 }
  89.             });
  90.         }
  91.     });
  92. }
  93.  
  94. // Load jQuery
  95. function jquery_ready(main_callback){
  96.     if (window.jQuery){
  97.         main_callback(window.jQuery);
  98.         return;
  99.     }
  100.    
  101.     // Attach jQuery to the page
  102.     var script = document.createElement("script");
  103.     script.src = jq_config.jquery_url;
  104.     script.type = 'text/javascript';
  105.     document.getElementsByTagName("head")[0].appendChild(script);
  106.    
  107.     // Wait for it to load
  108.     var checkReady = function(check_callback){
  109.         if (window.jQuery){
  110.             check_callback(jQuery);
  111.         } else {
  112.             setTimeout(function(){checkReady(check_callback);}, 20);
  113.         }
  114.     };
  115.  
  116.     checkReady(function($){
  117.         $(main_callback);
  118.     });
  119. }
  120.  
  121. // Initialize Modal code and extra stuff
  122. if (typeof modal === 'undefined'){
  123.     var modal = {};
  124.     modal.init = false;
  125. }
  126. function modal_init(){
  127.     // Fix "more videos" links not working
  128.     $('#clips > li > a').click(function(){
  129.         window.location = $(this).attr('href');
  130.     });
  131.    
  132.     modal.init = true;
  133.     // Close modal object
  134.     modal.close = function(){
  135.         if (modal.object){
  136.             modal.object.fadeOut(200, function(){
  137.                 $(this).remove();
  138.             });
  139.             modal.object = null;
  140.         }
  141.     };
  142.    
  143.     // Get Fontawesome
  144.     $('<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">').appendTo('body');
  145.    
  146.     // Modal styling
  147.     $("<style>#tsf{line-height: 20px;position:fixed;top:0;right:0;bottom:0;left:0;z-index:1050;display:block;background-color:rgba(0,0,0,.5)}#tsf .dialogue{background-color:#292929;color:#eee;max-width:600px;min-height:200px;margin:0 auto;font-size:14px;position:relative;box-shadow:0 2px 6px rgba(0,0,0,.7)}#tsf .muted{color:#777;font-size:13px}#tsf a{color:#aaa;display:block}#tsf a:hover{color:#ccc}#tsf .close{position:absolute;top:0;right:0;margin:6px 8px;font-size:20px;cursor:pointer;color:#aaa}#tsf .close:hover{color:#ccc}#tsf .header{border-bottom:1px solid #444;font-size:20px;padding:8px}#tsf .content{padding:8px}#tsf a.btn{background-color:#333;border:1px solid #444;border-radius:2px;padding:7px 9px;display:inline-block;margin:3px;text-decoration:none}#tsf a.btn:hover{background-color:#393939;}#tsf *{transition:color .2s,background-color .2s;} #tsf .version{position:absolute;bottom:0;right:0;text-align:right;font-size:11px;color:#777;padding:3px 5px;}</style>").appendTo('body');
  148.    
  149.     // Create modal with header text and main content
  150.     modal.open = function(header, content){
  151.         // Close existing modal
  152.         modal.close();
  153.        
  154.         var modal_html = "<div id='tsf'><div class='dialogue'><div class='close'><i class='fa fa-times'></i></div><div class='header'></div><div class='content'></div><div class='version'></div></div></div>";
  155.            
  156.         // Create modal object
  157.         modal.object = $(modal_html);
  158.        
  159.         // Click event for closing modal
  160.         modal.object.find('.close').click(modal.close);
  161.        
  162.         // Fill in content
  163.         modal.object.find('.header').html(header);
  164.         modal.object.find('.content').html(content);
  165.        
  166.         modal.object.find('.version').html(jq_config.info);
  167.        
  168.         // Append modal to page
  169.         $('body').append(modal.object);
  170.        
  171.         // Show it
  172.         modal.object.hide();
  173.         modal.object.fadeIn(200);
  174.         modal.object.find('.dialogue').animate({'margin-top': '30px'}, 200);
  175.     };
  176.    
  177.     // Clicking on backdrop closes modal
  178.     $(document).mouseup(function(e){
  179.         if ($(e.target).is('#tsf')) modal.close();
  180.     });
  181. }
  182.  
  183. // Run script
  184. jquery_ready(function(){
  185.     if (!modal.init) modal_init();
  186.     main();
  187. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement