mesak / myavsuper

// ==UserScript==
// @name         myavsuper
// @namespace    http://tampermonkey.net/
// @version      0.4
// @description  try to take over the world!
// @author       You
// @match        https://myavsuper.com/*
// @grant        none
// ==/UserScript==//
(function() {
    'use strict';
	var css = document.createElement("style");
	css.type = "text/css";
	css.innerHTML = '.btn { display: inline-block; margin-bottom: 0; font-weight: normal; text-align: center; vertical-align: middle; cursor: pointer; background-image: none; border: 1px solid transparent; white-space: nowrap; padding: 8px 12px; font-size: 14px; line-height: 1.42857143; border-radius: 4px; -webkit-border-radius: 4px; -moz-border-radius: 4px; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none;}'+
		'.btn-group{position: relative;vertical-align: middle;display:inline-block;}'+
		'.btn-group>.btn:first-child:not(:last-child):not(.dropdown-toggle) { border-bottom-right-radius: 0;    border-top-right-radius: 0;}'+
		'.btn-group>.btn:first-child {margin-left: 0;}'+
		'.btn-group>.btn, .btn-group-vertical>.btn {position: relative;float: left;}'+
		'.btn-primary {color: #ffffff;background-color: #da2657;border-color: #da2657;}';
	document.body.appendChild(css);
    $('#main .post > a').each(function(i,n){
        var vid = $(n).parent()[0].id.replace('post-','');
        var title = $(n).find('img:first').attr('alt');
        //var vv = $(n).after();
        $(n).find('progress').remove();
        $(n).after('<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+'">Get Download File</button></div>');
        
    });
    $('.get_link').on('click',function(e){
        var vid = $(this).data('vid');
        $('#videosav_'+vid).html('<i class="fa fa-refresh fa-spin fa-3x fa-fw" aria-hidden="true"></i>');
        getVideoData(vid);
        e.preventDefault();
    });
    function getVideoData(vid)
    {
        $.get('https://myavsuper.com/?p='+ vid,function(r){
        	var title =  $('.entry-title',r).html().match(/\w+[-]\d+/)[0].toUpperCase();
        	//console.log( $('.entry-title',r).html() )
        	var code = $('#video-block',r).html();
        	var list = getVideoList(code);
            $('#videosav_'+vid).html('<span class="fa fa-times" aria-hidden="true"></span>');
            if( $.isArray(list))
            {
                var ret = '';
                //console.log( data );
                $.each(list,function(i,n){
					ret += '<a class="btn btn-primary" href="'+n.file+'&title='+title+'" download><span>'+n.label+'</span></a>';
                });
                $('#videosav_'+vid).html(ret);
            }
        });
    }
	function getVideoList(code)
	{
		code = code.match(/atob\(\"(.+)\"\)\)\;/)[1];
		code = window.atob(code);
		code = code.match(/sources.+\],/)[0].replace('sources":','').replace('}],','}]');
		return JSON.parse(JSON.stringify(eval('('+code+')')) );
	}
	
})();