osatien / Oscar Probando

// ==UserScript==
// @name         Oscar Probando
// @namespace    http://your.homepage/
// @version      0.7
// @description  Corriendo el maraton
// @author       You
// @match        http://www.netliga.com/NetLiga_sub1/*
// @require  http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @require http://cdnjs.cloudflare.com/ajax/libs/canvasjs/1.7.0/jquery.canvasjs.min.js
// @require http://cdnjs.cloudflare.com/ajax/libs/numeral.js/1.4.5/numeral.min.js
// @grant    GM_addStyle
// @grant       GM_setValue
// @grant GM_xmlhttpRequest
// ==/UserScript==
//alert("hola mundo");
GM_addStyle ( "                         \
    #gmSomeID {                         \
        position:       fixed;          \
        top:            0px;            \
        left:           0px;            \
    }                                   \
.yellow { \
background-color:    yellow; \
} \
.blue { \
background-color:    blue; \
} \
.red { \
background-color:    red; \
} \
" );
//GM_setValue("CanvasJS",CanvasJS);

$(function () {
    $("#banner_central").hide();
    $("#div_pub_net_lat").hide();
//insert a new object in CloudBoost
   
 
});

var liguilla= jQuery("#ligaform option:selected").attr("value")
$("body").append ( '<div id="gmSomeID"><p>HOLA MUNDO</p></div>' );
$("body").append ( '<div id="charto"></div>' );
jQuery("#colDerecha").prepend('<div id="chartos"/>');
pintar();
var total=0;
var totalAlineado=0;
var jugadoresTotales=0;
var miDinero=0;


function pintar() {
    $("#gmSomeID").html("<p>HOLA MUNDO</p>");
     $("#gmSomeID").append ("Jugadores: " +  localStorage.getItem('jugadoresTotales') + " <br/>");
    $("#gmSomeID").append ("Total: " +  localStorage.getItem('total') + " <br/>");
    $("#gmSomeID").append ("Total Alineado: " +  localStorage.getItem('totalAlineado') + " <br/>");
    $("#gmSomeID").append ("Mi dinero: " +  localStorage.getItem('miDinero') + " <br/>");
$("#gmSomeID").append ("Algo raro: " +  (parseInt(localStorage.getItem('total'))+ parseInt(localStorage.getItem('miDinero')))  + " <br/>");

        
}
function actStorage() {
    localStorage.setItem('jugadoresTotales',jugadoresTotales);
    localStorage.setItem('total',total);
    localStorage.setItem('totalAlineado',totalAlineado);
    localStorage.setItem('miDinero',miDinero);
    

}


function actualizarTabla (jNode) {

}

/*
$(window).bind ("storage", function (zEvent) {
            var varName     = zEvent.originalEvent.key;
            var newValue    = zEvent.originalEvent.newValue;

            eval("'" +varName + '=' + newValue +"'");
    alert("OSCAR::"+total);

            
        } );

*/
//waitForKeyElements ("#contenidoPestanias", actualizarTabla);
var datos2=[{x:new Date(2016,10,11),y:1676005},];

function creaFuncion(indici,titulo) {
    return function() {
                jQuery("#charto"+indici).CanvasJSChart({ //Pass chart options
                    title: {
			text: titulo+"-" + nombrecito
		},
		data: [
		{
		type: "spline", //change it to column, spline, line, pie, etc
		dataPoints:dataChart
        }
           ,{
            type: "column",
             toolTipContent: "{jugador} {x}: {y}",
            dataPoints:datos3
        }
	], zoomEnabled: true, culture: 'es', 
                    toolTip: {
        shared: "true"
      },
                    axisX: { valueFormatString: 'DD-MMM-YY' }
                /*    axisY: { includeZero: false, 
                            labelFormatter: function ( e ) { if (e.value > 1000000) { return numeral(e.value).format('0[.]0a') + '?';} return numeral(e.value).format('0a') + '?'; },
                            labelFontSize: 12, interval: interval }, 
                    
                    toolTip: { enabled: true, } 
                    */
        });
        
    };
}

function creaFuncion33(indici,titulo) {
    return function() {
                jQuery("#charto"+indici).CanvasJSChart({ //Pass chart options
                    title: {
			text: titulo+"-" + nombrecito
		},
		data: [
		{
		type: "spline", //change it to column, spline, line, pie, etc
		dataPoints:dataChart
        }
           ,{
            type: "column",
             toolTipContent: "{jugador} {x}: {y}",
            dataPoints:datos3
        }
	], zoomEnabled: true, culture: 'es', 
                    toolTip: {
        shared: "true"
      },
                    axisX: { valueFormatString: 'DD-MMM-YY' }
                /*    axisY: { includeZero: false, 
                            labelFormatter: function ( e ) { if (e.value > 1000000) { return numeral(e.value).format('0[.]0a') + '?';} return numeral(e.value).format('0a') + '?'; },
                            labelFontSize: 12, interval: interval }, 
                    
                    toolTip: { enabled: true, } 
                    */
        });
        
    };
}



