Video Download Button

This script adds a download button on many video sites.

当前为 2016-06-27 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name        Video Download Button
// @namespace   VDBMB
// @author      MegaByte
// @description This script adds a download button on many video sites.
// @require     http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// @run-at 		document-end
// @noframes
// @include     http*://*streamcloud.eu/*
// @include     http*://*powerwatch.pw/*
// @include     http*://*vivo.sx/*
// @include     http*://*shared.sx/*
// @include     http*://*youtube.com/watch?v=*
// @version     1.3
// @grant       GM_addStyle
// ==/UserScript==


/*
 * getStyleObject Plugin for jQuery JavaScript Library
 * From: http://upshots.org/?p=112
 */

(function($){
    $.fn.getStyleObject = function(){
        var dom = this.get(0);
        var style;
        var returns = {};
        if(window.getComputedStyle){
            var camelize = function(a,b){
                return b.toUpperCase();
            };
            style = window.getComputedStyle(dom, null);
            for(var i = 0, l = style.length; i < l; i++){
                var prop = style[i];
                var camel = prop.replace(/\-([a-z])/g, camelize);
                var val = style.getPropertyValue(prop);
                returns[camel] = val;
            };
            return returns;
        };
        if(style = dom.currentStyle){
            for(var prop in style){
                returns[prop] = style[prop];
            };
            return returns;
        };
        return this.css();
    }
})(jQuery);

$.fn.copyCSS = function(source){
  var styles = $(source).getStyleObject();
  this.css(styles);
}


function main() {
  var site = window.location.href || document.URL;

  if(site.includes("streamcloud.eu")) streamcloud();
  else if(site.includes("powerwatch.pw")) powerwatch();
  else if(site.includes("vivo.sx")) vivo();
  else if(site.includes("shared.sx")) shared();
  else if(site.includes("youtube.com")) youtube();
}

function streamcloud() {
  if($("#player_code").length === 0) return;
	var url = searchInScripts("file:\\s?\"https?:\\/\\/.+?\\.mp4\"", "http", ".mp4");
	if(url !== null) 
		$(".container-fluid ul.nav").prepend("<li>" + button(url) + "</li>");
}

function powerwatch() {
  if($("#vplayer").length === 0) return;
	var url = searchInScripts("file:\\s?\"https?:\\/\\/.+?\\.mp4\"", "http", ".mp4");
	if(url !== null) {
		$("h5.h4-fine").html("<span class='head'>"+$("h5.h4-fine").html()+"</span><span class='down'>" + button(url) + "</span>")
		GM_addStyle("h5.h4-fine { display: flex; } h5.h4-fine .head { flex-grow: 1; } h5.h4-fine .down { flex-grow: 0; }");
	}
}

function vivo() {
  var e = $(".stream-content");
  if(e.length === 0) return;
  var url = e.attr("data-url");
  if(typeof url === "undefined") return;
  var lightBTN = $(".light-switch.btn");
  var downBTN = $("<div class='download'>" + button(url) + "</div>");
  downBTN.copyCSS(lightBTN);
  lightBTN.parent().prepend(downBTN);
  GM_addStyle(".download { margin-right: 15px !important;} .download:hover { color: #fff !important; background-color: #39b3d7 !important; border-color: #269abc !important; }  .download a { text-decoration: none; color: white; }");
}

function shared() {
  var e = $(".stream-content");
  if(e.length === 0) return;
  var url = e.attr("data-url");
  if(typeof url === "undefined") return;
  $(".light-switch").parent().prepend("<div class='download'>" + button(url) + "</div>");
  GM_addStyle(".addthis_toolbox { width: unset !important; }  .download { position: relative; background-color: #FF6550; color: #FFF; float: right; font-size: 13px; font-weight: 700; height: 32px; line-height: 32px; margin: 0 0 0 10px; padding: 0 15px; width: auto; cursor: pointer; -webkit-transition: all .35s ease-in; -moz-transition: all .35s ease-in; -o-transition: all .35s ease-in; transition: all .35s ease-in; opacity: 1; z-index: 300; }  .download a { text-decoration: none; color: white;}");
}

function youtube() {
  $("script").each(function() {
    var cont = decodeURI($(this).html());
    if(!cont.includes("\"adaptive_fmts\":")) return;
    cont = cont.replace(",", "\\u0026");
    var data = cont.split("\\u0026");
    var format = [];
    var vid = {};
    for(var dat of data) {
      var tmp = dat.split("=");
      var name = tmp[0];
      var val = tmp[1];
      if("|type|quality_label|url|fps|".includes("|"+name+"|")) {
        if(name === "url" || name === "type") val = decodeURIComponent(val);
        if(name === "type") val = val.substring(0, val.indexOf(";"));
        if(name in vid) {
          if(("url" in vid) && ("type" in vid)) format.push(vid);
          vid = {};
        } else vid[name] = val;
      }
    }
    for(var t of format) console.log(t);
    var links = "";
    for(var t of format) links += "<li><span loop='0' class='yt-ui-menu-item'><a class='download-link' download href='" + t["url"] + "'>" + t["type"] + "|" + t["quality_label"] + "</a></span></a></li>";
    $("#watch8-secondary-actions").prepend("<button title='Download' aria-expanded='false' aria-pressed='false' aria-labelledby='yt-uix-tooltip25-arialabel' role='button' type='button' data-tooltip-text='Download' class='yt-uix-button  yt-uix-button-size-default yt-uix-button-opacity yt-uix-tooltip' id='download-video-button'><span><span class='yt-uix-button-icon-wrapper'><img style='width:20px;height:20px;background-size:20px 20px;background-repeat:no-repeat;background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAABG0lEQVRYR+2W0Q3CMAxE2wkYAdiEEWADmIxuACMwCmzABpCTEmRSO7YTQX+ChECV43t2nF7GYeHPuLD+0AKwC/DnWMAp/N5qimkBuAfBdRTF/+2/AV6ZYFUxVYuicAfoHegd6B3oHfhZB+ByF+JyV8FkrAB74pqH3DU5L3iGoBURhdVODIQF4EjEkWLmmhYALOQgNIBcHHke4buhxXAAaFnaAhqbQ5QAOHHkwhZ8balkx1ICCiEBWNZ+CivdB7REHIC2ZjZK2oWklDDdB1NSdCd/Js2PqQMpSIKYVcM8kE6QCwDBNRCqOBJrW0CL8kCYxL0A1k6YxWsANAiXeC2ABOEWbwHAWrwxpzgkmA/JtIqnxTOElmPnjlkc4A3FykAhA42AxwAAAABJRU5ErkJggg==);' class='yt-uix-button-icon' src='//s.ytimg.com/yt/img/pixel-vfl3z5WfW.gif'></span><span class='yt-uix-button-content'>Download </span><ul class='yt-uix-button-menu hid' style='min-width: 114px; left: 40px; top: 501px; display: none;'>" + links + "</ul></span></button>");
    GM_addStyle(".download-link { text-decoration: none; color: inherit; } .download-link:hover { text-decoration: none; }");
  });
}

function searchInScripts(patt, start, end) {
  var url = null;
	$("body script").each(function() {
    var regex = new RegExp(patt);
		var out = regex.exec($(this).html());
		if(typeof out !== "undefined" && out !== "" && out !== null) {
      if(typeof out !== "string") out = out[0];
      var s = out.indexOf(start);
      var e = out.lastIndexOf(end);
      if( s!==-1 && e!==-1 ) url = out.substring(s, e);
      return false;
    }
	});
  return url;
}

function button(url) {
  return "<a href='" + url + "' download target='_blank'>Download</a>";
}

main();