// ==UserScript== // @name MyFreeFarm Common functions // @namespace https://github.com/BastianKanaan/GMscripts_MyFreeFarm // @author BastianKanaan // @description Common functions for MyFreeFarm-Scripts // @date 22.02.2015 // @version 2.1.5 // ==/UserScript== const VERSIONfunctionFile = "2.1.5"; var DEVMODE=GM_getValue("devmode",false); var DEVMODE_EVENTS=GM_getValue("devmode_events",false); var DEVMODE_FUNCTION=GM_getValue("devmode_function",false); var DEVMODE_LOG_WARNING=GM_getValue("devmode_log_warning",false); var DEVMODE_LOG_ERROR=GM_getValue("devmode_log_error",false); // PROTOTYPES ************************************************************************************************************ String.prototype.reverse = function(){ try{ var splitext = this.split(""); var revertext = splitext.reverse(); var reversed = revertext.join(""); return reversed; }catch(err){ GM_logError("String.prototype.reverse","","",err); } }; String.prototype.capitalize = function() { try{ return this.charAt(0).toUpperCase() + this.slice(1); }catch(err){ GM_logError("String.prototype.capitalize","","",err); } }; Array.prototype.shuffle = function (){ try{ var i=this.length, j, temp; if (i==0) return; while (--i) { j = Math.floor( Math.random()*(i+1)); temp = this[i]; this[i] = this[j]; this[j] = temp; } temp=null; }catch(err){ GM_logError("Array.prototype.shuffle","","",err); } }; Array.prototype.swap = function (from, to){ try{ var temp; if (this.length==0) return; temp = this[from]; this[from] = this[to]; this[to] = temp; temp=null; }catch(err){ GM_logError("Array.prototype.swap","from, to","",err); } }; Object.prototype.order = new Array(); Object.prototype.sortObj = function(sortfkt,descending){ // sortfkt: // a[0] accesses the object-key "a", a[1] the containing data "Object[a]" // numbers first, then strings : default // numbers : function(a,b){return (parseFloat(a[0],10)-parseFloat(b[0],10));} // dates - ascending sorting : function(a,b){return (getTime(a[0])-getTime(b[0]));} // descending: bool try{ // Setup helping array var sorted = new Array(); for (var i in this){ if(!this.hasOwnProperty(i)){ continue; } sorted.push([i,this[i]]); } // Define default sorting function if(typeof sortfkt!="function"){ sortfkt = function(a,b){ if(isNaN(a[0])){ if(isNaN(b[0])){ return ((a[0]>b[0])-(a[0] number } else { if(isNaN(b[0])){ return -1; } // number < string else { return (a[0]-b[0]); } // both numbers } }; } // Sorting the helping array sorted.sort(sortfkt); if(descending){ sorted.reverse(); } // Reconstruct sorted object this.order.splice(0,this.order.length); for(var j=0;j= 0; i--) { for (var j = document.styleSheets[i].cssRules.length - 1; j >= 0; j--) { if( document.styleSheets[i].cssRules[j].selectorText&&(document.styleSheets[i].cssRules[j].selectorText.match(reg))){ document.styleSheets[i].deleteRule(j); } } } }catch(err){ GM_logError("removeAllCSS","reg="+reg,"i="+i+" j="+j,err); } } //--------------------------------------------------------------------------------------------------------------------------- // TODO name? timeStr, getTimeStr, time2timestr, time2str, 2->To ? function getTimeStr(time,hideSeconds){ // was int2timestr // returns like "1d 12:30:42" try{ var str,help; if(time<0){ time *= -1; } if (hideSeconds) { str = timeFormatHM; } else { str = timeFormatHMS; help = time%60; str = str.replace("sec",((help<10)?"0":"")+Math.floor(help)); } time=time/60; help = time%60; str = str.replace("min",((help<10)?"0":"")+Math.floor(help)); time=time/60; help = time%24; str = str.replace("hour",((help<10)?"0":"")+Math.floor(help)); time=time/24; if (time>=1){ str=Math.floor(time)+"d "+str; } return str; }catch(err){ GM_logError("getTimeStr","time="+time+" hideSeconds="+hideSeconds,"",err); } } // TODO this includes the call of str2seconds(str) // TODO name? getTime, timestr2time, str2time, 2->To ? function getTime(str){ // was timestr2int try{ var help1 = null; var help; if(help=(/(\d+):(\d+):(\d+)/.exec(str))){ help1 = [parseInt(help[1],10),parseInt(help[2],10),parseInt(help[3],10)]; str = str.replace(/(\d+):(\d+):(\d+)/,""); }else if(help=(/(\d+):(\d+)/.exec(str))){ help1 = [parseInt(help[1],10),parseInt(help[2],10),0]; str = str.replace(/(\d+):(\d+)/,""); }else{ help1 = [0,0,0]; } if(help=(/(\d+)\.(\d+)\.(\d+)/.exec(str))){ help[1] = parseInt(help[1],10); help[2] = parseInt(help[2],10)-1; help[3] = parseInt(help[3],10); if(help[3]<100){ help[3] += 2000; } return ((new Date(help[3],help[2],help[1],help1[0],help1[1],help1[2])).getTime()/1000); }else if(help=(/(\d+)\.(\d+)/.exec(str))){ help[1] = parseInt(help[1],10)-1; help[2] = parseInt(help[2],10); if(help[2]<100){ help[2] += 2000; } return ((new Date(help[2],help[1],1,help1[0],help1[1],help1[2])).getTime()/1000); }else if(help=(/(\d+)-(\d+)/.exec(str))){ help[1] = parseInt(help[1],10); help[2] = parseInt(help[2],10)-1; if(help[1]<100){ help[1] += 2000; } return ((new Date(help[1],help[2],1,help1[0],help1[1],help1[2])).getTime()/1000); }else if(help=(/(\d+)/.exec(str))){ help[1] = parseInt(help[1],10); if(help[1]<100){ help[1] += 2000; } return ((new Date(help[1],0,1,help1[0],help1[1],help1[2])).getTime()/1000); }else{ return ((Date.UTC(1970,0,1,help1[0],help1[1],help1[2]))/1000); } }catch(err){ GM_logError("getTime","str="+str,"",err); } } /* function getTime(str){ // was timestr2int var help = null; var help2; if(str.match(/\d+\.\d+\.\d+/)){ if(str.match(/\d+\.\d+\.\d+.*\d+:\d+:\d+/)){ help = [,,,,,]; help2 = (/(\d+)\.(\d+)\.(\d+).*(\d+):(\d+):(\d+)/).exec(str); help[0] = parseInt(help2[3],10); help[1] = parseInt(help2[2],10); help[2] = parseInt(help2[1],10); help[3] = parseInt(help2[4],10); help[4] = parseInt(help2[5],10); help[5] = parseInt(help2[6],10); } else if(str.match(/\d+:\d+:\d+.*\d+\.\d+\.\d+/)){ help = [,,,,,]; help2 = (/(\d+):(\d+):(\d+).*(\d+)\.(\d+)\.(\d+)/).exec(str); help[0] = parseInt(help2[6],10); help[1] = parseInt(help2[5],10); help[2] = parseInt(help2[4],10); help[3] = parseInt(help2[1],10); help[4] = parseInt(help2[2],10); help[5] = parseInt(help2[3],10); } else { if(str.match(/\d+\.\d+\.\d+.*\d+:\d+/)){ help = [,,,,,0]; help2 = (/(\d+)\.(\d+)\.(\d+).*(\d+):(\d+)/).exec(str); help[0] = parseInt(help2[3],10); help[1] = parseInt(help2[2],10); help[2] = parseInt(help2[1],10); help[3] = parseInt(help2[4],10); help[4] = parseInt(help2[5],10); } else if(str.match(/\d+:\d+.*\d+\.\d+\.\d+/)){ help = [,,,,,0]; help2 = (/(\d+):(\d+).*(\d+)\.(\d+)\.(\d+)/).exec(str); help[0] = parseInt(help2[5],10); help[1] = parseInt(help2[4],10); help[2] = parseInt(help2[3],10); help[3] = parseInt(help2[1],10); help[4] = parseInt(help2[2],10); } else { help = [,,,0,0,0]; help2 = (/(\d+)\.(\d+)\.(\d+)/).exec(str); help[0] = parseInt(help2[3],10); help[1] = parseInt(help2[2],10); help[2] = parseInt(help2[1],10); } } if(help){ if(help[0]<100){ help[0]+=2000; } return ((new Date(help[0],help[1]-1,help[2],help[3],help[4],help[5])).getTime()/1000); } } else { if(str.match(/\d+:\d+:\d+/)){ help = [,,]; help2 = (/(\d+):(\d+):(\d+)/).exec(str); help[0] = parseInt(help2[1],10); help[1] = parseInt(help2[2],10); help[2] = parseInt(help2[3],10); } else if(str.match(/\d+:\d+/)){ help = [,,0]; help2 = (/(\d+):(\d+)/).exec(str); help[0] = parseInt(help2[1],10); help[1] = parseInt(help2[2],10); } if(help){ return ((Date.UTC(1970,0,1,help[0],help[1],help[2]))/1000); } } GM_logInfo("getTime","str="+str,"","Failed"); return 0; } */ function getFormattedTime(str){ try{ var help = null; var regDate = dateFormatDMY.replace("day","\\d+").replace("month","\\d+").replace("year","\\d+").replace(/\./g,"\\."); var regTime = timeFormatHMS.replace("hour","\\d+").replace("min","\\d+").replace("sec","\\d+").replace(/\./g,"\\."); if(str.match(new RegExp(regDate))){ if(str.match(new RegExp(regDate+".*"+regTime))){ help = [,,,,,]; help[0] = parseInt((new RegExp(dateFormatDMY.replace("day","\\d+").replace("month","\\d+").replace("year","(\\d+)").replace("hour","\\d+").replace("min","\\d+").replace(".","\\."))).exec(str)[1],10); help[1] = parseInt((new RegExp(dateFormatDMY.replace("day","\\d+").replace("month","(\\d+)").replace("year","\\d+").replace(".","\\."))).exec(str)[1],10); help[2] = parseInt((new RegExp(dateFormatDMY.replace("day","(\\d+)").replace("month","\\d+").replace("year","\\d+").replace(".","\\."))).exec(str)[1],10); str = str.replace(new RegExp(regDate),""); help[3] = parseInt((new RegExp(timeFormatHMS.replace("hour","(\\d+)").replace("min","\\d+").replace("sec","\\d+").replace(".","\\."))).exec(str)[1],10); help[4] = parseInt((new RegExp(timeFormatHMS.replace("hour","\\d+").replace("min","(\\d+)").replace("sec","\\d+").replace(".","\\."))).exec(str)[1],10); help[5] = parseInt((new RegExp(timeFormatHMS.replace("hour","\\d+").replace("min","\\d+").replace("sec","(\\d+)").replace(".","\\."))).exec(str)[1],10); } else if(str.match(new RegExp(regTime+".*"+regDate))){ help = [,,,,,]; help[3] = parseInt((new RegExp(timeFormatHMS.replace("hour","(\\d+)").replace("min","\\d+").replace("sec","\\d+").replace(".","\\."))).exec(str)[1],10); help[4] = parseInt((new RegExp(timeFormatHMS.replace("hour","\\d+").replace("min","(\\d+)").replace("sec","\\d+").replace(".","\\."))).exec(str)[1],10); help[5] = parseInt((new RegExp(timeFormatHMS.replace("hour","\\d+").replace("min","\\d+").replace("sec","(\\d+)").replace(".","\\."))).exec(str)[1],10); str = str.replace(new RegExp(regTime),""); help[0] = parseInt((new RegExp(dateFormatDMY.replace("day","\\d+").replace("month","\\d+").replace("year","(\\d+)").replace("hour","\\d+").replace("min","\\d+").replace(".","\\."))).exec(str)[1],10); help[1] = parseInt((new RegExp(dateFormatDMY.replace("day","\\d+").replace("month","(\\d+)").replace("year","\\d+").replace(".","\\."))).exec(str)[1],10); help[2] = parseInt((new RegExp(dateFormatDMY.replace("day","(\\d+)").replace("month","\\d+").replace("year","\\d+").replace(".","\\."))).exec(str)[1],10); } else { regTime = timeFormatHM.replace("hour","\\d+").replace("min","\\d+").replace(".","\\."); if(str.match(new RegExp(regDate+".*"+regTime))){ help = [,,,,,0]; help[0] = parseInt((new RegExp(dateFormatDMY.replace("day","\\d+").replace("month","\\d+").replace("year","(\\d+)").replace("hour","\\d+").replace("min","\\d+").replace(".","\\."))).exec(str)[1],10); help[1] = parseInt((new RegExp(dateFormatDMY.replace("day","\\d+").replace("month","(\\d+)").replace("year","\\d+").replace(".","\\."))).exec(str)[1],10); help[2] = parseInt((new RegExp(dateFormatDMY.replace("day","(\\d+)").replace("month","\\d+").replace("year","\\d+").replace(".","\\."))).exec(str)[1],10); str = str.replace(new RegExp(regDate),""); help[3] = parseInt((new RegExp(timeFormatHM.replace("hour","(\\d+)").replace("min","\\d+").replace(".","\\."))).exec(str)[1],10); help[4] = parseInt((new RegExp(timeFormatHM.replace("hour","\\d+").replace("min","(\\d+)").replace(".","\\."))).exec(str)[1],10); } else if(str.match(new RegExp(regTime+".*"+regDate))){ help = [,,,,,0]; help[3] = parseInt((new RegExp(timeFormatHM.replace("hour","(\\d+)").replace("min","\\d+").replace(".","\\."))).exec(str)[1],10); help[4] = parseInt((new RegExp(timeFormatHM.replace("hour","\\d+").replace("min","(\\d+)").replace(".","\\."))).exec(str)[1],10); str = str.replace(new RegExp(regTime),""); help[0] = parseInt((new RegExp(dateFormatDMY.replace("day","\\d+").replace("month","\\d+").replace("year","(\\d+)").replace("hour","\\d+").replace("min","\\d+").replace(".","\\."))).exec(str)[1],10); help[1] = parseInt((new RegExp(dateFormatDMY.replace("day","\\d+").replace("month","(\\d+)").replace("year","\\d+").replace(".","\\."))).exec(str)[1],10); help[2] = parseInt((new RegExp(dateFormatDMY.replace("day","(\\d+)").replace("month","\\d+").replace("year","\\d+").replace(".","\\."))).exec(str)[1],10); } else { help = [,,,0,0,0]; help[0] = parseInt((new RegExp(dateFormatDMY.replace("day","\\d+").replace("month","\\d+").replace("year","(\\d+)").replace("hour","\\d+").replace("min","\\d+").replace(".","\\."))).exec(str)[1],10); help[1] = parseInt((new RegExp(dateFormatDMY.replace("day","\\d+").replace("month","(\\d+)").replace("year","\\d+").replace(".","\\."))).exec(str)[1],10); help[2] = parseInt((new RegExp(dateFormatDMY.replace("day","(\\d+)").replace("month","\\d+").replace("year","\\d+").replace(".","\\."))).exec(str)[1],10); } } if(help){ if(help[0]<100){ help[0]+=2000; } return ((new Date(help[0],help[1]-1,help[2],help[3],help[4],help[5])).getTime()/1000); } } else { if(str.match(new RegExp(regTime))){ help = [,,]; help[0] = parseInt((new RegExp(timeFormatHMS.replace("hour","(\\d+)").replace("min","\\d+").replace("sec","\\d+").replace(".","\\."))).exec(str)[1],10); help[1] = parseInt((new RegExp(timeFormatHMS.replace("hour","\\d+").replace("min","(\\d+)").replace("sec","\\d+").replace(".","\\."))).exec(str)[1],10); help[2] = parseInt((new RegExp(timeFormatHMS.replace("hour","\\d+").replace("min","\\d+").replace("sec","(\\d+)").replace(".","\\."))).exec(str)[1],10); } else { regTime = timeFormatHM.replace("hour","\\d+").replace("min","\\d+").replace(".","\\."); if(str.match(new RegExp(regTime))){ help = [,,0]; help[0] = parseInt((new RegExp(timeFormatHM.replace("hour","(\\d+)").replace("min","\\d+").replace(".","\\."))).exec(str)[1],10); help[1] = parseInt((new RegExp(timeFormatHM.replace("hour","\\d+").replace("min","(\\d+)").replace(".","\\."))).exec(str)[1],10); } } if(help){ return ((Date.UTC(1970,0,1,help[0],help[1],help[2]))/1000); } } GM_logWarning("getFormattedTime","str="+str,"","Format did not match. Function returns 0."); return 0; }catch(err){ GM_logError("getFormattedTime","str="+str,"",err); } } //TODO name? getDaytime, time2daytime function getDaytimeStr(time,hideSeconds,paddHours){ // was uhrzeit try{ var time2 = new Date(time*1000); var str,help; if (hideSeconds) { str = timeFormatHM; } else { str = timeFormatHMS; help = time2.getSeconds(); str = str.replace("sec",((help<10)?"0":"")+Math.floor(help)); } help = time2.getMinutes(); str = str.replace("min",((help<10)?"0":"")+Math.floor(help)); help = time2.getHours(); str = str.replace("hour",((!!paddHours && help<10)?"0":"")+Math.floor(help)); return str; }catch(err){ GM_logError("getDaytimeStr","time="+time+" hideSeconds="+hideSeconds+" paddHours="+paddHours,"",err); } } /* function getDateStr(time,hideyear){ //TODO CHANGED THE ARGUMENTS var time2 = new Date(time*1000); str=""; if (time2.getDate()<10) { str += "0"; } str += time2.getDate() +"."; if (time2.getMonth()<9) { str += "0"; } str += (1+time2.getMonth())+"."; if (!hideyear) str += (time2.getFullYear()); return str; } */ //TODO name? getDate, time2date function getDateStr(time,yearformat,padd){ //in seconds //was datum // yearformat: // 0 -> 01.02. // 1 -> 01.02.11 // 2 -> 01.02.2011 (default) // padd: // true -> 01.02.2011 (default) // false -> 1.2.2011 try{ if(typeof yearformat!="number"){ yearformat = 2; } if(typeof padd!="boolean"){ padd = true; } var time2 = new Date(time*1000); var str,help; switch(yearformat){ case 0: str = "day.month"; break; case 1: str = ("day.month.year").replace("year",time2.getFullYear().toString().slice(-2)); break; case 2: str = ("day.month.year").replace("year",time2.getFullYear()); break; } help = time2.getDate(); str = str.replace("day",((padd&&help<10)?"0":"")+help); help = 1+time2.getMonth(); str = str.replace("month",((padd&&help<10)?"0":"")+help); return str; }catch(err){ GM_logError("getDateStr","time="+time+" yearformat="+yearformat+" padd="+padd,"",err); } } function getFormattedDateStr(time,yearformat,padd){ // yearformat: // 0 -> 01.02. // 1 -> 01.02.11 // 2 -> 01.02.2011 (default) // padd: // true -> 01.02.2011 (default) // false -> 1.2.2011 try{ if(typeof yearformat!="number"){ yearformat = 2; } if(typeof padd!="boolean"){ padd = true; } var time2 = new Date(time*1000); var str,help; switch(yearformat){ case 0: str = dateFormatDM; break; case 1: str = dateFormatDMY.replace("year",time2.getFullYear().toString().slice(-2)); break; case 2: str = dateFormatDMY.replace("year",time2.getFullYear()); break; } help = time2.getDate(); str = str.replace("day",((padd&&help<10)?"0":"")+help); help = 1+time2.getMonth(); str = str.replace("month",((padd&&help<10)?"0":"")+help); return str; }catch(err){ GM_logError("getFormattedDateStr","time="+time+" yearformat="+yearformat+" padd="+padd,"",err); } } //TODO name? getDateText function getDateText(time,yearformat){ // was datumDay try{ var time2 = Math.floor(time); var today = new Date(new Date().getFullYear(), new Date().getMonth(),new Date().getDate(),0,0,0)/1000; //begin of this day. if (time2 < today){ return getFormattedDateStr(time,yearformat); } else if (time2 < (today+(1*24*60*60))){ return getText("day0"); } else if (getText("day1") && (time2 < (today+(2*24*60*60)))){ return getText("day1"); } else if (getText("day2") && (time2 < (today+(3*24*60*60)))){ return getText("day2"); } else { return getFormattedDateStr(time,yearformat); } }catch(err){ GM_logError("getDateText","time="+time+" yearformat="+yearformat,"",err); } } function countDays(time1,time2){ //in seconds // returns number of days from 1 to 2. for example 0 if both on one day. // calculate times of midnight try{ time1 = new Date(Math.round(time1*1000)); time1 = ((new Date(time1.getFullYear(),time1.getMonth(),time1.getDate())).getTime())/1000; time2 = new Date(Math.round(time2*1000)); time2 = ((new Date(time2.getFullYear(),time2.getMonth(),time2.getDate())).getTime())/1000; return Math.round((time2-time1)/86400); }catch(err){ GM_logError("countDays","time1="+time1+" time2="+time2,"",err); } } //--------------------------------------------------------------------------------------------------------------------------- function explode(str,debugName,defaultReturn,depth){ try{ if(debugName==undefined){ debugName = ""; GM_logWarning("explode","debugName="+debugName,"","DebugName not set."); }else if(typeof defaultReturn==undefined){ GM_logWarning("explode","debugName="+debugName,"","DefaultReturn not set."); } if(str==undefined){ throw ("Argument is undefined."); } if(typeof str != "number" && typeof str != "string"){ throw ("Argument is not a string nor a number."); } // if(str==""){ return undefined; } var help = eval('(' + str + ')'); // if(defaultReturn==undefined){ return help; // } }catch(err){ if(typeof defaultReturn==undefined){ GM_logError("explode","debugName="+debugName+" defaultReturn=","",err); throw ("ERROR in function 'explode'"); } else { GM_logWarning("explode","debugName="+debugName+" defaultReturn="+JSON.stringify(defaultReturn),"","Function returns given default. "+err); return defaultReturn; } } /* try{ function recusiveCheck(h,d,lvl,dpth){ var i, correct=true; for(var i in d){ if(!d.hasOwnProperty(i)){continue;} if(!((typeof h[i])==(typeof d[i]))||!((h[i] instanceof Array)==(d[i] instanceof Array))){ return false; } if(typeof d[i]=="object" && lvl1&&!recusiveCheck(help,defaultReturn,0,depth)){ correct=false; } } if(correct){ return help; }else{ return defaultReturn; } }catch(err){ return help; } */ } function implode(arr,debugName){ try{ if(debugName==undefined){ GM_logWarning("implode","arr="+JSON.stringify(arr),"","DebugName not set."); debugName = ""; } var line = new String(); var InternalCounter = -1; var NoKey = new Boolean(false); if (arr == undefined){ return ""; } if (typeof arr == "function"){ return "function"; } if (typeof arr == "string"){ return arr; } if (typeof arr == "boolean"){ return arr.toString(); } if (typeof arr == "number"){ return arr.toString(); } if (typeof arr != "object"){ throw("Argument is not an Object or Array. Type is " + typeof arr +".\n"); } var type = (arr instanceof Array); //true->array | false->object line = (type)?"[":"{"; for(var i in arr ){ try{ if(!arr.hasOwnProperty(i)){continue;} InternalCounter++; if (type){ while (i>InternalCounter){ line += ","; InternalCounter++; } }else{ line += "\"" + i + "\":"; } if (typeof arr[i] == "number" || typeof arr[i] == "boolean"){ line += arr[i]; } else if (typeof arr[i] == "string"){ line += "\"" + arr[i].replace(/\\/g,"\\\\").replace(/\"/g,"\\\"") + "\""; } else if(typeof arr[i] == "undefined"){ line += 'undefined'; } else if(arr[i]==null){ line += 'null'; } else { line += implode(arr[i],debugName); } line += ","; }catch(err){ // GM_logError("implode","debugName="+debugName,"i="+i,err); } } var endChar = line.substring(line.length-1,line.length); return line.substring(0,line.length-1) + (("{[".indexOf(endChar)!=-1)? endChar:"")+ ((type)?"]":"}"); } catch (err){ GM_logError("implode","debugName="+debugName,"",err); throw ("ERROR in function 'implode'"); } } function enc(str,sh) { try{ var encoded = ""; for (i=0; i"; } else if(typeof arr[i] == "undefined"){ str += line + "[" + i + "] = " + "
"; } else { str += print_r_time(arr[i],line +"[" + i + "]"); } }catch(err){ GM_logError("print_r_time","","i="+i,err); continue; } } if(!i){str += line + " = " + "undefined" + "
";} return str; } catch (err){ GM_logError("print_r_time","","",err); throw ("ERROR in function 'print_r_time'"); } } function Log(obj,pre){ try{ if(true){ if(pre==undefined){ pre=""; } if(typeof(obj)=="object"){ for(var v in obj){ if(!obj.hasOwnProperty(v)){ continue; } Log(obj[v],pre+v+" : "); } } else if(typeof(obj)!="function"){ GM_logWarning("Log","obj="+obj+" pre="+pre,"",""); } } }catch(err){ GM_logError("Log","obj="+obj+" pre="+pre,"",err); } } //--------------------------------------------------------------------------------------------------------------------------- function produktPic(type,product,append){ try{ var prodNum = isNaN(parseInt(product,10))?prodId[product]:parseInt(product,10); var type = parseInt(type,10); var newdiv = createElement("div",{"type":type,"prod":prodNum,"style":"display:inline-block;position:relative;margin-right:3px;border:none;vertical-align:bottom;"}); switch(type){ case 0:case 4: if (prodNum>0){ newdiv.setAttribute("class","kp"+prodNum); } else { createElement("img",{"src":GFX+"menu/coins.gif","style":"height:15px;width:15px;border:none;top:0px;vertical-align:bottom;"},newdiv); } break; case 1: newdiv.setAttribute("class","f_m_symbol"+prodNum); break; case 2: newdiv.setAttribute("class","fmm"+prodNum); break; case 3: newdiv.setAttribute("class","fmm"+prodNum); break; default: throw("Unknown type"); } if (append){ append.appendChild(newdiv); } return newdiv; } catch(err){ GM_logError("produktPic","type="+type+" product="+product,"",err); return null; } } function numberFormat(number,decimals,dec_point,thousands_sep){ // http://kevin.vanzonneveld.net // + original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com) // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // + bugfix by: Michael White (http://getsprink.com) // + bugfix by: Benjamin Lupton // + bugfix by: Allan Jensen (http://www.winternet.no) // + revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com) // + bugfix by: Howard Yeend // + revised by: Luke Smith (http://lucassmith.name) // + bugfix by: Diogo Resende // + bugfix by: Rival // % note 1: For 1000.55 result with precision 1 in FF/Opera is 1,000.5, but in IE is 1,000.6 // * example 1: numberFormat(1234.56); // * returns 1: '1,235' // * example 2: numberFormat(1234.56, 2, ',', ' '); // * returns 2: '1 234,56' // * example 3: numberFormat(1234.5678, 2, '.', ''); // * returns 3: '1234.57' // * example 4: numberFormat(67, 2, ',', '.'); // * returns 4: '67,00' // * example 5: numberFormat(1000); // * returns 5: '1,000' // * example 6: numberFormat(67.311, 2); // * returns 6: '67.31' try{ var n = number, prec = decimals; n = !isFinite(+n) ? 0 : +n; prec = !isFinite(+prec) ? 0 : Math.abs(prec); var sep = (typeof thousands_sep == "undefined") ? delimThou : thousands_sep; // changed! var dec = (typeof dec_point == "undefined") ? delimDeci : dec_point; // changed! var s = (prec > 0) ? n.toFixed(prec) : Math.round(n).toFixed(prec); //fix for IE parseFloat(0.55).toFixed(0) = 0; var abs = Math.abs(n).toFixed(prec); var _, i; if (abs >= 1000) { _ = abs.split(/\D/); i = _[0].length % 3 || 3; _[0] = s.slice(0,i + (n < 0)) + _[0].slice(i).replace(/(\d{3})/g, sep+'$1'); s = _.join(dec); } else { s = s.replace('.', dec); } return s; }catch(err){ GM_logError("numberFormat","number="+number,"",err); return ""; } } function numberFormatPrefixed(number){ // results: 1; 21; 321; 4.321; 54k; 654k; 7.654k; 87M try{ if(number>=10000000){ return numberFormat(Math.floor(number/1000000))+"M"; }else if(number>=10000){ return numberFormat(Math.floor(number/1000))+"k"; }else{ return numberFormat(number); } }catch(err){ GM_logError("numberFormatPrefixed","number="+number,"",err); return ""; } } function moneyFormat(number){ try{ return numberFormat(number,2)+" "+getText("gamecurrency"); }catch(err){ GM_logError("moneyFormat","number="+number,"",err); } } function moneyFormatInt(number){ try{ return numberFormat(number,0)+" "+getText("gamecurrency"); }catch(err){ GM_logError("moneyFormatInt","number="+number,"",err); } } function moneyFormatLimit(number,limit){ try{ return (number>limit?moneyFormatInt(number):moneyFormat(number)); }catch(err){ GM_logError("moneyFormatLimit","number="+number+" limit="+limit,"",err); } } function pointsFormat(number,containertype,append){ try{ var newspan = createElement(containertype,{"style":"white-space:nowrap;"},append?append:false); newspan.addEventListener("mouseover",function(event){ toolTip.show(event,getText("points")); },false); // createElement("img",{"src":GFX+"points.gif","style":"border:0px;width:12px;height:12px;margin-right:2px;"},newspan); createElement("img",{"src":"data:image/gif;base64,R0lGODlhHgAeAMQfAP/ilv/Zdv/SWdikGv/VZkQ0CP/EJYhnEXdaDvO5HuqyHf/dgv/npf/MRLeLFpZyEsWWGP/FK//KO/7BH+KsHP/PT//gjqN8FP/HMayDFW1TDf/INWNLDH5gD6dmAAAAACH5BAEAAB8ALAAAAAAeAB4AAAX/4CeO5OgthFeubOtZaSu7b6rOuOgBljDdudluUVEAgyzPa9GgKJEui4VJcRyhJmlAUr1ilQCURAF5Yk2MMEGSoCS8JKV8rmRYAoLNZGKg+wkBCxYADIWGag0YBhESFYALC3gSEwoDHhINAo+DPCgVEhERGA0VAqYNEVUXD3MJE6OaBAQCnxEGixi5BmQPCBxyOh4OEAMJjA0SG7a3EwlkFx0F0mYmSg8ZEAp8tnuUAxkI0gXASR4XDgPazM0DDgfi1DQerswJzt/h5DkeohETFAMCQoA27gsyA1UyZHDg4MIBDXBoCBiToQOCDg8erOoQTwiBChMycBCnAeODAwciTq7wIAADBA3TPBQoiTJlECUNBkQbJ0cDzZQqTTRQ8CBmMJ8XbeKYd+FXvJ4aEAT9oAQCx4hKEEgN6mHAhY4llHRQ6qLMVKoeyJY7G+xKCAA7","style":"border:0px;width:12px;height:12px;margin-right:2px;"},newspan); createElement("span",{},newspan,numberFormat(number)); return newspan; }catch(err){ GM_logError("pointsFormat","number="+number+" containertype="+containertype,"",err); } } function coinsFormat(number,append){ try{ var newdiv = createElement("div",{"style":"display:inline-block;height:16px;"}); newdiv.addEventListener("mouseover",function(event){ toolTip.show(event,prodName[0]); },false); var newdiv1 = produktPic(0,0,newdiv); createElement("span",{},newdiv,numberFormat(number)); if (append) append.appendChild(newdiv); return newdiv; }catch(err){ GM_logError("coinsFormat","number="+number,"",err); } } function getForestryUserBuilding(pos){ try{ if(unsafeWindow.forestry_user_buildings!=undefined&&pos!=undefined){ for(var building in unsafeWindow.forestry_user_buildings){ if(!unsafeWindow.forestry_user_buildings.hasOwnProperty(building)){continue;} if(unsafeWindow.forestry_user_buildings[building]["position"]==pos){ return pos; } } } return false; }catch(err){ GM_logError("coinsFormat","pos="+pos,"",err); } } //--------------------------------------------------------------------------------------------------------------------------- function getRandom(min,max){ try{ if ( min > max ){return( -1 ); } if ( min == max ){return( min );} return( min + parseInt( Math.random() * ( max-min+1 ),10 ) ); }catch(err){ GM_logError("getRandom","min="+min+" max="+max,"",err); } } function compareVersions(version1,version2){ // returns -1 if ver1ver2 try{ if(version1==version2){ return 0; } else { var ver1 = /(\d+)\.*(.*)/.exec(version1); var ver2 = /(\d+)\.*(.*)/.exec(version2); if(parseInt(ver1[1],10)=0;i--){ if (elements[i]<=now){ removeElement(container.children[i]); elements.splice(i,1); } } } clearActive = false; } }catch(err){ GM_logError("logBubble.clear","","",err); } }; this.test=function(){ try{ GM_logInfo("logBubble.test","","",""); logBubble.add("logBubble.test"); }catch(err){ GM_logError("logBubble.test","","",err); } }; }catch(err){ GM_logError("logBubble","","",err); } } top.unsafeData.logBubble=logBubble; } if(top.unsafeData.tracking){ var tracking=top.unsafeData.tracking; }else{ var tracking=new function(){ try{ var data={}; this.init=function(skriptName){ try{ if(!data[skriptName]){ data[skriptName] = []; GM_registerMenuCommand("show tracking of "+skriptName, function(skriptName){ return function(){ tracking.plot(skriptName); } }(skriptName)); } }catch(err){ GM_logError("tracking.init","skriptName="+skriptName,"",err); } }; this.start=function(skriptName,functionName,parameterArray){ try{ return data[skriptName].push([functionName,(new Date()).getTime(),null,parameterArray]); }catch(err){ GM_logError("tracking.start","skriptName="+skriptName+" functionName="+functionName,"",err); } }; this.end=function(skriptName,trackingHandle){ try{ data[skriptName][trackingHandle-1][2]=(new Date()).getTime(); // check for long durations? }catch(err){ GM_logError("tracking.end","skriptName="+skriptName,"",err); } }; this.plot=function(skriptName){ try{ var container=createElement("div",{"style":"z-index:995;position:absolute;top:0;left:0;background-color:white;height:100%;"},ALL); var div=createElement("img",{"class":"link","src":GFX+"close.jpg","style":"position:absolute;top:0;right:0;width:20px;height:20px;margin:5px;"},container); div.addEventListener("click",function(){ removeElement(this.parentNode); },false); div=createElement("div",{"style":"height:100%;padding-right:20px;margin-right:30px;overflow:auto;"},container); var table,tr,td; table=createElement("table",{"border":"1"},div); tr=createElement("tr",{},table); createElement("th",{},table,"Nr"); createElement("th",{},table,"function"); createElement("th",{},table,"start"); createElement("th",{},table,"end"); createElement("th",{},table,"parameter"); for(var i=0;ia[0])-(b[0]