Vimeo Download

Adds a download button to the video player.

目前為 2016-01-24 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        Vimeo Download
// @namespace   schwarztee
// @description Adds a download button to the video player.
// @include     https://vimeo.com/*
// @copyright   2015, schwarztee
// @license     MIT
// @version     0.2.1
// @grant       none
// ==/UserScript==

'use strict';

(function(){
    
    // helper: find DOM element
    function find( selector ) { return document.querySelector( selector ); }
    
    // wait for player to be ready and set up periodic video check
    function setup()
    {
        // controller object in DOM and video element available?
        if ( window && 'vimeo' in window && find( '.video video' ) )
        {
            // keep track of video URL
            var currentURL = '';
            
            // periodically check video (to follow quality changes)
            setInterval( function checkVideo()
            {
                // find the main video element and get source URL
                var newURL = find( '.video video' ).src;
                
                // different from previously found URL?
                if ( newURL != currentURL )
                {
                    // developer information
                    console.log( "[Vimeo Download] Found new video URL:", newURL );
                    
                    // make new download button
                    makeButton( newURL );
                    
                    // remember new URL
                    currentURL = newURL;
                }
                
            }, 500 );
        }
        else
        {
            // try again later
            setTimeout( setup, 500 );
        }
    }
    
    // add download button to video player controls
    function makeButton( url )
    {
        var title, height, qualityInfo;
        
        // try to get video metadata
        // (this can easily break if Vimeo updates their object tree)
        try
        {
            // get video ID
            var videoId = window['vimeo']['clip_page_config']['clip']['id'];
            
            // retrieve active player properties
            var videoInfo = window['vimeo']['clips'][videoId]['video'];
            
            // get title and video height
            title  = videoInfo['title'];
            height = videoInfo['height'];
            
            // prepare quality information from video height
            qualityInfo = " ("+height+"p)";
            
            // log gathered information
            console.log( "[Vimeo Download] Making download button for \""+title+"\" ("+height+"p)" );
        }
        catch ( error )
        {
            // set quality information to empty string
            qualityInfo = "";
            
            // output a warning
            console.warn( "[Vimeo Download] Making download button for unknown video, error retrieving meta data:", error );
        }
        
        // make valid filename from title
        var filename = title.replace( /[<>:"\/\\|?*]/g, '' ) + '.mp4';
        
        // remove old button if existing
        var oldButton = find( '.button.dwnld' );
        oldButton && oldButton.remove();
        
        // create new button
        var button = document.createElement( 'a' );
        button.href = url;
        button.target = '_blank';
        button.download = filename;
        button.innerHTML = "⥥";
        button.title = "Download" + qualityInfo;
        button.setAttribute( 'class', "button dwnld" );
        button.setAttribute( 'style', 'display: inline-block; font-size: 1.7em; margin: -0.4em 0 0 0.4em; color: #fff' );
        
        // apply mouseover effect
        button.onmouseenter = function() { button.style.color = 'rgb(68,187,255)'; };
        button.onmouseleave = function() { button.style.color = '#fff'; };
        
        // find control bar and add button
        find( '.play-bar' ).appendChild( button );
    }
    
    // start looking for video player
    setup();
    
})();