background.js aktualisiert
This commit is contained in:
@@ -48,7 +48,7 @@ async function cleanupHistory() {
|
|||||||
await chrome.storage.local.set({ history });
|
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) {
|
async function sendDiscordNotification(service, status, responseTime = null) {
|
||||||
const settings = await chrome.storage.sync.get({ discordWebhookUrl: '' });
|
const settings = await chrome.storage.sync.get({ discordWebhookUrl: '' });
|
||||||
if (!settings.discordWebhookUrl) return; // Kein Webhook konfiguriert
|
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
|
// MODIFIZIERT: Funktion, um alle Dienste zu prüfen und Benachrichtigungen zu senden
|
||||||
async function checkAllServices() {
|
async function checkAllServices() {
|
||||||
const data = await chrome.storage.sync.get({ services: [], notifyOnline: false });
|
const data = await chrome.storage.sync.get({ services: [], notifyOnline: false });
|
||||||
@@ -108,25 +144,31 @@ async function checkAllServices() {
|
|||||||
// Benachrichtigung für Offline-Wechsel
|
// Benachrichtigung für Offline-Wechsel
|
||||||
if (previousResult?.status === 'online' && currentResult.status === 'offline') {
|
if (previousResult?.status === 'online' && currentResult.status === 'offline') {
|
||||||
chrome.notifications.create({
|
chrome.notifications.create({
|
||||||
type: 'basic', iconUrl: 'icons/notification_warning.png',
|
type: 'basic',
|
||||||
|
iconUrl: 'icons/notification_warning.png',
|
||||||
title: `Server "${service.name}" ist offline`,
|
title: `Server "${service.name}" ist offline`,
|
||||||
message: 'Der Dienst antwortet nicht mehr.', contextMessage: `Adresse: ${service.adresse}`,
|
message: 'Der Dienst antwortet nicht mehr.',
|
||||||
priority: 1, isClickable: true,
|
contextMessage: `Adresse: ${service.adresse}`,
|
||||||
buttons: [{ title: 'Anzeigen' }, { title: 'Ignorieren' }]
|
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)
|
// Benachrichtigung für Online-Wechsel (wenn aktiviert)
|
||||||
if (notifyOnline && previousResult?.status === 'offline' && currentResult.status === 'online') {
|
if (notifyOnline && previousResult?.status === 'offline' && currentResult.status === 'online') {
|
||||||
chrome.notifications.create({
|
chrome.notifications.create({
|
||||||
type: 'basic', iconUrl: 'icons/notification_warning.png',
|
type: 'basic',
|
||||||
|
iconUrl: 'icons/notification_warning.png',
|
||||||
title: `Server "${service.name}" ist wieder online!`,
|
title: `Server "${service.name}" ist wieder online!`,
|
||||||
message: 'Der Dienst ist wieder erreichbar.', contextMessage: `Adresse: ${service.adresse}`,
|
message: 'Der Dienst ist wieder erreichbar.',
|
||||||
priority: 0, isClickable: true,
|
contextMessage: `Adresse: ${service.adresse}`,
|
||||||
buttons: [{ title: 'Anzeigen' }]
|
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
|
// Verlaufsdaten aktualisieren
|
||||||
@@ -155,10 +197,6 @@ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Benachrichtigungs-Listener
|
// Benachrichtigungs-Listener
|
||||||
chrome.notifications.onButtonClicked.addListener((notificationId, buttonIndex) => {
|
|
||||||
if (buttonIndex === 0) chrome.action.openPopup();
|
|
||||||
chrome.notifications.clear(notificationId);
|
|
||||||
});
|
|
||||||
chrome.notifications.onClicked.addListener((notificationId) => {
|
chrome.notifications.onClicked.addListener((notificationId) => {
|
||||||
chrome.action.openPopup();
|
chrome.action.openPopup();
|
||||||
chrome.notifications.clear(notificationId);
|
chrome.notifications.clear(notificationId);
|
||||||
|
|||||||
Reference in New Issue
Block a user