(function() { // Webmentions functionality async function loadWebmentions() { const webmentionsContainer = document.getElementById('webmentions-list'); if (!webmentionsContainer) return; const currentUrl = window.location.href; const domain = window.location.hostname; try { // Fetch webmentions from webmention.io const response = await fetch(`https://webmention.io/api/mentions.jf2?target=${encodeURIComponent(currentUrl)}&per-page=50`); const data = await response.json(); if (data.children && data.children.length > 0) { displayWebmentions(data.children, webmentionsContainer); } else { showEmptyState(webmentionsContainer); } } catch (error) { console.warn('Failed to load webmentions:', error); showEmptyState(webmentionsContainer); } } function displayWebmentions(mentions, container) { const likes = mentions.filter(m => m['wm-property'] === 'like-of'); const reposts = mentions.filter(m => m['wm-property'] === 'repost-of'); const replies = mentions.filter(m => m['wm-property'] === 'in-reply-to'); const bookmarks = mentions.filter(m => m['wm-property'] === 'bookmark-of'); const general = mentions.filter(m => m['wm-property'] === 'mention-of'); let html = ''; // Show counts if (likes.length > 0 || reposts.length > 0 || bookmarks.length > 0) { html += '
'; if (likes.length > 0) html += `❤️ ${likes.length} like${likes.length !== 1 ? 's' : ''}`; if (reposts.length > 0) html += `🔄 ${reposts.length} repost${reposts.length !== 1 ? 's' : ''}`; if (bookmarks.length > 0) html += `🔖 ${bookmarks.length} bookmark${bookmarks.length !== 1 ? 's' : ''}`; html += '
'; } // Show replies and mentions const conversationMentions = [...replies, ...general].sort((a, b) => new Date(a.published) - new Date(b.published)); if (conversationMentions.length > 0) { html += '
'; conversationMentions.forEach(mention => { html += renderMention(mention); }); html += '
'; } container.innerHTML = html; } function renderMention(mention) { const author = mention.author || {}; const content = mention.content || {}; const published = mention.published ? new Date(mention.published).toLocaleDateString() : ''; return `
${author.photo ? `${author.name || 'Anonymous'}` : `
${(author.name || 'A')[0].toUpperCase()}
` }
${author.name || 'Anonymous'} ${mention['wm-property'] === 'in-reply-to' ? 'replied' : 'mentioned this'} ${published ? `` : ''}
${content.text ? `
${content.text.length > 280 ? content.text.substring(0, 280) + '...' : content.text}
` : '' }
`; } function showEmptyState(container) { container.innerHTML = `

No webmentions yet. Be the first to respond!

Learn about webmentions

`; } // Initialize webmentions when DOM is ready if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', loadWebmentions); } else { loadWebmentions(); } })();