rsenderakgmail.com / UniverseMapJavaScriptV1Fix

// ==UserScript==
// @name           UniverseMapJavaScriptV1Fix
// @namespace      seko.eea.sk
// @description    Fix Java Script V1 Universe Map for FF 20.0+; resizes and moves the window; draws center cross; now works with new UI
// @include        http://*.war-facts.com/extras/view_universe.php?*
// @grant          GM_log
// @grant          GM_xmlhttpRequest
// @version        1.6
// ==/UserScript==

// if true, tries to resize map to new width/height (see bellow)
var resizeMap = true;

// if true, tries to fix the problem http://www.war-facts.com/forum_view.php?forum=4&thread=4295
var fixMap = true;

// draw center cross (for orientation purpose)
var drawCross = true;

// local wfs3 server ready
var localServerReady = true

// new map dimensions
var newWidth = 950;
var newHeight = 950;
var newX = 80;
var newY = 60;

WFS3_URL = "http://localhost:8080/wfs3"

// map symbols
FF0=';font-family:Arial,Helvetica,sans-serif;font-style:normal;font-weight:normal;'
MS_CROSS = {symbol:'*', style:'text-align:center;color:#113333' + FF0 + ';font-size:100%'}
MS_BLOCKADE = {symbol:'!', style:'text-align:center;color:yellow' + FF0 + ';font-size:80%'}
MS_MY_FLEET_SMALL = {symbol:'X', style:'text-align:center;color:white' + FF0 + ';font-size:70%'}
MS_MY_FLEET_BIG = {symbol:'X', style:'text-align:center;color:white' + FF0 + ';font-size:100%'}
MS_WORMHOLE = {symbol:'@', style:'text-align:center;color:#666600' + FF0 + ';font-size:80%'}


