torinogranata / scriptDatiExpDuelli

// ==UserScript==
// @name        scriptDatiExpDuelli
// @namespace   scriptDatiExpDuelli
// @include    *.the-west.*/*
// @version     1
// @grant       none
// ==/UserScript==

//inserisce tag script

//javascript injection

//javascript injection
function creaScript(fn) {
	var script = document.createElement('script');
	script.setAttribute("type", "application/javascript");
	script.textContent = '(' + fn + ')();';
	document.body.appendChild(script);
	document.body.removeChild(script);
	
}

creaScript(function(){
//crea bottone interfaccia West
function creaButton(){
var div = $('<div class="ui_menucontainer">/');
	var link = $('<div class="menulink" style="background: url(https://westens.innogamescdn.com/images/interface/character/menuicons.jpg));background-position: 0 -250px;"></div>');
	link.click(clickHandler);
	div.append(link);
	div.append('<div class="loptions"></div><div class="menucontainer_bottom"></div>');
	$("#ui_menubar").append(div);
}
//clickhandler
function clickHandler(){
	var mywindow=wman.open();
	var content='<div id=contentduelscript><span>Inserire le username separate da una virgola</span><br><input id="username"><input id="go" type=submit value="Importa"><br><input id="r1" type="radio" name="scelta" value="duels" checked><span>duelli</span><br><input id="r2" type="radio" name="scelta" value="experience" checked>experience</div>';
	mywindow.appendToContentPane(content);
	document.getElementById("go").addEventListener("click",ImportaUser,false);
}

	function ImportaUser(){
	setTimeout(function(){
	var ArrayPartecipanti=[];
	var UserPartecipanti= document.getElementById("username").value;
	var scelta=""
	if (document.getElementById("r2").checked) {
			scelta=document.getElementById("r2").value
	}
	else{
			scelta=document.getElementById("r1").value
	}
	ArrayPartecipanti= UserPartecipanti.split(",");
	var ArrayRisultato=[];
	
	var flag=false;var n=ArrayPartecipanti.length;var i=0;
	function AjaxWest(){
			console.log(i);var player="";
		flag=false;
		player= ArrayPartecipanti[i].toLowerCase();console.log(player);
		Ajax.remoteCallMode('ranking','get_data',
												{tab: scelta, search: player },
		function(json){
			for(var j=0;j<json.ranking.length;j++)
			{console.log(i);console.log(player);console.log(json.ranking[j].name.toLowerCase());
				if(json.ranking[j].name.toLowerCase() == player)
				{   var ArrayPlayer=[];
					ArrayPlayer[0]=json.ranking[j].name;
					ArrayPlayer[1]=json.ranking[j].player_id;
				 	if(scelta=="duels"){
							ArrayPlayer[2]=json.ranking[j].duel_win;
							ArrayPlayer[3]=json.ranking[j].duel_loss;
							ArrayPlayer[4]=json.ranking[j].difference;
							ArrayPlayer[5]=json.ranking[j].town_name;
							ArrayPlayer[6]=json.ranking[j].experience;
					}
				 	else{
							ArrayPlayer[2]=json.ranking[j].class;
							ArrayPlayer[3]=json.ranking[j].level;
							ArrayPlayer[4]="Mondo_"+window.location.host.substr(0,4).replace(".","")
							ArrayPlayer[5]=json.ranking[j].town_name;
							ArrayPlayer[6]=json.ranking[j].experience;
					}
				
					
					ArrayRisultato.push(ArrayPlayer);
					flag= true;
					console.log(ArrayRisultato);		
			
				}
				
		}
				if(flag==false)
			{
				var ArrayPlayer=[];
				ArrayPlayer[0]=player;
				ArrayPlayer[1]="non trovato";
				ArrayRisultato.push(ArrayPlayer);
			}  i++;if(i<n){ 
	AjaxWest();}
			else{
				function salvaCSV(){
var csvContent = "data:text/csv;charset=utf-8,";
var intestazione=[];
if (scelta=="duels"){
	intestazione=["Name","player_id","duel_win","duel_loss","difference","town_name"," duel experience"];
}					
else{
	intestazione=["Name","player_id","class","level","mondo","town_name"," character experience"];
}			
ArrayRisultato.unshift(intestazione);
ArrayRisultato.forEach(function(infoArray, index){

   dataString = infoArray.join(";");
   csvContent += index < ArrayRisultato.length ? dataString+ "\n" : dataString;

}); 

var encodedUri = encodeURI(csvContent);
//window.open(encodedUri);

var link = document.createElement("a");
    link.href = encodedUri;
	  //set the visibility hidden so it will not effect on your web-layout
    link.style = "visibility:hidden"; var Data=new Date();
		var fileName= "Mondo_"+window.location.host.substr(0,4).replace(".","")+"_"+Data.getDate() + "_" + (Data.getMonth()+1) +"_" + Data.getFullYear()+"_"+scelta;
    link.download = fileName +  ".csv";

    //this part will append the anchor tag and remove it after automatic click
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
} salvaCSV();
			}
	});
	} AjaxWest();},2000);}


	

//

//main
function ini(){
creaButton();

}

//avvia
try { setTimeout(ini,5000);
		  } catch(e) { console.log(e);}

});