Vimeo Download

Adds a download button to the video player.

当前为 2016-01-24 提交的版本,查看 最新版本

  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.1
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. 'use strict';
  13.  
  14. (function(){
  15. // helper: find DOM element
  16. function find( selector ) { return document.querySelector( selector ); }
  17. // wait for player to be ready and set up periodic video check
  18. function setup()
  19. {
  20. // controller object in DOM and video element available?
  21. if ( window && 'vimeo' in window && find( '.video video' ) )
  22. {
  23. // keep track of video URL
  24. var currentURL = '';
  25. // periodically check video (to follow quality changes)
  26. setInterval( function checkVideo()
  27. {
  28. // find the main video element and get source URL
  29. var newURL = find( '.video video' ).src;
  30. // different from previously found URL?
  31. if ( newURL != currentURL )
  32. {
  33. // developer information
  34. console.log( "[Vimeo Download] Found new video URL:", newURL );
  35. // make new download button
  36. makeButton( newURL );
  37. // remember new URL
  38. currentURL = newURL;
  39. }
  40. }, 500 );
  41. }
  42. else
  43. {
  44. // try again later
  45. setTimeout( setup, 500 );
  46. }
  47. }
  48. // add download button to video player controls
  49. function makeButton( url )
  50. {
  51. var title, height, qualityInfo;
  52. // try to get video metadata
  53. // (this can easily break if Vimeo updates their object tree)
  54. try
  55. {
  56. // get video ID
  57. var videoId = window['vimeo']['clip_page_config']['clip']['id'];
  58. // retrieve active player properties
  59. var videoInfo = window['vimeo']['clips'][videoId]['video'];
  60. // get title and video height
  61. title = videoInfo['title'];
  62. height = videoInfo['height'];
  63. // prepare quality information from video height
  64. qualityInfo = " ("+height+"p)";
  65. // log gathered information
  66. console.log( "[Vimeo Download] Making download button for \""+title+"\" ("+height+"p)" );
  67. }
  68. catch ( error )
  69. {
  70. // set quality information to empty string
  71. qualityInfo = "";
  72. // output a warning
  73. console.warn( "[Vimeo Download] Making download button for unknown video, error retrieving meta data:", error );
  74. }
  75. // make valid filename from title
  76. var filename = title.replace( /[<>:"\/\\|?*]/g, '' ) + '.mp4';
  77. // remove old button if existing
  78. var oldButton = find( '.button.dwnld' );
  79. oldButton && oldButton.remove();
  80. // create new button
  81. var button = document.createElement( 'a' );
  82. button.href = url;
  83. button.target = '_blank';
  84. button.download = filename;
  85. button.innerHTML = "⥥";
  86. button.title = "Download" + qualityInfo;
  87. button.setAttribute( 'class', "button dwnld" );
  88. button.setAttribute( 'style', 'display: inline-block; font-size: 1.7em; margin: -0.4em 0 0 0.4em; color: #fff' );
  89. // apply mouseover effect
  90. button.onmouseenter = function() { button.style.color = 'rgb(68,187,255)'; };
  91. button.onmouseleave = function() { button.style.color = '#fff'; };
  92. // find control bar and add button
  93. find( '.play-bar' ).appendChild( button );
  94. }
  95. // start looking for video player
  96. setup();
  97. })();