There are 1 previous version of this script.
// ==UserScript==
// @name Grondstoffen info (Dutch)
// @namespace Grondstoffen info (Dutch)
// @autor Mitchell
// @description Dit script voegt de informatie van je grondstoffen toe, aan het paneel aan de linkerkant.
// @include http://*ikariam.nl/index.php*
// Version 1.1
// ==/UserScript==
/* Vars naming: character at the beginning of a variable name indicates its type (most of times :P)
s->String
o->Object (DOM nodes, attributes, and anything else)
a->Array
h->Hash
f->float
*/
// Attributes
// Id of the user's City: used to generate url's
var sCityId;
// If of the user's Island: used to generate url's
var sIslandId;
// Increment per second of wood
var fResourcesDelta;
// Increment per secord of the trade resourse
var fTradegoodDelta;
// Name of the user's var: used to put the shortcut
var sTradeGoodName;
// Hash containing HTML id's and class' aattributes: used to put the shortcut
var hTradeGoodsNames = new Array();
hTradeGoodsNames['value_wine'] = 'Wijn';
hTradeGoodsNames['value_marble'] = 'Marmer';
hTradeGoodsNames['value_crystal'] = 'Kristal'; // That's why I cannot just strip the name and I have to create a Hash >o<
hTradeGoodsNames['value_sulfur']= 'Zwavel';
/*
Uses the parameter to find the element containing a certain link to strip part of it.
Used to get sCityId and sIslandId, from the main panel links.
@params: sSearchKey
@return: String
*/
function extractId (sSearchKey) {
var oForm = document.getElementById('changeCityForm');
var aA = oForm.getElementsByTagName('a');
for (var i = 0; i < aA.length; i++) {
if (aA[i].href.match(sSearchKey) != null) {
return aA[i].href.substr(aA[i].href.lastIndexOf("id=")+3,aA[i].href.length);
}
}
}
/*
Obtains the Id of the city
@return: string
*/
function extractIslandId () {
return extractId("view=island");
}
/*
Obtains the If of the island
*/
function extractCityId () {
return extractId("view=city");
}
/*
Creates a new <a href> element and appends it to another given element.
@params: sBaseUrl: basic part of the url,
nParent: element to which the new node will be appended,
sTag: value of the <a href> element. (In fact recieves an Integer)
*/
function addShortCut (sBaseUrl, oParent, sTag) {
oA = document.createElement('a');
oA.href=sBaseUrl+this.sIslandId;
oA.innerHTML = sTag;
oParent.appendChild(oA);
}
/*
Calculates a resource's increment per hour and converts it into integer.
Assumption: sValue is positive
@param: resource increment per second
@return: iInteger
*/
function getResourcePerHour(sValue) {
var fValue = parseFloat(sValue)*3600;
var fDecPart = fValue - Math.floor(fValue);
var iFinalValue;
if (fDecPart > 0.5) iFinalValue = Math.ceil(parseFloat(fValue));
else iFinalValue = Math.floor(parseFloat(fValue));
return iFinalValue;
}
/*
Finds the elements into which add the extra shortcuts and info for resources. Then, adds the links.
DEPRECATED from 0.3...oh good! how I love to say deprecated XD
*/
/*
function setShortCuts() {
var oDiv = document.getElementById("cityResources");
var aLis = oDiv.getElementsByTagName("li");
var oCurrent;
for (var i = 0; i < oDiv.getElementsByTagName("li").length; i++) {
oCurrent = oDiv.getElementsByTagName("li")[i];
var sAttributeName= oCurrent.attributes[1].value;
if (sAttributeName == "wood") {
addShortCut("index.php?view=resource&type=resource&id=",oCurrent,"+"+getResourcePerHour(fResourcesDelta));
}
if (sAttributeName == hTradeGoodsNames[sTradeGoodName]) {
addShortCut("index.php?view=tradegood&type=tradegood&id=",oCurrent,"+"+getResourcePerHour(fTradegoodDelta));
}
}
}
*/
/*
Searchs for a line in a array and then extracts the relevant info from it.
Used to get the value of the resources increment from the page script.
@params: iStartIndex: position at which it starts to search,
sName: name of the variable searched.
aArray: array of strings with the code.
@return: string
*/
function findValue (iStartIndex,sName, aArray) {
for (i=iStartIndex; i < aArray.length; i++) {
iPos = aArray[i].search(sName);
if (iPos > 0) {
return stripValue(aArray[i]);
}
}
}
/*
Extracts the substring from '=' to the end of sLine.
@params: sLine, string. To avoid Regular expression, here is an example :P "var startResourcesDelta = 0.0141666666667;"
@return: string, e.g. "0.0141666666667"
*/
function stripValue (sLine) {
return sLine.substring(sLine.lastIndexOf("=")+1,sLine.length);
}
/*
Extracts from the script code the name of the element's id of the user's island resource.
Used to know what resource does the island contain in order to put the tag next to it.
Note: i think i don't use this now -> Line:163
@params: sCodeLine, line of the code where the first ocurence of the variable appears.
Obtained looking directly at it, if the scripts is changed this code will fail :/
@return: string
*/
function discoverTradegood (sCodeLine) {
return sCodeLine.substring(sCodeLine.indexOf('(')+2,sCodeLine.indexOf(')')-1);
}
/*
Initializes global variables with the resources information.
*/
function initResourcesValues () {
var nScript = document.getElementById("cityResources").getElementsByTagName("script")[0];
var sCode = nScript.innerHTML;
var aCodeLines = sCode.split(';');
sTradeGoodName = aCodeLines[20].substring(aCodeLines[20].indexOf('(')+2,aCodeLines[20].indexOf(')')-1);
fResourcesDelta = findValue(0,"startResourcesDelta",aCodeLines);
fTradegoodDelta = findValue(0,"startTradegoodDelta",aCodeLines);
}
/* Not Working :P
function scroll (oEvent) {
var iX = oEvent.offsetX;
var iY = oEvent.offsetY;
alert(iX+" + "+iY);
}
*/
/*
Set a small tip with the resources shortcuts. Used now for better interface.
@todo: solve this nicely
solved in v0.4
*/
/*
function addDebugWindow () {
GM_addStyle(".debugDiv {position:absolute; top:2px; left:2px; z-index:100; text-align:left; background-color:white; border-style:solid; border-width: 4px 2px 2px 2px; padding: 5px 3px 4px 5px}");
var oDiv = document.getElementById("extraDiv2");
oDiv.className ="debugDiv";
oDiv.id = "debugID"
addShortCut("index.php?view=resource&type=resource&id=",oDiv,"Hout: +"+getResourcePerHour(fResourcesDelta));
var oBr = document.createElement('br');
oDiv.appendChild(oBr);
addShortCut("index.php?view=tradegood&type=tradegood&id=",oDiv,hTradeGoodsNames[sTradeGoodName]+": +" + getResourcePerHour(fTradegoodDelta));
document.body.appendChild(oDiv);
}
*/
/*
Creates a new row to add to a Panel.
@params: sLabel, label for the span element to witch the link will be added
--> For the rest see addShortCut() description.
*/
function newInfoCityElement (sLabel,sBaseUrl, oParent, sTag) {
//GM_addStyle(".RosemerysInfo {text-align: right; color:green;}");
var oNewLi = document.createElement('li');
var oNewSpan = document.createElement('span');
oNewSpan.innerHTML = sLabel;
oNewLi.appendChild(oNewSpan);
addShortCut(sBaseUrl,oNewLi,sTag);
oParent.appendChild(oNewLi);
}
/*
Add Resources info and link into the Main City "information" Div.
@params: sBaseUrl: basic part of the url,
nParent: element to which the new node will be appended,
sTag: value of the <a href> element. (In fact recieves an Integer)
*/
/*
Auxiliar method to inser an element after another. Used when appendChild was causing problems.
*/
function insertAfter(newElement, targetElement) {
var parent = targetElement.parentNode;
if(parent.lastChild == targetElement) {
parent.appendChild(newElement);
} else {
parent.insertBefore(newElement, targetElement.nextSibling);
}
}
/*
Return de first div of the side panel. I really miss Prototype in moments like this :(
*/
function jumpToFirstDinamicDiv () {
var aNodes = document.getElementById('container2').childNodes;
var bFound = false;
var oCurrent;
for (var i = 0; i< aNodes.length && !bFound; i++) {
oCurrent = aNodes[i];
if (oCurrent.className == "dynamic") {
bFound = true;
}
}
//There's always one, at least so far in the game.
return oCurrent;
}
/*
Adds the panel with the resources elements.
*/
function addDinamicPanel () {
var oParent = document.getElementById("container2");
var oPanel = document.createElement('div');
oPanel.className ="dynamic";
var oTitle = document.createElement('h3');
oTitle.innerHTML="Grondstoffen"
oTitle.title="by RecourcesShortcuts Script";
oTitle.className ="header"
var oContent = document.createElement('div');
oContent.className="content";
var oListUl = document.createElement('ul');
oListUl.className="cityinfo";
newInfoCityElement(" Hout:","index.php?view=resource&type=resource&id=",oListUl," +"+getResourcePerHour(fResourcesDelta));
newInfoCityElement(" " + hTradeGoodsNames[sTradeGoodName]+":","index.php?view=tradegood&type=tradegood&id=",oListUl," +"+getResourcePerHour(fTradegoodDelta));
oContent.appendChild(oListUl);
var oFooter = document.createElement('div');
oFooter.className = "footer";
oPanel.appendChild(oTitle);
oPanel.appendChild(oContent);
oPanel.appendChild(oFooter);
var oDiv = jumpToFirstDinamicDiv();
insertAfter(oPanel, oDiv);
}
/*
Starting method. Invoke before everyting else.
*/
function init () {
this.sCityId = extractCityId();
this.sIslandId = extractIslandId();
initResourcesValues();
}
init();
addDinamicPanel();