Free javascript Hosting


timeback.js

Uploaded on Dec 07 2021 02:52 by nico_elmer

const container = document.getElementById("commands_outgoings");
const elements = container.getElementsByClassName("command-row");
const getAttackersArrival = prompt("Arrival time", "23:01:56:313");
const getDistance = prompt("Input walking time","1:09:34");
const now = new Date(Date.now());

init();

function init() {
var tbody = container.getElementsByTagName("tbody")[0];
var trs = tbody.getElementsByTagName("tr");
for (var i = 0; i < trs.length; i++) {
var tr = trs[i];
var el = i === 0 ?
tr.getElementsByTagName("th") : tr.getElementsByTagName("td");
var newNode = i === 0 ?
document.createElement("th") : document.createElement("td");
var text = i === 0 ?
document.createTextNode("Autoreturn in") : document.createTextNode(createTimer(tr));
var childNode = el[2];

if (text && newNode && childNode) {
newNode.appendChild(text);
tr.insertBefore(newNode, childNode);
}
}
}

function createTimer(element) {
var attackTimeArr = getAttackersArrival.split(":");
var attackersArrival = correctTimeOffset(new Date(now.getFullYear(), now.getMonth(), now.getDate(), attackTimeArr[0], attackTimeArr[1], attackTimeArr[2]).setMilliseconds(attackTimeArr[3]));

var msUntilAttack = attackersArrival - correctTimeOffset(now);

// for each different
var msDeffAway = correctTimeOffset(now) - getSendOutBOTime(element);

var sendBackDeff = (msUntilAttack - msDeffAway) / 2;

// Backoff Deff
setTimeout(() => {
element.getElementsByClassName("command-cancel")[0].click();
}, sendBackDeff);

console.log("==========================================================");
console.log("Sendout army: ", getSendOutBOTime(element));
console.log("Attack at: ", attackersArrival);
console.log("Now: ", correctTimeOffset(now));
console.log("----------------------------------------------------------");
console.log("ms Until Attack: ", msUntilAttack, getTimeFormated(msUntilAttack));
console.log("Diff Deff away and upcoming Attack: ", sendBackDeff, getTimeFormated(sendBackDeff));
console.log("ms Deff away: ", msDeffAway, getTimeFormated(msDeffAway));
console.log("----------------------------------------------------------");
console.log("Time until backoff: ", sendBackDeff, getTimeFormated(sendBackDeff));
console.log("==========================================================");

return getTimeFormated(sendBackDeff);
}

function getAnkunft(element) {
var ankunft = element.getElementsByTagName("td")[1].innerHTML;

var regex = /\d+/g;
var matches = ankunft.match(regex);

var ankunftTime = correctTimeOffset(new Date(now.getFullYear(), now.getMonth(), now.getDate(), matches[0], matches[1], matches[2], matches[3]));
return ankunftTime;
}

function correctTimeOffset(date) {
var timeZoneOffset = now.getTimezoneOffset() * 60 * 1000;
return new Date(date - timeZoneOffset);
}

function getSendOutBOTime(element) {
var ankunft = getAnkunft(element);
var distArr = getDistance.split(":");
var hours = distArr[0] * 3600 * 1000;
var minutes = distArr[1] * 60 * 1000;
var seconds = distArr[2] * 1000;

return new Date(ankunft - (hours + minutes + seconds));
}

function getTimeFormated(time) {
var hours = time / 60 / 60 / 1000;
var minutes = (time - (Math.floor(hours) * 3600 * 1000)) / 60 / 1000;
var seconds = (time - (Math.floor(hours) * 3600 * 1000) - (Math.floor(minutes) * 60 * 1000)) / 1000;

return `(${Math.floor(hours)}:${Math.floor(minutes)}:${Math.floor(seconds)})`;
}

Back to list