Show War Local Time

Shows the war end time in local time.

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Show War Local Time
// @namespace    https://www.torn.com/profiles.php?XID=1936821
// @version      1.6
// @description  Shows the war end time in local time.
// @author       TheFoxMan
// @owner        Phillip_J_Fry [2184575]
// @license      Apache License 2.0
// @match        https://www.torn.com/factions.php*
// @run-at       document-end
// ==/UserScript==

// Made for Phillip_J_Fry [2184575].
// DO NOT EDIT.

if (!Document.prototype.find)
	Object.defineProperties(Document.prototype, {
		find: {
			value(selector) {
				return document.querySelector(selector);
			},
			enumerable: false
		},
		findAll: {
			value(selector) {
				return document.querySelectorAll(selector);
			},
			enumerable: false
		}
	});

if (!Element.prototype.find)
	Object.defineProperties(Element.prototype, {
		find: {
			value(selector) {
				return this.querySelector(selector);
			},
			enumerable: false
		},
		findAll: {
			value(selector) {
				return this.querySelectorAll(selector);
			},
			enumerable: false
		}
	});

async function waitFor(sel, parent = document) {
	return new Promise((resolve) => {
		const intervalID = setInterval(() => {
			const el = parent.find(sel);
			if (el) {
				resolve(el);
				clearInterval(intervalID);
			}
		}, 500);
	});
}

(async () => {
	showWarTimes();

	window.addEventListener("hashchange", showWarTimes);
})();

async function showWarTimes() {
	if (window.location.hash.includes("tab=")) return;

	const warList = await waitFor("#faction_war_list_id");

	if (window.location.hash.includes("tab=")) return;

	document.findAll(".war-end-time").forEach((x) => x.remove());

	warList.findAll("[class*='warListItem__']").forEach((war) => {
		if (war.find(".timer")) {
			// Territory War
			const timer = war.find(".timer");
			const timeLeft = parseTime(timer.textContent);
			let date = Date.now();
			// date -= timeLeft;
			date += timeLeft;
			// date += 3 * 24 * 60 * 60 * 1000;
			date = (new Date(date)).toLocaleString()
			timer.insertAdjacentHTML("afterend", "<div class='war-end-time'>" + date + "</div>");
			return;
		}

		// RW
		if (!war.find("[data-warid]")) return;

		const bottomDiv = war.find("[class*='bottomBox__']");
		const timer = bottomDiv.find("[class*='timer__']");
		if (bottomDiv.textContent.includes("WINNER")) return;

		if (!parseTime(bottomDiv.textContent)) return;

		// console.log(Date.now() - parseTime(timer.textContent));
		const date = (new Date(Date.now() - parseTime(timer.textContent) + 123 * 60 * 60 * 1000)).toLocaleString();
		bottomDiv.insertAdjacentHTML(
			"beforeend",
			"<div class='war-end-time'>" + date + "</div>"
		);
	});
}

function parseTime(str) {
	const splits = str.split(":").map((x) => parseInt(x));
	// console.log(splits);
	let time = 0;
	time += splits[0] * 1000 * 60 * 60 * 24;
	time += splits[1] * 1000 * 60 * 60;
	time += splits[2] * 1000 * 60;
	time += splits[3] * 1000;
	return time;
}