function addCanvas(callback,p1,p2,p3) {
    var script = document.createElement("script");
    script.setAttribute("src", "//cdnjs.cloudflare.com/ajax/libs/canvasjs/1.7.0/jquery.canvasjs.min.js");
    script.addEventListener('load', function() {
        //console.log("cargado" + "('"+p1+"','"+p2+"','"+p3+"');");
        var script = document.createElement("script");
        script.textContent = "" + callback + "('"+p1+"','"+p2+"','"+p3+"');";
        document.body.appendChild(script);
    }, false);
    document.body.appendChild(script);
}




function creaFuncion2(indicito,nombre,id_jugador,liga,nombreSi) {
    return function () {
         console.log("cargado" + "('"+indicito+"','"+nombre+"','"+nombreSi+"');");
        jQuery.getScript("http://txipirones-osatien.rhcloud.com/scriptjson/dame/"+nombre+"/"+id_jugador+"/"+liga).done(creaFuncion(indicito,nombreSi));
    };
}




//unsafeWindow.creaFuncion2= creaFuncion2;
//unsafeWindow.creaFuncion=creaFuncion;
function contar(url) {
    if (url == "t_mercado.jsp") {
        
 
                          }
        
    
    if (url == "fichaEquipo.jsp") {
        var aalineados=[];
jQuery("#plantilla td.jugador").each(function() {
	elemento=jQuery(this).find("a");
	if (elemento.attr("style").indexOf("B4")==-1)
	{aalineados.push(elemento.text().trim().toUpperCase())}

});
        
  $("#gmSomeID").dblclick(function()  {

          var indicito=1;
        
        //#botones_compra > div > table > tbody > tr:nth-child(3) > td:nth-child(2) > div:nth-child(3) > strong
        jQuery("tr.impar").each(function() {
            jj = jQuery(this);
            element = jj.find("td strong:first");
            nombre =element.text().trim();
            nombreSimple=nombre;
            nombre=nombre.replace("´","-");
            nombre=nombre.replace("`","-");
            nombre=nombre.replace("Á","-");
            nombre=nombre.replace("É","-");
            nombre=nombre.replace("Í","-");
            nombre=nombre.replace("Ó","-");
            nombre=nombre.replace("Ú","-");
            nombre=nombre.replace("ñ","-");
             nombre=nombre.replace("Ñ","-");
            nombre=nombre.replace("\.","-");
             nombre=nombre.replace(" ","-");
           console.log("oscar " + nombre);
             jQuery("#chartos").append ( '<div id="charto' +indicito+'" style="height:200px;"></div>' );
            console.log(jj.find("td:nth-child(3)"));
            console.log(jQuery("#charto"+indicito + " " + nombre + " " + nombreSimple));
            id_jugador=element.find("a").attr("href").split("=")[1];
              jQuery.getScript("http://txipirones-osatien.rhcloud.com/scriptjson/dame/"+nombre+"/"+id_jugador+"/"+liguilla).done(creaFuncion(indicito,nombreSimple));
            //addCanvas("creaFuncion2",indicito,nombre,nombreSimple);
                  
      
            indicito++;
           
    });
    });    
        miDinero=jQuery("#resumen_economia span:first").text().replace("€","").replace(/\./g,"").trim();
        console.log("sssss"+miDinero);
        jugadoresTotales=jQuery("tr.impar td:nth-child(5)").size();
        actStorage();
        pintar();
total=0;
totalAlineado=0;
 jQuery("tr.impar").each(function() {
     jj = $(this);
     
     element = jj.find("td:nth-child(9)");
     //console.log("vendo"+element.parent().text());
     nombreJugador = jj.find("td:nth-child(1)").find("div:nth-child(2) > strong > a");
     console.log("nombre"+nombreJugador.text().trim());
     nombreJugador=nombreJugador.text().trim();
     element.click(function() {
         elemento = $(this).parent().find("td:nth-child(1)");
         linkado = elemento.find("div:nth-child(2) > strong > a");
         numero_id = linkado.attr("href").split("=")[1];
         nombre = linkado.text();
         dinero = elemento.parent().find("td:nth-child(5)").text().replace("€","").trim();
         vender_directamente(parseInt(numero_id),nombre,dinero);
         
     });
     
     element = jj.find("td:nth-child(4)");
         
       element.click(function(){ 
          // debugger;
           elemento = $(this);
           numero = parseInt(elemento.text().replace("€","").replace(/\./g,"").trim());
           console.log("el jugador vale:" + numero);
           if (elemento.hasClass("yellow") ) {
             //azul alinea y vende
               elemento.removeClass("yellow");
               totalAlineado=totalAlineado+numero;
              // total=total-numero;
               elemento.addClass("blue");
           } else if (elemento.hasClass("blue") ) {
               
               //red  alineaado  
              elemento.addClass("red");
               total=total-numero;
               //totalAlineado=totalAlineado+numero;
               elemento.removeClass("blue");
           } else if (elemento.hasClass("red") ) {
               
               //blanco
               total=total+numero;
               elemento.removeClass("red");
               
               
           }            else {
               
               //amarillo desalinea
               elemento.addClass("yellow");
               totalAlineado=totalAlineado-numero;
               console.log(22)
           }
           actStorage();
           pintar();
         
       });
     numero = parseInt(element.text().replace("€","").replace(/\./g,"").trim());
     
     total = total + numero;
     console.log("nnnn"+nombreJugador)
     if (jQuery.inArray(nombreJugador,aalineados)!=-1)
     {
         console.log("SUMO A " + nombreJugador +" con " + numero);
         totalAlineado=totalAlineado+numero;
     }
     actStorage();
  console.log(numero)

});
        
         pintar();
        }
}

