Travian key binder

By kirilloid Last update Jun 27, 2008 — Installed 2,120 times.
// ==UserScript==
// @author	kirilloid
// @name		Travian key binder
// @namespace	http://userscripts.org/
// @include	http://s*.travian*
// @include	http://rs*.travian*
// @exclude	http://shop.travian*
// ==/UserScript==

function nav_to(url) {
	with (window.location) {
		if (url.match(/\//))
			href = url;
		else
			href = href.replace(/(.*)\/.*/, "$1/"+url);
	}
}

function nav_to_gid(gid) {
	nav_to("build.php?gid="+gid);
}

function is_our() {
	return lmid2.childNodes[1].className == 'txt_menue';
}

if (window.location.href.match('login.php')) {
	document.forms[0].getElementsByTagName('table')[1].rows[0].cells[0].childNodes[2].focus();
}

var msg_rpt_table = false;
var lmid2 = document.getElementById('lmid2');
var vil_nums = false;
var vil_idx = 0;
if (document.getElementById('lright1').firstChild) {
	vil_list = document.getElementById('lright1').getElementsByTagName('table')[0];
	vil_nums = vil_list.rows.length;
	while (vil_list.rows[vil_idx++].cells[0].firstChild.className != "c2") {};
	vil_idx--;
}

//default controller
var controller = {
	actions: {
		tilde: function() { nav_to('dorf1.php') },
		k: function() { nav_to('karte.php') }, // Karte
		backslash: function() { nav_to('statistiken.php') },
		p: function() {	nav_to('berichte.php') }, // rePorts
		i: function() {	nav_to('nachrichten.php') }, // Igm
		q: function() {	nav_to('dorf3.php') }, // just dorf3 =P
		
		a: function() {	nav_to('a2b.php') },
		z: function() {	nav_to('allianz.php') },
		f: function() {	nav_to('spieler.php?s=1') }, // proFile
		esc: function() { nav_to('login.php') },
		
		b: function() { nav_to_gid(19) }, // Barracks
		s: function() { nav_to_gid(20) }, // Stable
		w: function() { nav_to_gid(21) }, // Workshop
		h: function() { nav_to_gid(24) }, // townHall
		m: function() { nav_to_gid(17) }, // Marketplace
		n: function() { nav_to('build.php?gid=17&t=3') }, // Npc-trader
		x: function() { nav_to('build.php?id=40') }, // wall
		r: function() { nav_to('build.php?id=39') }, // Rally point
		v: function() { nav_to_gid(37) }, // taVern
		l: function() { nav_to_gid(12) }, // bLacksmith
		o: function() { nav_to_gid(13) }, // armOry
		t: function() { nav_to_gid(36) }, // Trapper
		home: function() { nav_to_gid(15) }, // Main Building
		
		up: function() { if(vil_nums){vil_idx = vil_idx?--vil_idx:vil_nums-1; nav_to(vil_list.rows[vil_idx].cells[0].lastChild.href)} },
		down: function() { if(vil_nums){vil_idx = ++vil_idx % vil_nums; nav_to(vil_list.rows[vil_idx].cells[0].lastChild.href)} },
		right: function() {},
		left: function() {},
		num: function() {},
		numpad: function() {},
		wildcard: function() {},
		del: function() {},
		ins: function() {},
		plus: function() {},
		enter: function() {},
		F4: function() {},
		
		d: function() {},
		c: function() {},
		u: function() {},
	}
}

if (window.location.href.match(/nachrichten\.php([?&]s=\d+)?([?&]t=\d)?(\&o=1)?$/)
||	window.location.href.match(/berichte\.php([?&]s=\d+)?(\&o=1)?$/)) {
	if (document.forms[0]) {
		var msg_rpt_table = document.forms[0].getElementsByTagName('table')[0];
		// explicit date sorting order
		var send_text = msg_rpt_table.rows[0].cells[msg_rpt_table.rows[0].cells.length-1].firstChild;
		if (send_text.firstChild
		&&	send_text.firstChild.firstChild) {
			send_text.firstChild.firstChild.innerHTML +=
				window.location.href.match(/\&o=1/)?' ↑':' ↓';
		}		
		if (document.forms[0].name != 'adressbuch') { // we're not writing msg
			msg_rpt_table.rows[0].insertCell(0).innerHTML = '#';
			for(i=0; i<10; i++) msg_rpt_table.rows[i+1].insertCell(0).innerHTML = (i+1);
			msg_rpt_table.rows[11].insertCell(0).innerHTML = '*';
			
			var nav_elt = msg_rpt_table.rows[11].cells[msg_rpt_table.rows[11].cells.length-1];
			with (controller.actions) {
				num = function(n) { nav_to(msg_rpt_table.rows[n].cells[2].firstChild.href) };
				left = function() { if(nav_elt.childNodes[0].href) nav_to(nav_elt.childNodes[0].href) };
				right = function() { if(nav_elt.childNodes[1].href) nav_to(nav_elt.childNodes[1].href) };
				numpad = function(n) { msg_rpt_table.rows[n].cells[1].firstChild.click() };
				//						document.getElementsByName('n'+n))[0].click()
				wildcard = function() { for(i=0;i<10;i++) msg_rpt_table.rows[i+1].cells[1].firstChild.click() };
				del = function() { document.getElementsByName('del')[0].click() };
				ins = function() { document.getElementsByName('archive')[0].click() };
				s = function() { nav_to(msg_rpt_table.rows[0].cells[msg_rpt_table.rows[0].cells.length-1].firstChild.href) };
			}
		} else { // we're writing msg
//			protect_box(['an', 'be', 'message']);
			with (controller.actions) {
				a = function() {
					document.forms[1].getElementsByTagName('table')[0].rows[1].cells[3].firstChild.click();
					//toggleFriendsList(); return false;
				};
			}
		}
	}
}

if (window.location.href.match(/berichte\.php\?id/)) {
	var troops_tables = lmid2.lastChild.rows[3].cells[0].getElementsByTagName('table');
	var xcell = troops_tables[0].rows[0].cells[1];
	var url_e = 'a2b.php?z='+xcell.lastChild.href.replace(/^.*d=(\d+).*$/, "$1");
	controller.actions.e = function() { nav_to(url_e) };
	xcell.innerHTML += ' <strong>[<a href="'+url_e+'">E</a>]</strong>';
	for (i=1; i<troops_tables.length; i++) {
		xcell = troops_tables[i].rows[0].cells[1];
		if (xcell.childNodes.length > 1) {
			var url_d = 'a2b.php?z='+xcell.lastChild.href.replace(/^.*d=(\d+).*$/, "$1");
			controller.actions.d = function() { nav_to(url_d) };
			xcell.innerHTML += ' <strong>[<a href="'+url_d+'">D</a>]</strong>';
		}
	}
}

if (window.location.href.match(/nachrichten\.php\?id/)) {
	var msg_rpt_table = document.forms[0].getElementsByTagName('table')[0];
	var s = msg_rpt_table.rows[4].cells[1].innerHTML;
	s = s.replace(/(http[^\s<]*)/g, "<a href=\"$1\">$1</a>");
	msg_rpt_table.rows[4].cells[1].innerHTML = s;
	with (controller.actions) {
		r = function() { document.forms[0].submit() }
	}
}

function rebuild_od() {
	var counter = 0;
	od.innerHTML = '';
	var links_idx = 0;
	delete links;
	var links = new Array();
	for(x=0; x<7; x++) {
		for(y=0; y<7; y++) {
			if(!document.getElementById('i_'+x+'_'+y).src.replace(/^.*?(\w+)\.gif/, "$1").match(/t\d/)) {
				links[counter++] = document.getElementById('a_'+x+'_'+y).href;
				od.innerHTML +=
					'<span style="position:absolute; right:'+(463-x*37-y*36)+'px; top:'+(221+(x-y)*20)+'px;'
				+	' z-index:1000; font-weight:bold; padding:0px 1px;">' + counter + '</span>';
			}
		}
	}
	var xspans = od.getElementsByTagName('span');
	f_before = function() {
		if(links_idx) with (xspans[links_idx-1].style) {
			backgroundColor = 'transparent';
		}
	}
	f_after = function() {
		if (links_idx > counter) {
			links_idx = 0;
		} else {
			if(links_idx) with (xspans[links_idx-1].style) {
				backgroundColor = 'yellow';
			}
		}		
	}
	with (controller.actions) {
		num = function(n) {
			f_before();
			links_idx *= 10;
			links_idx += (n==10?0:n);			
			if (links_idx > counter) links_idx = counter;
			f_after();
		};
		F4 = function() { f_before(); links_idx=0; }
		del = F4;
		numpad = function(n) { nav_to(links[n-1]); }
		enter = function() { if(links_idx)nav_to(links[links_idx-1]) } 
	}
}

if (window.location.href.match(/karte\.php([?&]newdid=\d+)?([?&]z=\d+)?$/)) {
	var mdiv = document.getElementById('map_content').childNodes[1];
	mdiv.innerHTML += '<div style="width:100%;height:100%" id="over_display"></div>';
	var od = document.getElementById('over_display');
	for(i=1; i<=4; i++) {
		document.getElementById('ma_n'+i).addEventListener('click',
			function(e){setTimeout(rebuild_od, 500)}, false);
		document.getElementById('ma_n'+i+'p7').addEventListener('click',
			function(e){setTimeout(rebuild_od, 500)}, false);
	}
	with (controller.actions) {
		left =	function() { setTimeout(rebuild_od, 500) };
		right =	function() { setTimeout(rebuild_od, 500) };
		up =	function() { setTimeout(rebuild_od, 500) };
		down =	function() { setTimeout(rebuild_od, 500) };
		x = function() { document.getElementsByName('xp')[0].focus() };
	}
	rebuild_od();
}

if (window.location.href.match(/karte\.php\?d/)) {
	var xdivs = lmid2.getElementsByTagName('div');
	var map_details_troops = xdivs[xdivs.length-4];
	if (map_details_troops.className != 'map_details_troops')
		map_details_troops = false;
	else {
		xcell = map_details_troops.lastChild.rows[0].cells[1];
		xcell.innerHTML = '<ol style="margin:0px; padding-left:8px;">' + xcell.innerHTML + '</ol>';
	}
	with (controller.actions) {
		a = function() {
			try {
				nav_to(lmid2.lastChild.lastChild.rows[1].cells[0].firstChild.href);
			} catch(e) {
				alert('Cannot attack!');
			}
		};
		c = function() { nav_to(lmid2.lastChild.lastChild.rows[0].cells[0].firstChild.href) };
		num = function(n) {
			if ((n >= 1) && (n <= 5) && map_details_troops) try {
				window.location.href = 
					xcell.firstChild.childNodes[n-1].firstChild.href;
			} catch(e) { alert('out of range') }
		}
	}	
}

if (window.location.href.match('dorf1.php')) {
	with (controller.actions) {
		tilde = function() { nav_to('dorf2.php') };
	}
}

if (window.location.href.match('dorf3.php')) {
	links = lmid2.firstChild.getElementsByTagName('a');
	for(i=0; i<links.length; i++)
		links[i].innerHTML = (i+1) + '.' + links[i].innerHTML;
	with (controller.actions) {
		num = function(n) { nav_to('dorf3.php'+(n==1?'':'?s='+n)) }
	}
}

if (window.location.href.match('statistiken.php')) {
	with (controller.actions) {
		v = function() { nav_to('statistiken.php?id=2') };
		a = function() { nav_to('statistiken.php?id=4') };
		o = function() { nav_to('statistiken.php?id=31') };
		d = function() { nav_to('statistiken.php?id=32') };
		plus = function() { nav_to('statistiken.php?id=5') };
		c = function() { nav_to('statistiken.php?id=0') };
		t = function() { nav_to('statistiken.php?id=7') };
	}
	if (document.forms[0]) {
		xcell = document.forms[0].getElementsByTagName('table')[2].rows[0].cells[6];
		with (controller.actions) {
			left = function() { nav_to(xcell.firstChild.href) };
			right = function() { nav_to(xcell.childNodes[2].href) };
		}
//		protect_box(['rang', 'spieler']);
	}
}

if (window.location.href.match('build.php?')) {
	var divs = lmid2.getElementsByTagName('div');
	with(controller.actions) {
		u = function() { try {nav_to(divs[divs.length-1].getElementsByTagName('a')[0].href)}catch(e){} };
		// upgrade building if possible
	}
}

if (window.location.href.match('a2b.php')) {
	var form_tables = document.forms[0].getElementsByTagName('table');
	var xdivs_atk_type = form_tables[2].rows[0].cells[0].getElementsByTagName('div');
	var img_name = form_tables[1].rows[0].cells[0].firstChild.src.replace(/^.*?(\d+)\.gif$/, "$1");
	switch(Math.floor(parseInt(img_name)/10)) {
		case 0: units = {off:0xb20, def:0x011, spy:0x002}; break;
		case 1: units = {off:0xb01, def:0x030, spy:0x002}; break;
		case 2: units = {off:0xb12, def:0x021, spy:0x100}; break;
	}
	function select_troops(troops_mask) {
		for(i=0; i<10; i++) {
			var xrow = form_tables[1].rows[Math.floor(i/4)].cells;
			if (troops_mask & (1<<i)) {
				xrow[(i%4)*3+1].firstChild.value = 
				xrow[(i%4)*3+2].firstChild.innerHTML.replace(/[)(]/g,'');
			} else
				xrow[(i%4)*3+1].firstChild.value = '';
		}
		if ((troops_mask & (1<<11))
		&&	(form_tables[1].rows[2].cells.length > 8)) { // hero is possible
			form_tables[1].rows[2].cells[8].firstChild.value = 1;
		}
	}
	with (controller.actions) {
//		d = function() { xdivs_atk_type[0].firstChild.click() };
		a = function() { xdivs_atk_type[1].firstChild.click() };
		r = function() { xdivs_atk_type[2].firstChild.click() };
		x = function() { document.getElementsByName('x')[0].focus() };
		o = function() { select_troops(units.off) };
		d = function() { select_troops(units.def) };
		s = function() { select_troops(units.spy) };
//		c = function() { ;/* select all off + capturing */ };
	}
}

if (window.location.href.match('allianz.php') && is_our()) { // our allianz
	with (controller.actions) {
		a = function() { nav_to('allianz.php?s=3') };
		n = function() { nav_to('allianz.php?s=4') };
		o = function() { nav_to('allianz.php?s=5') };
		if (window.location.href.match('s=3')) {
			num = function(n) { nav_to(lmid2.lastChild.rows[n+1].cells[1].firstChild.href) }
		}
	}
}

if (window.location.href.match('spieler.php') && is_our()) { // our allianz
	links = lmid2.childNodes[1].getElementsByTagName('a');
	for(i=0; i<links.length; i++)
		links[i].innerHTML = (i+1) + '.' + links[i].innerHTML;	
	with (controller.actions) {
		num = function(n) { if(n<=5) nav_to(links[n-1].href) }
	}
}

var xtable = lmid2.getElementsByTagName('table')[0];
try { // in tavern	
	for (i=1; i<=5; i++) { xtable.rows[i].cells[3].childNodes[1].innerHTML = i }
	with (controller.actions) {
		num = function(n) {
			if (n <= 5) nav_to(xtable.rows[n].cells[3].childNodes[1].href);
		}
	}
} catch(e) {};

// key binding
addEventListener('keyup', function (e){
	if ( typeof e == "undefined" ) e = window.event;
	var wkey = e.keyCode;
	var mods = e.which;
	switch (wkey) {
//		case 9: controller.actions.tab(); break;
		case 13: controller.actions.enter(); break;
		case 27: controller.actions.esc(); break;
//		case 32: controller.actions.space(); break;
//		case 33: controller.actions.pageUp(); break;
//		case 34: controller.actions.pageDown(); break;
//		case 35: controller.actions.end(); break;
		case 36: controller.actions.home(); break;
		case 37: controller.actions.left(); break;
		case 38: controller.actions.up(); break;
		case 39: controller.actions.right(); break;
		case 40: controller.actions.down(); break;
		case 45: controller.actions.ins(); break;
		case 46: controller.actions.del(); break;
		case 106: controller.actions.wildcard(); break;
		case 107: controller.actions.plus(); break;
//		case 109: controller.actions.minus(); break;
//		case 112: controller.actions.F1(); break;
//		case 113: controller.actions.F2(); break;
//		case 114: controller.actions.F3(); break;
		case 115: controller.actions.F4(); break;
//		case 116: controller.actions.F5(); break;
//		case 117: controller.actions.F6(); break;
//		case 118: controller.actions.F7(); break;
//		case 119: controller.actions.F8(); break;
//		case 120: controller.actions.F9(); break;
//		case 121: controller.actions.F10(); break;
//		case 122: controller.actions.F11(); break;
//		case 123: controller.actions.F12(); break;
		case 192: controller.actions.tilde(); break;
		case 220: controller.actions.backslash(); break;
		
		case 49: case 50: case 51: case 52: case 53: case 54: case 55: case 56: case 57: case 48:
			var n = wkey - 48;
			controller.actions.num(n?n:10);
		break;
		case 97: case 98: case 99: case 100: case 101: case 102: case 103: case 104: case 105: case 96:
			var n = wkey - 96;
			controller.actions.numpad(n?n:10);
		break;

		case 65: controller.actions.a(); break;
		case 66: controller.actions.b(); break;
		case 67: controller.actions.c(); break;
		case 68: controller.actions.d(); break;
		case 69: controller.actions.e(); break;
		case 70: controller.actions.f(); break;
//		case 71: controller.actions.g(); break;
		case 72: controller.actions.h(); break;
		case 73: controller.actions.i(); break;
//		case 73: controller.actions.j(); break;		
		case 75: controller.actions.k(); break;
		case 76: controller.actions.l(); break;
		case 77: controller.actions.m(); break;
		case 78: controller.actions.n(); break;
		case 79: controller.actions.o(); break;
		case 80: controller.actions.p(); break;
		case 81: controller.actions.q(); break;		
		case 82: controller.actions.r(); break;
		case 83: controller.actions.s(); break;
		case 84: controller.actions.t(); break;
		case 85: controller.actions.u(); break;
		case 86: controller.actions.v(); break;
		case 87: controller.actions.w(); break;
		case 88: controller.actions.x(); break;
//		case 89: controller.actions.y(); break;
		case 90: controller.actions.z(); break;		
	default:
		window.status = wkey + (mods ? '+'+mods : '');
	}
}, false);

for(f=0; f<document.forms.length; f++)
	for(i=0; i<document.forms[f].elements.length; i++)
		document.forms[f].elements[i].addEventListener(
			'keyup', function(e){e.stopPropagation()}, false
		);