Travian: Resource++ v4

By FDisk Last update Mar 11, 2009 — Installed 6,800 times. Daily Installs: 2, 6, 3, 5, 4, 8, 5, 0, 3, 4, 5, 3, 2, 5, 2, 1, 5, 0, 4, 6, 3, 1, 5, 3, 2, 7, 1, 8, 1, 2, 6, 4

There are 2 previous versions of this script.

// ==UserScript==
// @author		usr8472@gmail.com cleaned by FDisk
// @name		Travian: Resource++ v4 
// @namespace	http://userscripts.org/
// @description	Travian resource summary, overflow of resources. Cleaned version works with Travian Beyond v3. Rewrite of original with HTML DOM methods instead of innerHTML. Works in Opera. Please report bugs.
// @include		http://*.travian.*/dorf1.php*
// @include		http://*.travian3.*php*
// @exclude 	http://forum.travian.*
// @exclude 	http://*.travian.*/index.php*
// @exclude 	http://*.travian.*/anleitung.php*
// @exclude 	http://*.travian.*/login.php*
// @exclude 	http://*.travian.*/logout.php*
// @exclude 	http://*.travian.*/chat.php*
// @exclude 	http://*.travian.*/impressum.php*
// @exclude 	http://*.travian.*/karte2.php*
// @exclude 	http://www.travian.*
// @exclude 	http://forum.travian3.*
// @exclude 	http://*.travian3.*/index.php*
// @exclude 	http://*.travian3.*/anleitung.php*
// @exclude 	http://*.travian3.*/login.php*
// @exclude 	http://*.travian3.*/logout.php*
// @exclude 	http://*.travian3.*/chat.php*
// @exclude 	http://*.travian3.*/impressum.php*
// @exclude 	http://*.travian3.*/karte2.php*
// @exclude 	http://www.travian.*
// ==/UserScript==

var eventSource= (navigator.appName.indexOf== 'Opera') ? document : window; // I use this because I might want to make it cross browser later
eventSource.addEventListener( 'load', function( e ) {  onLoad();  } ,false); //to be run on load of the page

var loc=window.location.href; // the current page href
var lang;

var order=1;// what is the order of the resources lumber to crop or crop to lumber
var fields=[]; //setup the space for the resource names
var resource; //setup the space for the resource data

function onLoad(){ // runs the differnt functions and procedures, 
	
	resource=getResourceInfo(); // gathers the current store, capacity and production of each resource

	gatherFields();//Get fields names
	var tme=menu();//make the menu
	addElement( tme ); // add the menu to the page
	
}


function rowOpera(i,leftval){
  var tol= (leftval<50) ? 150 : 255;
  var sec=155-leftval;
    
  var testmain = document.createElement( 'div' );
  var value = document.createElement( 'div' );
  var bar = document.createElement( 'table' );
  var row= document.createElement( 'tr' );
  var left= document.createElement( 'td' );
  var right= document.createElement( 'td' );
  var simg = document.createElement( 'img' );
    
    testmain.style.width='500px';
    testmain.style.height='16px';

    simg.src="img/un/r/"+(i+1)+".gif";
    simg.className='res';
    simg.style.cssFloat="left";
    simg.title=fields[lang][i];
    simg.alt=fields[lang][i];

    bar.style.cssFloat="left";
    bar.style.width='442px';
    bar.style.height='16px';
    bar.style.backgroundColor="white"
    bar.setAttribute("cellpadding","1");
    bar.setAttribute("cellspacing","0");

    left.id=fields[lang][i]+"left";
    right.id=fields[lang][i]+"right";
    value.id=fields[lang][i]+"value";
    
    
    left.style.width=leftval+'%';
    left.style.backgroundColor=rgb2HEX(sec, sec, parseInt(tol*(leftval/100)) );
    right.style.width=(100-leftval)+'%';
      
    if(leftval>=90){
      value.setAttribute("style","text-decoration:blink;");
      value.style.color='red';
    } else {
      value.setAttribute("style","text-decoration:none;");
    }
    
    value.style.width='40px';
    value.appendChild( document.createTextNode(  leftval+"%"  )  );
    value.style.cssFloat="left"
    value.title=leftval;

  row.appendChild(left);
  row.appendChild(right);
  bar.appendChild(row);

  testmain.appendChild( simg );
  testmain.appendChild( value );
  testmain.appendChild( bar );

  testmain.id='resbar'+i;

  return testmain
}

