background.js aktualisiert

This commit is contained in:
2025-12-06 15:13:40 +00:00
parent f78de6f2ff
commit 43699a210f

View File

@@ -48,7 +48,7 @@ async function cleanupHistory() {
await chrome.storage.local.set({ history });
}
// NEU: Funktion zum Senden einer Discord-Benachrichtigung
// Funktion zum Senden einer Discord-Benachrichtigung
async function sendDiscordNotification(service, status, responseTime = null) {
const settings = await chrome.storage.sync.get({ discordWebhookUrl: '' });
if (!settings.discordWebhookUrl) return; // Kein Webhook konfiguriert
@@ -89,6 +89,42 @@ async function sendDiscordNotification(service, status, responseTime = null) {
}
}
// NEU: Funktion zum Senden einer Telegram-Benachrichtigung
async function sendTelegramNotification(service, status, responseTime = null) {
const settings = await chrome.storage.sync.get({ telegramBotToken: '', telegramChatId: '' });
if (!settings.telegramBotToken || !settings.telegramChatId) return; // Nicht konfiguriert
const isOnline = status === 'online';
const emoji = isOnline ? '✅' : '❌';
let message = `${emoji} *${service.name}*\nStatus: ${status.toUpperCase()}\nAdresse: ${service.adresse}`;
if (isOnline && responseTime !== null) {
message += `\nAntwortzeit: ${responseTime} ms`;
}
const url = `https://api.telegram.org/bot${settings.telegramBotToken}/sendMessage`;
const payload = {
chat_id: settings.telegramChatId,
text: message,
parse_mode: 'Markdown' // Für Markdown-Formatierung
};
try {
const response = await fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload)
});
if (!response.ok) {
const errorData = await response.json();
console.error('Telegram-Bot API fehlgeschlagen:', response.statusText, errorData);
}
} catch (error) {
console.error('Fehler beim Senden der Telegram-Benachrichtigung:', error);
}
}
// MODIFIZIERT: Funktion, um alle Dienste zu prüfen und Benachrichtigungen zu senden
async function checkAllServices() {
const data = await chrome.storage.sync.get({ services: [], notifyOnline: false });
@@ -108,25 +144,31 @@ async function checkAllServices() {
// Benachrichtigung für Offline-Wechsel
if (previousResult?.status === 'online' && currentResult.status === 'offline') {
chrome.notifications.create({
type: 'basic', iconUrl: 'icons/notification_warning.png',
type: 'basic',
iconUrl: 'icons/notification_warning.png',
title: `Server "${service.name}" ist offline`,
message: 'Der Dienst antwortet nicht mehr.', contextMessage: `Adresse: ${service.adresse}`,
priority: 1, isClickable: true,
buttons: [{ title: 'Anzeigen' }, { title: 'Ignorieren' }]
message: 'Der Dienst antwortet nicht mehr.',
contextMessage: `Adresse: ${service.adresse}`,
priority: 1,
isClickable: true
});
await sendDiscordNotification(service, 'offline'); // NEU
await sendDiscordNotification(service, 'offline');
await sendTelegramNotification(service, 'offline'); // NEU
}
// Benachrichtigung für Online-Wechsel (wenn aktiviert)
if (notifyOnline && previousResult?.status === 'offline' && currentResult.status === 'online') {
chrome.notifications.create({
type: 'basic', iconUrl: 'icons/notification_warning.png',
type: 'basic',
iconUrl: 'icons/notification_warning.png',
title: `Server "${service.name}" ist wieder online!`,
message: 'Der Dienst ist wieder erreichbar.', contextMessage: `Adresse: ${service.adresse}`,
priority: 0, isClickable: true,
buttons: [{ title: 'Anzeigen' }]
message: 'Der Dienst ist wieder erreichbar.',
contextMessage: `Adresse: ${service.adresse}`,
priority: 0,
isClickable: true
});
await sendDiscordNotification(service, 'online', currentResult.responseTime); // NEU
await sendDiscordNotification(service, 'online', currentResult.responseTime);
await sendTelegramNotification(service, 'online', currentResult.responseTime);
}
// Verlaufsdaten aktualisieren
@@ -155,10 +197,6 @@ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
});
// Benachrichtigungs-Listener
chrome.notifications.onButtonClicked.addListener((notificationId, buttonIndex) => {
if (buttonIndex === 0) chrome.action.openPopup();
chrome.notifications.clear(notificationId);
});
chrome.notifications.onClicked.addListener((notificationId) => {
chrome.action.openPopup();
chrome.notifications.clear(notificationId);