Created
July 26, 2025 18:28
-
-
Save maluramichael/f571d874d7ac31f2315599d27ea066fd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name OGame Event Timer in Titlebar | |
// @namespace http://tampermonkey.net/ | |
// @version 1.0 | |
// @description Zeigt die verbleibende Zeit des aktuellsten Events in der Browser-Titlebar an | |
// @author Michael Malura <[email protected]> | |
// @match *://*.ogame.gameforge.com/* | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
// Ursprünglicher Titel für die Wiederherstellung | |
let originalTitle = document.title; | |
let updateInterval; | |
// Funktion zum Parsen der Zeitangaben (z.B. "1m 16s", "16m 21s", "1h 18m 24s") | |
function parseTimeString(timeStr) { | |
let totalSeconds = 0; | |
// Stunden extrahieren | |
const hoursMatch = timeStr.match(/(\d+)h/); | |
if (hoursMatch) { | |
totalSeconds += parseInt(hoursMatch[1]) * 3600; | |
} | |
// Minuten extrahieren | |
const minutesMatch = timeStr.match(/(\d+)m/); | |
if (minutesMatch) { | |
totalSeconds += parseInt(minutesMatch[1]) * 60; | |
} | |
// Sekunden extrahieren | |
const secondsMatch = timeStr.match(/(\d+)s/); | |
if (secondsMatch) { | |
totalSeconds += parseInt(secondsMatch[1]); | |
} | |
return totalSeconds; | |
} | |
// Funktion zum Formatieren der Zeit für die Titlebar | |
function formatTimeForTitle(seconds) { | |
if (seconds <= 0) return "Event beendet"; | |
const hours = Math.floor(seconds / 3600); | |
const minutes = Math.floor((seconds % 3600) / 60); | |
const secs = seconds % 60; | |
if (hours > 0) { | |
return `${hours}h ${minutes}m ${secs}s`; | |
} else if (minutes > 0) { | |
return `${minutes}m ${secs}s`; | |
} else { | |
return `${secs}s`; | |
} | |
} | |
// Funktion zum Finden des aktuellsten Events | |
function findEarliestEvent() { | |
const eventRows = document.querySelectorAll('#eventContent .eventFleet'); | |
let earliestEvent = null; | |
let earliestTime = Infinity; | |
eventRows.forEach(row => { | |
const countdownElement = row.querySelector('.countDown span'); | |
if (countdownElement) { | |
const timeText = countdownElement.textContent.trim(); | |
const seconds = parseTimeString(timeText); | |
if (seconds < earliestTime) { | |
earliestTime = seconds; | |
earliestEvent = { | |
element: countdownElement, | |
timeText: timeText, | |
seconds: seconds | |
}; | |
} | |
} | |
}); | |
return earliestEvent; | |
} | |
// Funktion zum Aktualisieren der Titlebar | |
function updateTitlebar() { | |
const earliestEvent = findEarliestEvent(); | |
if (earliestEvent && earliestEvent.seconds > 0) { | |
const formattedTime = formatTimeForTitle(earliestEvent.seconds); | |
document.title = `[${formattedTime}] ${originalTitle}`; | |
} else { | |
// Keine aktiven Events gefunden, ursprünglichen Titel wiederherstellen | |
document.title = originalTitle; | |
} | |
} | |
// Funktion zum Starten des Updates | |
function startTitlebarUpdate() { | |
// Sofort erste Aktualisierung | |
updateTitlebar(); | |
// Dann alle 1 Sekunden aktualisieren | |
updateInterval = setInterval(updateTitlebar, 1000); | |
} | |
// Funktion zum Stoppen des Updates | |
function stopTitlebarUpdate() { | |
if (updateInterval) { | |
clearInterval(updateInterval); | |
updateInterval = null; | |
} | |
// Ursprünglichen Titel wiederherstellen | |
document.title = originalTitle; | |
} | |
// Event-Listener für Seitenwechsel | |
function handlePageChange() { | |
// Kurz stoppen und dann neu starten | |
stopTitlebarUpdate(); | |
setTimeout(startTitlebarUpdate, 1000); | |
} | |
// MutationObserver für dynamische Inhaltsänderungen | |
function setupMutationObserver() { | |
const observer = new MutationObserver(function(mutations) { | |
mutations.forEach(function(mutation) { | |
if (mutation.type === 'childList') { | |
// Prüfen ob #eventContent geändert wurde | |
const eventContent = document.getElementById('eventContent'); | |
if (eventContent && mutation.target.contains(eventContent)) { | |
// Kurze Verzögerung für die Aktualisierung | |
setTimeout(updateTitlebar, 500); | |
} | |
} | |
}); | |
}); | |
// Beobachte das gesamte Dokument | |
observer.observe(document.body, { | |
childList: true, | |
subtree: true | |
}); | |
} | |
// Initialisierung | |
function init() { | |
// Warten bis DOM geladen ist | |
if (document.readyState === 'loading') { | |
document.addEventListener('DOMContentLoaded', function() { | |
setTimeout(init, 1000); | |
}); | |
return; | |
} | |
// Prüfen ob wir auf einer OGame-Seite mit Events sind | |
const eventContent = document.getElementById('eventContent'); | |
if (eventContent) { | |
console.log('OGame Event Timer: Script gestartet'); | |
startTitlebarUpdate(); | |
setupMutationObserver(); | |
// Event-Listener für Seitenwechsel (AJAX-Navigation) | |
window.addEventListener('beforeunload', stopTitlebarUpdate); | |
// Beobachte URL-Änderungen für SPA-Navigation | |
let currentUrl = window.location.href; | |
setInterval(() => { | |
if (window.location.href !== currentUrl) { | |
currentUrl = window.location.href; | |
handlePageChange(); | |
} | |
}, 1000); | |
} | |
} | |
// Script starten | |
init(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment