Display hospital timer on the attack page
当前为
// ==UserScript==
// @name torn-attack-hospital-timer
// @namespace typhon.torn.attack-hospital
// @version 1.1
// @description Display hospital timer on the attack page
// @author rantMore [3265877]
// @license GNU GPLv3
// @run-at document-end
// @license MIT
// @grant GM_log
// @match https://www.torn.com/loader.php?sid=attack*
// ==/UserScript==
const apiKey = "YOUR_API_HERE";
const preFire = 2000; // Milliseconds desired for automatic reload
function getTimeLeft(hospital_timestamp) {
let outTime = new Date(0);
outTime.setUTCSeconds(hospital_timestamp);
const seconds = Math.floor((outTime - Date.now()) / 1000);
if (seconds >= 60) {
const secondsFormated = Math.floor(seconds % 60).toString().padStart(2, '0');
return seconds < 3600 ? `${Math.floor(seconds/60)}:${secondsFormated}` : `${Math.floor(seconds / 3600)}h${Math.floor((seconds % 3600) / 60).toString().padStart(2, '0')}`;
} else {
return `${seconds}s`;
}
}
(function hospital_time() {
'use strict';
const initialTitle = document.title;
const userid = location.href.replace(/.*?user2ID=(\d+)/i, "$1");
fetch(`https://api.torn.com/user/${userid}?selections=profile,personalstats&key=${apiKey}&comment=attack_stats`).then( async response => {
let user = (await response.json())||{};
if (user.status.state.toLowerCase() === 'hospital') {
let alertWrapper = document.createElement("div")
alertWrapper.setAttribute("class", "userName___loAWK bold");
let outTime = new Date(0); // The 0 there is the key, which sets the date to the epoch
outTime.setUTCSeconds(user.states.hospital_timestamp);
alertWrapper.innerHTML = `<div> </div><div>Coming out at ${outTime.toLocaleTimeString('en-US')}<div><div>In <span class="title___fOh2J">${getTimeLeft(user.states.hospital_timestamp)}</span>`;
document.title = `${user.name} | ${getTimeLeft(user.states.hospital_timestamp)}`;
let outerBox = document.querySelector('.dialogButtons___nX4Bz');
outerBox.appendChild(alertWrapper);
let timerPtr = setInterval( () => {
const millis = outTime - Date.now();
if (millis <= preFire) {
// Stop everything, we are done.
document.title = initialTitle;
clearInterval(timerPtr);
location.reload();
} else {
alertWrapper.innerHTML = `<div> </div><div>Coming out at ${outTime.toLocaleTimeString('en-US')}<div><div>In <span class="title___fOh2J">${getTimeLeft(user.states.hospital_timestamp)}</span>`;
document.title = `${user.name} | ${getTimeLeft(user.states.hospital_timestamp)}`;
}
}, 1000)
}
});
})();