var direcccion = window.location.href;




if (direcccion.indexOf("mercado.jsp")!=-1) {


 $("#gmSomeID").dblclick(function()  {
            //var chart = new CanvasJS.Chart("menu_horizontal_principal", optionsLine); 
            
            //chart.render();
            
          var indicito=1;
        jQuery("tr.impar,tr.par").each(function() {
            jj = jQuery(this);
            element = jj.find("td strong a");
            nombre =element.text().trim();
            nombreSimple=nombre;
            nombre=nombre.replace(new RegExp("´", 'g'),"-");
            nombre=nombre.replace(new RegExp("`", 'g'),"-");
            nombre=nombre.replace(new RegExp("Á", 'g'),"-");
            nombre=nombre.replace(new RegExp("É", 'g'),"-");
            nombre=nombre.replace(new RegExp("Í", 'g'),"-");
            nombre=nombre.replace(new RegExp("Ó", 'g'),"-");
            nombre=nombre.replace(new RegExp("Ú", 'g'),"-");
            nombre=nombre.replace(new RegExp("ñ", 'g'),"-");
             nombre=nombre.replace(new RegExp("Ñ", 'g'),"-");
            console.log("nnnn1"+nombre);
            nombre=nombre.replace(new RegExp("\\.", 'g'),"-");
            console.log("nnnn2"+nombre);
             nombre=nombre.replace(new RegExp(" ", 'g'),"-");
           
             jQuery("#chartos").append ( '<div id="charto' +indicito+'" style="height:200px;"></div>' );
            console.log(jj.find("td:nth-child(3)"));
            console.log(jQuery("#charto"+indicito));
            id_jugador=element.attr("href").split("=")[1];
            console.log("nnnn"+nombre);
            //nombre44=nombre.replace(new RegExp("-", 'g'),"%");
            console.log("nnnn"+nombre);
             
            jQuery.getScript("http://txipirones-osatien.rhcloud.com/scriptjson/dame/"+nombre+"/"+id_jugador+"/"+liguilla).done(creaFuncion(indicito,nombreSimple));
             
            //addCanvas("creaFuncion2",indicito,nombre,nombreSimple);
                  
      
            indicito++;
           
    });
    });


}

if (direcccion.indexOf("equipo_perfil.jsp")!=-1) {
    $("#gmSomeID").dblclick(function()  {
     
          var indicito=1;
        
        //#botones_compra > div > table > tbody > tr:nth-child(3) > td:nth-child(2) > div:nth-child(3) > strong
        jQuery("tr.impar").each(function() {
            jj = jQuery(this);
            element = jj.find("td strong:first");
            nombre =element.text().trim();
            nombreSimple=nombre;
            nombre=nombre.replace("´","-");
            nombre=nombre.replace("`","-");
            nombre=nombre.replace("Á","-");
            nombre=nombre.replace("É","-");
            nombre=nombre.replace("Í","-");
            nombre=nombre.replace("Ó","-");
            nombre=nombre.replace("Ú","-");
            nombre=nombre.replace("ñ","-");
             nombre=nombre.replace("Ñ","-");
            nombre=nombre.replace("\.","-");
             nombre=nombre.replace(" ","-");
           console.log("oscar " + nombre);
             jQuery("#chartos").append ( '<div id="charto' +indicito+'" style="height:200px;"></div>' );
            console.log(jj.find("td:nth-child(3)"));
            console.log(jQuery("#charto"+indicito + " " + nombre + " " + nombreSimple));
            id_jugador=element.parent().find("a").attr("href").split("=")[1];
             jQuery.getScript("http://txipirones-osatien.rhcloud.com/scriptjson/dame/"+nombre+"/"+id_jugador+"/"+liguilla).done(creaFuncion(indicito,nombreSimple));
             
            //addCanvas("creaFuncion2",indicito,nombre,nombreSimple);
                  
      
            indicito++;
           
    });
    });

};

var s = unsafeWindow.makeRequest;

unsafeWindow.makeRequest= function(tipo,url,valores,id_html,onComplete) {
	
	jQuery('#'+id_html).html('<img src="estaticos_2_0/img/cargando.gif" />');
	jQuery.ajax(url, {
		   "type": tipo,   // usualmente post o get
		   "success": function(result) {
    		   onComplete;
			   jQuery('#'+id_html).html(result);
               contar( url);
		   },
		   "error": function(result) {
		    
		   },
		   "dataType ": "html",
		   "data": valores,
		   "async": true,
		});	

        return(1);
};