function returnObjById( id ){ //compatibility, gets the object by id, based on different returns of document 
    if (document.getElementById)
        return document.getElementById(id);
    else if (document.all)
        return document.all[id];
    else if (document.layers)
        return document.layers[id];
}

function getElementsByClassName(oElm, strTagName, strClassName){ // searches the oElm for strTagName objects with strClassName class
    var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
     
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];      
        if(oRegExp.test(oElement.className)){
       arrReturnElements.push(oElement);
        }   
    }
    return (arrReturnElements)
}

function addElement(theElement) { // adds an element to the page either by the center object lmidlc or appends to html
  var htmldoc=returnObjById("lmid2");
	
	if(!htmldoc) {
		htmldoc=document.getElementsByTagName('body')[0];
		if(!htmldoc) return;
		theElement.style.marginLeft="130px";  
		theElement.style.marginBottom="10px";
		theElement.style.width="550px";

		if(loc.indexOf('dorf2')!=-1){
			theElement.style.top="150px";}
		else {
			theElement.style.top="50px";}
	} else {
		theElement.style.width = "500px";
		if(loc.indexOf('karte.php?d=')!=-1){       
			theElement.style.top="428px";       
		}
	}
	
	htmldoc.appendChild(theElement);
}

function gatherFields(){//gathers the name of each resource
	var orgbar=returnObjById('lres0');
	if(!orgbar) {orgbar=getElementsByClassName(document, 'div', 'div4')[0];}
	if(!orgbar) {orgbar=returnObjById('lres');}
	if(!orgbar) return;
	var resbar=orgbar.getElementsByTagName('img');
	var ret=fields[lang]=[resbar[0].title,resbar[1].title,resbar[2].title,resbar[3].title]
	var idbar=orgbar.getElementsByTagName('td');
	if(idbar[1].id.indexOf('1')==-1) order=1;
  return ret;
}

function rgb2HEX(red, green, blue){ // given red green blue values return their hexcode
  var decColor = red + (256 * green) + (65536 * blue);//offset each value and create a new int
	decColor=decColor.toString(16);//convert to a base16 string
	while( decColor.length < 6){//append 0 till it is a 6 length string
		decColor="0"+decColor;
	}
    return "#"+decColor;
}


function getResourceInfo(){ //using the resource values on the page, return the production, store, and capacity values as an array
	var resource=new Array();
	for(var i=1;i<=4;i++) {
		var rtd  = returnObjById("l"+i);
		if(!rtd) return;
		resource.push( [parseInt(rtd.title),parseInt(rtd.textContent.match(/\-?(\d+)\//)),parseInt(rtd.textContent.replace(/(\d+)\//,""))] );
	}
	
	if(!order) { //if the order is 0 return the array as is, if not reverse the array
		return resource;
	} else {
		return resource.reverse();
	}
	
}


function menu(){ //create an resource menu
	var mymenu = document.createElement( 'table' );
	mymenu.id="blocktimer";
	mymenu.setAttribute('style', 'z-index:5;');
	mymenu.setAttribute('cellspacing', 1);
	mymenu.setAttribute('cellpadding', 1);
	mymenu.className = "tbg";
	mymenu.style.marginTop="6px";
	mymenu.style.clear = "both";
	mymenu.style.position="relative"; 

	row = document.createElement("tr");
	cell = document.createElement("td");
	cell.setAttribute("colspan",2);
	cell.setAttribute('align', 'center');
	cell.appendChild(document.createTextNode('Resource Bars'));
	cell.className = "rbg";
	row.appendChild( cell );
	mymenu.appendChild(row);

	for(var i=0; i<4; i++){

    var leftval=parseInt(resource[i][1]/resource[i][2] *100);
    var inRow=rowOpera(i,leftval);
    
    row = document.createElement("tr");
    cell = document.createElement("td");
    cell.setAttribute("colspan",2);
    cell.appendChild(      inRow         );
    row.appendChild(      cell         );
    
    mymenu.appendChild( row );
	}
		
  return mymenu;
}