mirror of
https://codeberg.org/kirche-im-netz/Startodon-Hub.git
synced 2025-08-17 16:20:57 +02:00
code optimiert
This commit is contained in:
parent
6395a16915
commit
66036d92c9
1 changed files with 43 additions and 57 deletions
98
code.js
98
code.js
|
@ -52,7 +52,7 @@ function createStarterKitElement(kit) {
|
||||||
authorLink.href = kit.author;
|
authorLink.href = kit.author;
|
||||||
authorLink.target = '_blank';
|
authorLink.target = '_blank';
|
||||||
|
|
||||||
fetchAuthorProfile(kit.author, authorLink);
|
fetchProfile(kit.author, { updateElement: authorLink });
|
||||||
authorContainer.textContent = 'zusammengestellt von ';
|
authorContainer.textContent = 'zusammengestellt von ';
|
||||||
authorContainer.appendChild(authorLink);
|
authorContainer.appendChild(authorLink);
|
||||||
|
|
||||||
|
@ -71,41 +71,6 @@ function createStarterKitElement(kit) {
|
||||||
return kitElement;
|
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
|
* Zusätzliche Funktionen für StarterKits
|
||||||
*/
|
*/
|
||||||
|
@ -150,14 +115,9 @@ function createRecommendationPopup(accounts, title) {
|
||||||
body.classList.add('recommendation-popup-body');
|
body.classList.add('recommendation-popup-body');
|
||||||
|
|
||||||
accounts.forEach(accountUrl => {
|
accounts.forEach(accountUrl => {
|
||||||
const accountLink = document.createElement('a');
|
fetchProfile(accountUrl, { createCard: true })
|
||||||
accountLink.classList.add('account');
|
.then(card => body.appendChild(card))
|
||||||
accountLink.href = accountUrl;
|
.catch(error => console.error(`Fehler beim Laden des Accounts: ${accountUrl}`, error));
|
||||||
accountLink.target = '_blank';
|
|
||||||
|
|
||||||
// Lade Profilinformationen für das Konto
|
|
||||||
fetchAccountProfile(accountUrl, accountLink)
|
|
||||||
.then(card => body.appendChild(card));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
content.appendChild(header);
|
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) => {
|
return new Promise((resolve, reject) => {
|
||||||
try {
|
try {
|
||||||
const url = new URL(accountUrl);
|
const url = new URL(profileUrl);
|
||||||
const pathParts = url.pathname.split('/');
|
const pathParts = url.pathname.split('/');
|
||||||
if (pathParts.length > 1 && pathParts[1].startsWith('@')) {
|
if (pathParts.length > 1 && pathParts[1].startsWith('@')) {
|
||||||
const handle = pathParts[1].substring(1);
|
const handle = pathParts[1].substring(1);
|
||||||
|
@ -189,16 +151,35 @@ function fetchAccountProfile(accountUrl, linkElement) {
|
||||||
fetch(lookupUrl)
|
fetch(lookupUrl)
|
||||||
.then(response => response.json())
|
.then(response => response.json())
|
||||||
.then(data => {
|
.then(data => {
|
||||||
const card = createAccountCard(data, accountUrl, instance);
|
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);
|
resolve(card);
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.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);
|
reject(error);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Ungültige URL:', accountUrl, error);
|
console.error('Ungültige URL:', profileUrl, error);
|
||||||
reject(error);
|
reject(error);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -234,7 +215,12 @@ function createAccountCard(data, href, instance) {
|
||||||
const serverHandleElement = document.createElement('span');
|
const serverHandleElement = document.createElement('span');
|
||||||
serverHandleElement.classList.add('server-handle');
|
serverHandleElement.classList.add('server-handle');
|
||||||
serverHandleElement.textContent = `@${instance}`;
|
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.textContent = `@${data.username}`;
|
||||||
handleElement.appendChild(serverHandleElement);
|
handleElement.appendChild(serverHandleElement);
|
||||||
|
@ -273,12 +259,12 @@ function seededRandom(seed) {
|
||||||
|
|
||||||
function getServerClass(instance) {
|
function getServerClass(instance) {
|
||||||
const serverClasses = {
|
const serverClasses = {
|
||||||
'libori.social': 'rgb(255, 86, 86)',
|
'libori.social': 'liboriSocial',
|
||||||
'reliverse.social': 'rgb(255, 165, 0)',
|
'reliverse.social': 'reliverseSocial',
|
||||||
'kirche.social': 'rgb(28, 125, 126)',
|
'kirche.social': 'kircheSocial',
|
||||||
'katholisch.social': 'rgb(14, 123, 226)'
|
'katholisch.social': 'katholischSocial'
|
||||||
};
|
};
|
||||||
return serverClasses[instance] || 'rgb(69, 89, 109)';
|
return serverClasses[instance] || null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue