// ==UserScript==
// @name        CS2 Grab Captain Details
// @namespace   http://black-beach.net
// @version     0.1c 
// @description Extract detail from Captain Pages
// @include     http://*.chosenspace.com/index.php?go=user_info&user_id=*
// ==/UserScript==

// version 0.1c - Uploads data without iframes.

/*	function sleep(ms){
		var dt = new Date();
		dt.setTime(dt.getTime() + ms);
		while (new Date().getTime() < dt.getTime());
	}*/
	
function checkTime(i) {if(i<10){i="0"+i;};return i;}
var nd = new Date();
						d=checkTime(nd.getUTCDate());
						m=checkTime(nd.getUTCMonth()+1);
						y=nd.getUTCFullYear();
						h=checkTime(nd.getUTCHours());
						n=checkTime(nd.getUTCMinutes());
						s=checkTime(nd.getUTCSeconds());
						ms=checkTime(nd.getUTCMilliseconds());
						rtime=y+'/'+m+'/'+d+' '+h+':'+n+':'+s+':'+ms;

var userid = location.href.split('&user_id=')[1];

	var caplist = document.evaluate("//font[@size='2']", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
			var capgrab=caplist.snapshotItem(0);
	if (capgrab) {
			var captain=capgrab.textContent;
			var capstart=capgrab.nextSibling.nextSibling.textContent.split('Start Year: ')[1];
			var status=capgrab.nextSibling.nextSibling.nextSibling.nextSibling.textContent;
			if (status.length<2) {status='Active'};

	var tribelist = document.evaluate("//a[@href[contains(.,'index.php?go=system_info&system_id=')]]", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
			var tribegrab=tribelist.snapshotItem(0);
			var tribe=tribegrab.textContent;
			
	var homelist = document.evaluate("//a[@href[contains(.,'index.php?go=planet_info&planet_id=')]]", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
			var homegrab=homelist.snapshotItem(0);
			var home=homegrab.textContent;
			
	var faclist = document.evaluate("//a[@href[contains(.,'index.php?go=faction_info&faction_id=')]]", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
		if (faclist) {
				var faction = new Array();
				var factionpos = new Array();
				for(var x = 0;x < faclist.snapshotLength; x++){
				var facgrab=faclist.snapshotItem(x);
				faction[x]=facgrab.textContent;
				factionpos[x]=facgrab.previousSibling.textContent.split(' of the ')[0];
				}
		}

		if (faction[0]==undefined) {faction[0]=""; } else {var primary=faction[0];}
		if (faction[1]==undefined) {faction[1]="";}
		if (primary==undefined) {primary="";}
		if (factionpos[0]==undefined) {factionpos[0]="";} else {var primpos=factionpos[0];}
		if (factionpos[1]==undefined) {factionpos[1]="";}
		if (primpos==undefined) {primpos="";}
		
	var online = document.evaluate("//div[contains(., 'Last Online:')]", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
		var ongrab=online.snapshotItem(0);
		if (ongrab) {
		var lonline=ongrab.textContent.split('Last Online: ')[1].split('\n')[0];
		}
		if (lonline==undefined) {lonline="";}
		
	var cLink = userid+ "&rtime=" + rtime+
				"&name=" +captain+ "&start=" +capstart+ "&status=" +status+ "&lonline=" +lonline+
				"&tribe=" +tribe+ "&home=" +home+ "&primpos=" +primpos+
				"&primary=" +primary+ "&faction0=" +faction[0]+ "&faction1=" +faction[1]+
				"&factionpos0=" +factionpos[0]+ "&factionpos1=" +factionpos[1];
	GM_xmlhttpRequest({
		method: "GET",
        url: "http://black-beach.net/db/captainimport.php?userid=" + cLink,
        headers: {
            "Content-Type": "application/x-www-form-urlencoded"
          },
});
//		alert(cLink);
	}	
//userid = userid*1;
//userid = userid + 1;
//sleep(250);
//if (userid<67577) {top.location.assign("http://g1.chosenspace.com/index.php?go=user_info&user_id="+userid);}