Bauws / Alarm Script

// ==UserScript==
// @name         Alarm Script
// @namespace    http://tampermonkey.net/
// @version      2.0
// @description  Alarm script for OGame whenever hostile missions are incoming 
// @author       Bauws
// @match        https://*.gameforge.com/*
// @exclude      https://*.gameforge.com/game/index.php?page=chat
// @exclude      https://*.gameforge.com/game/index.php?page=ingame&component=galaxy*
// @exclude      https://*.gameforge.com/game/index.php?page=ingame&component=fleetdispatch*
// @exclude      https://*.gameforge.com/game/index.php?page=messages*
// @exclude      https://*.gameforge.com/game/index.php?page=highscore*
// @exclude      https://*.ogame.gameforge.com/game/index.php?page=ingame&component=traderOverview*
// @exclude      https://*.gameforge.com/game/index.php?page=standalone&component=combatsim
// @updateURL    https://openuserjs.org/meta/Bauws/Alarm_Script.meta.js
// @downloadURL  https://openuserjs.org/install/Bauws/Alarm_Script.user.js
// @copyright    2024, Bauws (https://openuserjs.org/users/Bauws)
// @icon         https://www.google.com/s2/favicons?sz=64&domain=gameforge.com
// @license      MIT
// @grant        none
// ==/UserScript==

var token = "YOUR_BOT_TOKEN"; //Telegram Bot Token
var chatId = "YOUR_CHAT_ID"; // Telegram Chat ID

(async function() {
    'use strict';

    var url = window.location.href;
    url = url.split("/game")[0];

    var audio = new Audio("https://assets.mixkit.co/active_storage/sfx/999/999-preview.mp3");

    fetch(url + "/game/index.php?page=componentOnly&component=eventList&action=fetchEventBox&ajax=1&asJson=1", {
        "headers": {
            "accept": "text/plain, */*; q=0.01",
            "x-requested-with": "XMLHttpRequest"
        },
        "referrer": url + "/game/index.php?page=ingame&component=overview",
        "referrerPolicy": "strict-origin-when-cross-origin",
        "body": null,
        "method": "GET",
        "mode": "cors",
        "credentials": "include"
    }).then((res) => res.json()).then((json) => {
        if (json.hostile != 0) {
            audio.play();

            var message = "Angriff! -> " + url;

            $.ajax({
                type: 'POST',
                url: `https://api.telegram.org/bot${token}/sendMessage`,
                data: {
                    chat_id: chatId,
                    text: message,
                    parse_mode: 'html',
                },
                success: function (res) {
                    console.debug(res);
                    $('#response').text('Message sent');
                },
                error: function (error) {
                    console.error(error);
                    alert("error failed");
                }
            });
        }
    });

    var sleeptimer = Math.floor(Math.random() * (300000 - 60000 + 1)) + 180000;

    var refresh_time = new Date(Date.now() + sleeptimer).toTimeString().split("G")[0];

    var sleeptimer_text = "Nächste Aktualisierung in ca. " + Math.floor(sleeptimer / 1000 / 60) + " Minuten " + Math.floor(sleeptimer / 1000 % 60) + " Sekunden um " + refresh_time;

    console.log(sleeptimer_text);

    var label = document.createElement("label");
    label.innerHTML = sleeptimer_text;
    label.style.position = "fixed";
    label.style.bottom = "20px";
    label.style.right = "10px";
    label.style.padding = "5px";
    label.style.border = "1px solid black"; // Optional: Rahmen für bessere Sichtbarkeit


    document.querySelector("#ingamepage").append(label);

    await sleep(sleeptimer);

    location.reload();

    function sleep(milliseconds) {
        return new Promise(resolve => setTimeout(resolve, milliseconds));
    }

})();