Insert dungeon run time

Insert dungeon run time after the key count

Versión del día 6/12/2025. Echa un vistazo a la versión más reciente.

Tendrás que instalar una extensión para tu navegador como Tampermonkey, Greasemonkey o Violentmonkey si quieres utilizar este script.

You will need to install an extension such as Tampermonkey to install this script.

Necesitarás instalar una extensión como Tampermonkey o Violentmonkey para instalar este script.

Necesitarás instalar una extensión como Tampermonkey o Userscripts para instalar este script.

Necesitará instalar una extensión como Tampermonkey para instalar este script.

Necesitarás instalar una extensión para administrar scripts de usuario si quieres instalar este script.

(Ya tengo un administrador de scripts de usuario, déjame instalarlo)

Necesitará instalar una extensión como Stylus para instalar este estilo.

Necesitará instalar una extensión como Stylus para instalar este estilo.

Necesitará instalar una extensión como Stylus para instalar este estilo.

Necesitará instalar una extensión del gestor de estilos de usuario para instalar este estilo.

Necesitará instalar una extensión del gestor de estilos de usuario para instalar este estilo.

Necesitará instalar una extensión del gestor de estilos de usuario para instalar este estilo.

(Ya tengo un administrador de estilos de usuario, déjame instalarlo)

// ==UserScript==
// @name         Insert dungeon run time
// @namespace    http://tampermonkey.net/
// @version      2025-12-06
// @description  Insert dungeon run time after the key count
// @license      MIT
// @author       sentientmilk
// @match        https://www.milkywayidle.com/*
// @icon         https://www.milkywayidle.com/favicon.svg
// @grant        unsafeWindow
// @run-at       document-start
// ==/UserScript==

/*
	Changelog
	=========

	v2025-11-26
		- Initial version
	v2025-11-26-1
		- FIXED: Did not add time after the run
	v2025-11-26-2
		- FIXED: Selecting messages from other chats
	v2025-11-27
		- FIXED: 29m 60s
	v2025-11-27-2
		- Added rolling average times of the collected runs
	v2025-12-06
		- FIXED: Reconnecting after unfocus, tab switching, desktop switching

	        TODO
	====================
	- Incompatability with MWITools on Chromium with Violentmonkey(specifically)
*/

