diff --git a/code.js b/code.js index da295ea..b81869e 100644 --- a/code.js +++ b/code.js @@ -52,7 +52,7 @@ function createStarterKitElement(kit) { authorLink.href = kit.author; authorLink.target = '_blank'; - fetchAuthorProfile(kit.author, authorLink); + fetchProfile(kit.author, { updateElement: authorLink }); authorContainer.textContent = 'zusammengestellt von '; authorContainer.appendChild(authorLink); @@ -71,41 +71,6 @@ function createStarterKitElement(kit) { return kitElement; } -/** - * Ruft den Avatar und andere Informationen für den Autor ab und fügt sie hinzu - */ -function fetchAuthorProfile(authorUrl, linkElement) { - try { - const url = new URL(authorUrl); - const pathParts = url.pathname.split('/'); - if (pathParts.length > 1 && pathParts[1].startsWith('@')) { - const handle = pathParts[1].substring(1); - const instance = url.hostname; - const lookupUrl = `https://${instance}/api/v1/accounts/lookup?acct=${handle}`; - - fetch(lookupUrl) - .then(response => response.json()) - .then(data => { - linkElement.textContent = data.display_name || data.username; - - const avatarImage = document.createElement('img'); - avatarImage.classList.add('account-avatar'); - avatarImage.alt = data.avatar ? `Profilbild von ${data.username}` : 'Profilbild nicht verfügbar'; - avatarImage.src = data.avatar || ''; - linkElement.prepend(avatarImage); - - const serverClass = getServerClass(instance); - if (serverClass) { - linkElement.classList.add(serverClass); - } - }) - .catch(error => console.error(`Fehler beim Abrufen des Profils für ${authorUrl}:`, error)); - } - } catch (error) { - console.error('Ungültige URL:', authorUrl, error); - } -} - /** * Zusätzliche Funktionen für StarterKits */ @@ -150,14 +115,9 @@ function createRecommendationPopup(accounts, title) { body.classList.add('recommendation-popup-body'); accounts.forEach(accountUrl => { - const accountLink = document.createElement('a'); - accountLink.classList.add('account'); - accountLink.href = accountUrl; - accountLink.target = '_blank'; - - // Lade Profilinformationen für das Konto - fetchAccountProfile(accountUrl, accountLink) - .then(card => body.appendChild(card)); + fetchProfile(accountUrl, { createCard: true }) + .then(card => body.appendChild(card)) + .catch(error => console.error(`Fehler beim Laden des Accounts: ${accountUrl}`, error)); }); content.appendChild(header); @@ -174,12 +134,14 @@ function createRecommendationPopup(accounts, title) { } /** - * Ruft Profilinformationen ab und erstellt eine Profil-Kachel + * Ruft Profilinformationen ab und aktualisiert ein DOM-Element oder erstellt eine Profil-Kachel. */ -function fetchAccountProfile(accountUrl, linkElement) { +function fetchProfile(profileUrl, options = {}) { + const { updateElement = null, createCard = false } = options; + return new Promise((resolve, reject) => { try { - const url = new URL(accountUrl); + const url = new URL(profileUrl); const pathParts = url.pathname.split('/'); if (pathParts.length > 1 && pathParts[1].startsWith('@')) { const handle = pathParts[1].substring(1); @@ -189,16 +151,35 @@ function fetchAccountProfile(accountUrl, linkElement) { fetch(lookupUrl) .then(response => response.json()) .then(data => { - const card = createAccountCard(data, accountUrl, instance); - resolve(card); + if (updateElement) { + // Aktualisiere das übergebene Element + updateElement.textContent = data.display_name || data.username; + + const avatarImage = document.createElement('img'); + avatarImage.classList.add('account-avatar'); + avatarImage.alt = data.avatar ? `Profilbild von ${data.username}` : 'Profilbild nicht verfügbar'; + avatarImage.src = data.avatar || ''; + updateElement.prepend(avatarImage); + + const serverClass = getServerClass(instance); + if (serverClass) { + updateElement.classList.add(serverClass); + } + + resolve(updateElement); + } else if (createCard) { + // Erstelle eine Profil-Kachel + const card = createAccountCard(data, profileUrl, instance); + resolve(card); + } }) .catch(error => { - console.error(`Fehler beim Abrufen des Profils für ${accountUrl}:`, error); + console.error(`Fehler beim Abrufen des Profils für ${profileUrl}:`, error); reject(error); }); } } catch (error) { - console.error('Ungültige URL:', accountUrl, error); + console.error('Ungültige URL:', profileUrl, error); reject(error); } }); @@ -234,7 +215,12 @@ function createAccountCard(data, href, instance) { const serverHandleElement = document.createElement('span'); serverHandleElement.classList.add('server-handle'); serverHandleElement.textContent = `@${instance}`; - serverHandleElement.style.backgroundColor = getServerClass(instance); + + // Ergänze die Klasse basierend auf der Instanz + const serverClass = getServerClass(instance); + if (serverClass) { + serverHandleElement.classList.add(serverClass); + } handleElement.textContent = `@${data.username}`; handleElement.appendChild(serverHandleElement); @@ -273,12 +259,12 @@ function seededRandom(seed) { function getServerClass(instance) { const serverClasses = { - 'libori.social': 'rgb(255, 86, 86)', - 'reliverse.social': 'rgb(255, 165, 0)', - 'kirche.social': 'rgb(28, 125, 126)', - 'katholisch.social': 'rgb(14, 123, 226)' + 'libori.social': 'liboriSocial', + 'reliverse.social': 'reliverseSocial', + 'kirche.social': 'kircheSocial', + 'katholisch.social': 'katholischSocial' }; - return serverClasses[instance] || 'rgb(69, 89, 109)'; + return serverClasses[instance] || null; } /**