Vimeo Download

Adds a download button to the video player.

当前为 2016-08-04 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Vimeo Download
  3. // @namespace schwarztee
  4. // @description Adds a download button to the video player.
  5. // @include https://vimeo.com/*
  6. // @copyright 2015, schwarztee
  7. // @license MIT
  8. // @version 0.2.3
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function(){
  13.  
  14. 'use strict';
  15.  
  16. // helper: find DOM element
  17. function find( selector ) { return document.querySelector( selector ); }
  18.  
  19. // wait for player to be ready and set up periodic video check
  20. function setup()
  21. {
  22. // controller object in DOM and video element available?
  23. if ( window && 'vimeo' in window && find( '.video video' ) )
  24. {
  25. // try to get video metadata
  26. // (this can easily break if Vimeo updates their object tree)
  27. try
  28. {
  29. // get video ID
  30. var videoId = vimeo['clip_page_config']['clip']['id'];
  31.  
  32. // retrieve active player properties
  33. var videoInfo = vimeo['clips'][videoId];
  34.  
  35. // save title
  36. var title = videoInfo['video']['title'];
  37.  
  38. // get streams
  39. var streams = videoInfo['request']['files']['progressive'];
  40.  
  41. // sort streams descending by video resolution
  42. streams.sort( function compare( streamA, streamB )
  43. {
  44. // compare width property
  45. return streamB.width - streamA.width;
  46. });
  47.  
  48. // get video file info
  49. // - just take the first one with the highest quality
  50. // - this will be replaced when I got more time
  51. var file = streams[0];
  52.  
  53. // log gathered information
  54. console.log( "[Vimeo Download] Making download button for \""+title+"\" ("+file.quality+")" );
  55.  
  56. // display the button
  57. makeButton( file.url, title, file.quality );
  58. }
  59. catch ( error )
  60. {
  61. // log the error
  62. console.error( "[Vimeo Download] Error retrieving video meta data:", error );
  63. }
  64. }
  65. else
  66. {
  67. // try again later
  68. setTimeout( setup, 500 );
  69. }
  70. }
  71.  
  72. // add download button to video player controls
  73. function makeButton( url, title, quality )
  74. {
  75. // make valid filename from title
  76. var filename = title.replace( /[<>:"\/\\|?*]/g, '' ) + '.mp4';
  77.  
  78. // remove old button if existing
  79. var oldButton = find( '.button.dwnld' );
  80. oldButton && oldButton.remove();
  81.  
  82. // create new button
  83. var button = document.createElement( 'a' );
  84. button.href = url;
  85. button.target = '_blank';
  86. button.download = filename;
  87. button.innerHTML = "⥥";
  88. button.title = "Download " + quality;
  89. button.setAttribute( 'class', "button dwnld" );
  90. button.setAttribute( 'style', 'display: inline-block; font-size: 1.7em; margin: -0.4em 0 0 0.4em; color: #fff' );
  91.  
  92. // apply mouseover effect
  93. button.onmouseenter = function() { button.style.color = 'rgb(68,187,255)'; };
  94. button.onmouseleave = function() { button.style.color = '#fff'; };
  95.  
  96. // find control bar and add button
  97. find( '.play-bar' ).appendChild( button );
  98. }
  99.  
  100. // start looking for video player
  101. setup();
  102.  
  103. })();