/*
	Test by running in the console:

		partyTabI = Array.from(document.querySelectorAll(`.Chat_tabsComponentContainer__3ZoKe .MuiButtonBase-root`)).findIndex((el) => el.textContent.includes("Party"));
		d = (new Date()).toISOString();
		handle({ type: "chat_message_received", message: { isSystemMessage: true, "chan": "/chat_channel_types/party", m: "systemChatMessage.partyKeyCount", t: d }});
		document.querySelector(`.TabPanel_tabPanel__tXMJF:nth-child(${partyTabI + 1}) .ChatHistory_chatHistory__1EiG3`).insertAdjacentHTML("beforeend", `<div class="ChatMessage_chatMessage__2wev4 ChatMessage_systemMessage__3Jz9e"><span class="ChatMessage_timestamp__1iRZO">[${d}] </span><span>Key counts: test </div>`);
*/
(function() {
	async function waitFnRepeatedFor (cond, callback) {
		let notified = false;
		return new Promise((resolve) => {
			function check () {
				const r = cond();
				setTimeout(check, 1000/30); // Schedule first to allow the callback to throw
				if (r && !notified) {
					notified = true;
					resolve();
					if (callback) {
						callback();
					}
				} else if (r && notified) {
					// Skip, wait for cond to be false again
				} else {
					notified = false;
				}
			}
			check();
		});
	}

	let keyMessages = [];

	function isOfInterest (message) {
		/*
			Example messages of interest:
				{
					"id": 46049453,
					"chan": "/chat_channel_types/party",
					"t": "2025-11-25T21:27:28.962922208Z",
					"cId": 0,
					"isSystemMessage": true,
					"m": "systemChatMessage.partyBattleStarted",
					"systemMetadata": "{\"actionHrid\":\"/actions/combat/enchanted_fortress\"}"
				}

				{
					"id": 46049454,
					"chan": "/chat_channel_types/party",
					"t": "2025-11-25T21:27:28.971510704Z",
					"cId": 0,
					"isSystemMessage": true,
					"m": "systemChatMessage.partyKeyCount",
					"systemMetadata": "{\"keyCountString\":\"[OmegaShitsLurpa - 167] [sentientmilk - 216] [Kiyuwu - 72] [Metzli - 198] [Tank - 274] \"}"
				}
		*/
		return message.chan == "/chat_channel_types/party"
			&& message.isSystemMessage == true
			&& (message.m == "systemChatMessage.partyBattleStarted" || message.m == "systemChatMessage.partyKeyCount");
	}

	function f (t) {
		return Math.floor(t / 1000 / 60) + "m " + Math.floor(t / 1000 % 60) + "s";
	}

	function addDungeonRunTimes () {
		if (!isPartySelected()) {
			return;
		}

		const partyTabI = Array.from(document.querySelectorAll(`.Chat_tabsComponentContainer__3ZoKe .MuiButtonBase-root`)).findIndex((el) => el.textContent.includes("Party"));

		let times = keyMessages.map((m, i, ms) => {
			if (i == 0) {
				return "first";
			}
			const p = ms[i-1];
			if (m.m == "systemChatMessage.partyBattleStarted") {
				return "start";
			}
			if (p.m == "systemChatMessage.partyBattleStarted") {
				return "start-keys";
			}
			if (m.m == "systemChatMessage.partyKeyCount" && p.m == "systemChatMessage.partyKeyCount") {
				return { t: (+new Date(m.t)) - (+new Date(p.t)) };
			}
		});
		console.log(times);
		window.times = times;

		times.forEach((d, i, arr) => {
			const ts = arr.slice(0, i + 1).filter((d2) => d2.t);
			d.average = ts.reduce((acc, d3) => acc + d3.t, 0) / ts.length;
		});

		times.reverse();

		const systemMessagesEls = Array.from(document.querySelectorAll(`.TabPanel_tabPanel__tXMJF:nth-child(${partyTabI + 1}) .ChatHistory_chatHistory__1EiG3 > .ChatMessage_systemMessage__3Jz9e`))
			.filter((el) => el.textContent.includes("Key counts:") || el.textContent.includes("Battle started:"));

		systemMessagesEls.reverse().forEach((el, i) => {
			const d = times[i];
			if (!d.t) {
				return;
			}

			if (el.querySelector(".userscript-idrt")) {
				return;
			}

			el.insertAdjacentHTML("beforeend", `<span class="userscript-idrt">
				<span style="color: orange">${f(d.t)}</span>
				<span style="color: tan"> Average:</span>
				<span style="color: orange">${f(d.average)}</span>
			</span>`.replace(/[\t\n]+/g, " "));
		});
	}

	function handle (message) {
		if (message.type == "init_character_data") {
			message.partyChatHistory.forEach((message2) => {
				if (isOfInterest(message2)) {
					keyMessages.push(message2);
				}
			});
		}

		if (message.type == "chat_message_received" && isOfInterest(message.message)) {
			keyMessages.push(message.message);
			setTimeout(addDungeonRunTimes, 100);
		}
	}

	/*
		Wrap WebSocket to set own listener
		Use unsafeWindow + run-at document-start to do that before MWI calls the WebSocket constuctor
	*/
	const OriginalWebSocket = unsafeWindow.WebSocket;
	let ws;
	function listener (e) {
		const message = JSON.parse(e.data);
		handle(message);
	}
	const WrappedWebSocket = function (...args) {
		ws = new OriginalWebSocket(...args)
		ws.addEventListener("message", listener);
		return ws;
	};

	// Used in .performConnectionHealthCheck() and .sendHealthCheckPing() in the MWI
	WrappedWebSocket.CONNECTING = OriginalWebSocket.CONNECTING;
	WrappedWebSocket.OPEN = OriginalWebSocket.OPEN;
	WrappedWebSocket.CLOSED = OriginalWebSocket.CLOSED;

	unsafeWindow.WebSocket = WrappedWebSocket;

	function isPartySelected () {
		const selectedTabEl = document.querySelector(`.Chat_tabsComponentContainer__3ZoKe .MuiButtonBase-root[aria-selected="true"]`)
		return selectedTabEl && selectedTabEl.textContent.includes("Party");
	}

	waitFnRepeatedFor(isPartySelected, addDungeonRunTimes);



})();