function onMapMaxLoad() {
	try {
		if (resizeMap) {
			window.resizeTo(newWidth, newHeight);
			window.moveTo(newX, newY);
			// patch styles
			var styleElemList = document.evaluate("/html/head/style[1]", document, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
			if (styleElemList) {
				var styleElem = styleElemList.iterateNext();
				if (styleElem) {
					var txt = styleElem.innerHTML;
					styleElem.innerHTML = txt.replace(/width:\s*700px;/g, 'width:\s*' + newWidth + 'px;').replace(/height:\s*520px;/g, 'height:' + newHeight + 'px;').replace(/cursor:\s*hand;/g, '');
					var xpos = Number(document.evaluate("/html/body/div/form[@id='navigation']/input[@id='xcoord']", document, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null).iterateNext().value);
					var ypos = Number(document.evaluate("/html/body/div/form[@id='navigation']/input[@id='ycoord']", document, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null).iterateNext().value);
					var parent = document.evaluate("/html/body/div/div[@id='map']", document, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null).iterateNext();
                    if (drawCross) { 
                        addLocation('cross0', 1992, 1992, 0, styleElem, parent, 'CROSS', null, MS_CROSS.symbol, MS_CROSS.style);
                    }
                    if (localServerReady) {
                        requestAndDrawAll(styleElem, parent, xpos, ypos);
                    }
    			}
			}
		}
		if (fixMap) {
			fixJavascript()
		}
	} catch (ex) {
		var msg = 'OnMapMaxLoad error: ' + String(ex);
		GM_log(msg)
		alert(msg);
	}
    
    function requestAndDrawAll(styleElem, parent, xpos, ypos) {
        var minx = getXcoords(xpos, 0);
        var maxx = getXcoords(xpos, 4000);
        var miny = getYcoords(ypos, 4000);
        var maxy = getYcoords(ypos, 0);
        GM_xmlhttpRequest({
            method : "GET",
            url : WFS3_URL + "/blockades?minx=" + minx + "&maxx=" + maxx + "&miny=" + miny + "&maxy=" + maxy,
            headers : {"Accept" : "application/json"},
            onload : function(rsp) {drawBlockades(styleElem, parent, xpos, ypos, rsp);},
            onerror: function(rsp) {console.log(rsp);}
        });
        GM_xmlhttpRequest({
            method : "GET",
            url : WFS3_URL + "/myfleets?minx=" + minx + "&maxx=" + maxx + "&miny=" + miny + "&maxy=" + maxy,
            headers : {"Accept" : "application/json"},
            onload : function(rsp) {drawMyFleets(styleElem, parent, xpos, ypos, rsp);},
            onerror: function(rsp) {console.log(rsp);}
        });
        GM_xmlhttpRequest({
            method : "GET",
            url : WFS3_URL + "/wormholes?minx=" + minx + "&maxx=" + maxx + "&miny=" + miny + "&maxy=" + maxy,
            headers : {"Accept" : "application/json"},
            onload : function(rsp) {drawWormholes(styleElem, parent, xpos, ypos, rsp);},
            onerror: function(rsp) {console.log(rsp);}
        });
    }

    function drawBlockades(styleElem, parent, xpos, ypos, rsp) {
        r = JSON.parse(rsp.responseText);
        for (i in r.blockades) {
            b = r.blockades[i]
            addLocation('bl' + i, 
                getXloc(xpos, b.x) + 6, 
                getYloc(ypos, b.y) - 10, 
                90000, styleElem, parent, b.ts + '\n' + b.reason + '\n' + b.note, 
                null, MS_BLOCKADE.symbol, MS_BLOCKADE.style);
        }
    }
    
    function drawMyFleets(styleElem, parent, xpos, ypos, rsp) {
        r = JSON.parse(rsp.responseText);
        for (i in r.fleets) {
            f = r.fleets[i]
            ms = (f.ships > 1 || f.tonnage > 1.1) ? MS_MY_FLEET_BIG : MS_MY_FLEET_SMALL;
            addLocation('mfl' + i, 
                getXloc(xpos, f.x), 
                getYloc(ypos, f.y) - 10, 
                80000, styleElem, parent, 
                f.name + ' (#' + f.fid + ')\n' + f.ships + ' / ' + f.tonnage + '\n' + f.location, 
                null, ms.symbol, ms.style);
        }
    }

    function drawWormholes(styleElem, parent, xpos, ypos, rsp) {
        r = JSON.parse(rsp.responseText);
        for (i in r.wormholes) {
            w = r.wormholes[i]
            addLocation('wh' + i, 
                getXloc(xpos, w.x) - 10, 
                getYloc(ypos, w.y), 
                70000, styleElem, parent, w.name + ' (' + w.type + '-way)\nto ' + w.end, 
                null, MS_WORMHOLE.symbol, MS_WORMHOLE.style);
        }
    }

    function getIcon(icon) {
        if (icon=='cross') return '/gfx/map/ar_in.gif';
        if (icon=='blockade') return '/gfx/empire-war.gif';
        if (icon=='fleet') return '/gfx/dev.gif';
        return '/gfx/dev.gif';
    }

	function addLocation(locId, locX, locY, zIndex, styleElem, parent, label, icon, text, style) {
        var ftxt = '#'+locId+' {left: '+locX+'px; top: '+locY+'px; z-index: '+zIndex + ";}\n";
        var txt = styleElem.innerHTML;
        txt = txt.replace(/cursor:\s*hand;/g, '');
        styleElem.innerHTML = txt.replace(/#i0\s*{\s*/, ftxt + '#i0{');
        var spanElem = document.createElement('span');
        spanElem.setAttribute('id', locId);
        spanElem.setAttribute('type','button');
        spanElem.title = label;
        var imgElem;
        if (icon) {
            imgElem = document.createElement('img');
            imgElem.src = getIcon(icon);
        } else {
            imgElem = document.createTextNode(text);
            if (style) {
                spanElem.setAttribute('style', style);
            }
        }
        spanElem.appendChild(imgElem);
        parent.appendChild(spanElem);
	}

	function addStr(str, sep) {
		return str ? (sep + str) : '';
	}

	function getXloc(xpos, xx) {
		return Math.round(2000 - (xpos - xx) * 0.08);
	}

	function getYloc(ypos, yy) {
		return Math.round(2000 + (ypos - yy) * 0.08);
	}

	function getXcoords(xpos, xloc) {
		return Math.round(xpos + (xloc - 2000) / 0.08);
	}

	function getYcoords(ypos, yloc) {
		return Math.round(ypos - (yloc - 2000) / 0.08);
	}

	function fixJavascript() {
		// we put the script in a new script element in the DOM
		var script = document.createElement('script'); // create the script element
		scriptCode = createNewJavascript();
		script.innerHTML = scriptCode.join('\n'); // add the script code to it
		scriptCode.length = 0; // recover the memory we used to build the script
		// this is sort of hard to read, because it's doing 2 things:
		// 1. finds the first <head> tag on the page
		// 2. adds the new script just before the </head> tag
		document.getElementsByTagName('head')[0].appendChild(script);
	}

	function createNewJavascript() {
		var sc = new Array();
		sc.push('var map = document.getElementById("map");');
		sc.push('var mytop = -1 * 2000 + 260;');
		sc.push('var left = -1 * 2000 + 350;');
		sc.push('var mposx = 0;');
		sc.push('var mposy = 0;');
		sc.push('var init = 0;');
		sc.push('var modx = document.getElementById("xcoord");');
		sc.push('var mody = document.getElementById("ycoord");');
		sc.push('var modx_value = parseInt(modx.value)');
		sc.push('var mody_value = parseInt(mody.value)');
		sc.push('var tsize = 2000;');
		sc.push('var bsize = 2000;');
		sc.push('var lsize = 2000;');
		sc.push('var rsize = 2000;');
		sc.push('var scount = 84;');
		sc.push('var mapElement = null;');
		sc.push('function mscroll(e) {');
		sc.push('	if (!e) var e = window.event;');
		sc.push('	if (init == 0) {');
		sc.push('		init = 1;');
		sc.push('	} else {');
		sc.push('		mytop = mytop + (e.clientY - mposy);');
		sc.push('		map.style.top = mytop;');
		sc.push('		left = left  + (e.clientX - mposx);');
		sc.push('		map.style.left = left;');
		sc.push('		modx.value = Math.round(modx_value - (left - 350 + 2000) / 0.08);');
		sc.push('		mody.value = Math.round(mody_value + (mytop - 260 + 2000) / 0.08);');
		sc.push('	}');
		sc.push('	if (left > (-1 * 2000 + lsize) ) postload("left");');
		sc.push('	else if (left < ( -1 * 2000 - rsize + 700) ) postload("right");');
		sc.push('	else if (mytop > (-1 * 2000 + tsize) ) postload("top");');
		sc.push('	else if (mytop < ( -1 * 2000 - bsize + 520) ) postload("bottom");');
		sc.push('	mposx = e.clientX; mposy = e.clientY;');
		sc.push('}');
		sc.push('function postload(where) {');
		sc.push('	document.onmousemove = null;');
		sc.push('	var status = document.getElementById("status");');
		sc.push('	status.innerHTML = "LOADING ...";');
		sc.push('	document.getElementById("navigation").submit();');
		sc.push('}');
		sc.push('document.onmousedown = function() { document.onmousemove = mscroll; }');
		sc.push('document.onmouseup = function() { document.onmousemove = null; init = 0; }');
		return sc;
	}
}

window.addEventListener("load", onMapMaxLoad, false);