mesak / youav

// ==UserScript==
// @name         youav
// @namespace    http://tampermonkey.net/
// @version      0.7
// @description  try to take over the world!
// @author       You
// @match        https://www.youav.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    function clearNode(element)
    {
        var clone = element.cloneNode();
        while (element.firstChild) {
            clone.appendChild(element.lastChild);
        }
        element.parentNode.replaceChild(clone, element);
    }
    clearNode( document.getElementsByTagName('body')[0] );
    function getVid(url){
        return url.match(/video\/(\d+)/)[1];
    }
    $('.well > a').each(function(i,n){
        var vid = getVid( $(n).attr('href') );
        var title = $(n).find('img:first').attr('title');
        $(n).parent().find('.video-added').html('<input type="text" value="'+title+'" class="form-control"><div class="btn-group text-center" id="videosav_'+vid+'"><button class="get_link btn btn-primary" data-vid="'+vid+'" data-title="'+title+'">Get Download File</button></div>');
    });
    if( $('#videowatch').size() ){
        var vid = getVid( location.href );
        $('.pull-right > .btn-group').parent().append('<div class="btn-group text-center" id="videosav_'+vid+'"><button class="get_link btn btn-primary" data-vid="'+vid+'">Get Download File</button></div>');
        
         getVideoData(vid , $('.big-title-truncate').text());
    }
    $('.get_link').on('click',function(e){
        var vid = $(this).data('vid'),
            title = $(this).data('title');
        $('#videosav_'+vid).html('<span class="glyphicon glyphicon-refresh"></span>');
        getVideoData(vid,title);
        e.preventDefault();
    });
    
    function getVideoData(vid, title)
    {
        title = typeof title == 'string' ? title :'';
        title = title.match(/\w+[-]\d+/)[0].toUpperCase();
        $.get('/load.php?pid=' + vid,function(r){
            var list = getVideoList(r);

            $('#videosav_'+vid).html('<span class="glyphicon glyphicon-remove-sign" aria-hidden="true"></span>');
            if( $.isArray(list))
            {
                var ret = '';
                //console.log( data );
                $.each(list,function(i,n){
                	var file = n.file;
	                if( file.indexOf('title=') != -1 ){
	                	var oname = file.split('title=')[1];
	                	var new_name = oname.toUpperCase();
		                if( new_name.substr(new_name.length-2) == 'HK'){
		                	new_name = new_name.substr(0,new_name.length-2);
		                }
	                	file = file.replace(oname,new_name);
	                }else{
                        console.log(title);
                        file += '&title=' + title;
                    }
                	ret += '<a class="btn btn-primary" href="'+file+'" download><span>'+n.label+'</span></a>';
                });
                $('#videosav_'+vid).html(ret);
            }
        });
    }
	function getVideoList(code)
	{
        code = code.match(/sources.+/)[0].replace('sources:','').replace('},],','}]');
        return JSON.parse(JSON.stringify(eval('('+code+')')) );
    }
})();