// ==UserScript==
// @name FastRecall
// @namespace FastRecallNamespace
// @author [TSN]Kanly
// @include *.ogame.gameforge.com/game/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// @description FastRecall for Ogame: Allows you to recall fleets from the events list drop down bar
// @version 1.1
// @downloadURL none
// ==/UserScript==
var scriptFunc = (function() {
var Recall = {
getMovement: true,
pendingMovement: false,
availableMovements: [],
currentPage: "",
universeFull: "",
init: function() {
var observer = new MutationObserver(Recall.initAppendRecall);
var target = document.getElementById('eventboxContent');
Recall.universeFull = document.getElementsByName("ogame-universe")[0].content;
Recall.currentPage = (window.location+"").match(/page=[^&]+((?=&)|(?=#)|)/g)[0].replace("page=", "");
/* Disable FastRecall on fleet1/2/3 and movement pages */
if (Recall.currentPage !== "movement" &&
Recall.currentPage !== "fleet1" &&
Recall.currentPage !== "fleet2" &&
Recall.currentPage !== "fleet3") {
observer.observe(target, {childList: true});
/* If events are not hidden (Options->Display->Always show events */
if ($('table#eventContent').exists()) {
Recall.initAppendRecall();
}
}
},
initAppendRecall: function(mutations) {
if ($('table#eventContent').exists()) {
/* Get the "fleet movement" page information only once - when the fleets eventbox is showen for the first time */
if (Recall.getMovement === true && Recall.pendingMovement === false) {
Recall.pendingMovement = true;
Recall.getFleetMovement();
}
else if (Recall.getMovement === false) {
Recall.doAppendRecall();
}
}
},
doAppendRecall: function() {
if ($('table#eventContent').exists()) {
Recall.appendEventlistReload();
$('table#eventContent tr[data-return-flight="false"]').each(function() {
var row = $(this);
var isFriendly = row.find('.countDown.friendly').exists();
if (isFriendly === true) {
var info = {};
info.arrivalTime = row.attr('data-arrival-time');
info.originPosStr = row.find('.coordsOrigin').text().trim();
info.destinationPosStr = row.find('.destCoords').text().trim();
var returnId = Recall.matchEventWithMovement(info);
if (returnId !== "")
row.append('
| ');
}
});
$('.oaf_recall_icon').click(function(){
var recallId = $(this).attr('oaf_recall_id');
var url = 'http://'+Recall.universeFull+'/game/index.php?page=movement&return='+recallId;
var host = Recall.universeFull;
var ref = 'http://'+Recall.universeFull+'/game/index.php?page=movement';
Recall.sendRequest({
method: 'GET',
url: url,
host: host,
acc: '*/*',
ref: ref,
onloadFun: function(){$('#eventHeader .icon_reload').click();}
});
});
}
},
appendEventlistReload: function() {
if (!window.AGO && $('#eventHeader .icon_reload.recall_eventlist_reload').exists() === false) {
//alert(!unsafeWindow.AGO);
$('#eventHeader').prepend('').click(function(){
$.get("/game/index.php?page=eventList&ajax=1", function (a) { $("#eventboxContent").html(a);});
});
}
},
getFleetMovement: function() {
/* Get the "fleet movement" page information; should be called only once */
var url = 'http://'+Recall.universeFull+'/game/index.php?page=movement';
var host = Recall.universeFull;
var ref = 'http://'+Recall.universeFull+'/game/index.php?page='+Recall.currentPage;
Recall.sendRequest({
method: 'GET',
url: url,
host: host,
acc: '*/*',
ref: ref,
onloadFun: Recall.parseFleetMovement
});
},
parseFleetMovement: function(html) {
/* get own fleet flights except returning ones */
var page = $(html);
page.find('.fleetDetails[data-return-flight="false"]').each(function() {
var item = $(this);
var info = {};
info.arrivalTime = item.attr('data-arrival-time');
info.originPosStr = item.find('.originCoords').text();
info.destinationPosStr = item.find('.destinationCoords').text();
info.returnId = item.attr('id').match(/\d+/g)[0];
Recall.availableMovements.push(info);
});
Recall.pendingMovement = false;
Recall.getMovement = false;
Recall.doAppendRecall();
},
matchEventWithMovement: function(evInfo) {
var retVal = "";
for (var m in Recall.availableMovements) {
if (Recall.availableMovements.hasOwnProperty(m) === false)
continue;
var mvInfo = Recall.availableMovements[m];
if (mvInfo.arrivalTime === evInfo.arrivalTime &&
mvInfo.originPosStr === evInfo.originPosStr &&
mvInfo.destinationCoords === evInfo.destinationCoords) {
retVal = mvInfo.returnId;
}
}
return retVal;
},
sendRequest: function(opts) {
var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function() {
if (httpRequest.readyState == 0 || httpRequest.readyState == 4) {
if (IsRequestSuccessful(httpRequest)) {
if (opts.onloadFun)
opts.onloadFun(httpRequest.responseText);
}
else {
if (opts.onerrorFun)
opts.onerrorFun(httpRequest.responseText);
}
}
}
httpRequest.open(opts.method, opts.url, true);
httpRequest.setRequestHeader('Accept-Language', 'en-us,en;q=0.5');
httpRequest.setRequestHeader('Accept', (opts.acc && opts.acc !== '') ? opts.acc : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8');
if (opts.contType)
httpRequest.setRequestHeader('Content-Type', opts.contType);
if (!opts.payload)
opts.payload = null;
httpRequest.send(opts.payload);
function IsRequestSuccessful (httpRequest) {
var success = (httpRequest.status == 0 ||
(httpRequest.status >= 200 && httpRequest.status < 300) ||
httpRequest.status == 304 || httpRequest.status == 1223);
return success;
}
}
};
$.fn.exists = function () {
return this.length !== 0;
};
Recall.init();
}).toString();
function injectScript (myFunc) {
var script = document.createElement ("script");
script.setAttribute ("type", "application/javascript");
script.textContent = "(" + myFunc + ") ();";
document.body.appendChild (script);
}
function injectCss() {
var recallImage = "data:image/gif;base64,R0lGODlhEAAQAKIAAISbrAAAAP///2+JnVx2iwAAAAAAAAAAACH5BAAAAAAALAAAAAAQABAAAAMqGLTcrS7KSautIut9mejLtwwkKZTliQ5Atg4qus1x2dbviud875eBHzABADs=";
var recallCss = '.oaf_recall_icon {background-image: url('+recallImage+'); background-repeat: no-repeat !important;} #eventHeader .recall_eventlist_reload {position: absolute; left: 14px; margin: 4px 0;}';
var eventlistReloadCss = '#eventHeader .recall_eventlist_reload {position: absolute; left: 14px; margin: 4px 0;}';
var style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = recallCss;
document.getElementsByTagName("head")[0].appendChild(style);
}
if (document.body) {
injectScript(scriptFunc);
injectCss();
}