Skip to content

Commit

Permalink
Actualizar index.html
Browse files Browse the repository at this point in the history
  • Loading branch information
nubesurrealista authored Dec 19, 2024
1 parent c68b8e7 commit 542ef61
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,21 @@
transform: scale(1.05);
}

.link-preview {
margin-top: 10px;
padding: 10px;
border: 1px solid var(--link-color);
border-radius: 5px;
background-color: var(--background-color);
color: var(--text-color);
}

.link-preview img {
max-width: 100%;
height: auto;
border-radius: 5px;
}

#theme-toggle {
position: fixed;
top: 10px;
Expand Down Expand Up @@ -193,13 +208,53 @@ <h1>Mis Toots <img id="mastodon-icon" src="https://upload.wikimedia.org/wikipedi
<script>
const instanceURL = 'https://tkz.one';
const userHandle = 'nubesurrealista';
const linkPreviewApiKey = 'b0a8335fdc903c0b312b64d3c72b62c2';

function sanitizeHTML(html) {
const template = document.createElement('template');
template.innerHTML = html;
return template.content.textContent || '';
}

function convertLinksToClickable(content) {
const urlPattern = /https?:\/\/[^\s]+/g;
return content.replace(urlPattern, (url) => {
return `<a href="${url}" target="_blank" rel="noopener noreferrer">${url}</a>`;
});
}

async function fetchLinkPreview(url) {
try {
const response = await fetch(`https://api.linkpreview.net/?key=${linkPreviewApiKey}&q=${encodeURIComponent(url)}`);
if (response.ok) {
return response.json();
} else {
throw new Error('Error fetching link preview');
}
} catch (error) {
console.error('Error fetching link preview:', error);
return null;
}
}

async function generateLinkPreviews(contentDiv) {
const links = contentDiv.querySelectorAll('a');
for (const link of links) {
const url = link.href;
const previewData = await fetchLinkPreview(url);
if (previewData) {
const previewHtml = `
<div class="link-preview">
<img src="${previewData.image}" alt="${previewData.title}">
<p><strong>${previewData.title}</strong></p>
<p>${previewData.description}</p>
</div>
`;
link.insertAdjacentHTML('afterend', previewHtml);
}
}
}

async function loadToots() {
const errorDiv = document.getElementById("error-message");
const contentDiv = document.getElementById("rss-content");
Expand Down Expand Up @@ -230,6 +285,7 @@ <h1>Mis Toots <img id="mastodon-icon" src="https://upload.wikimedia.org/wikipedi
const tootContent = isReblog ? toot.reblog : toot;

let content = sanitizeHTML(tootContent.content);
content = convertLinksToClickable(content);
const createdAt = new Date(tootContent.created_at).toLocaleString();
const url = tootContent.url;
const mediaAttachments = tootContent.media_attachments;
Expand Down Expand Up @@ -275,6 +331,9 @@ <h3>${tootContent.account.display_name}</h3>
`;
contentDiv.innerHTML += htmlContent;
});

// Generate link previews after toots are loaded
await generateLinkPreviews(contentDiv);
} else {
errorDiv.innerText = 'No se encontraron toots.';
}
Expand All @@ -297,6 +356,7 @@ <h3>${tootContent.account.display_name}</h3>
}
const post = await postResponse.json();
let content = sanitizeHTML(post.content);
content = convertLinksToClickable(content);
const createdAt = new Date(post.created_at).toLocaleString();
const url = post.url;
const mediaAttachments = post.media_attachments;
Expand Down Expand Up @@ -337,6 +397,9 @@ <h3>${post.account.display_name}</h3>
`;
modalContent.innerHTML = htmlContent;
modal.style.display = "block";

// Generate link previews for the modal content
await generateLinkPreviews(modalContent);
} catch (error) {
console.error("Error:", error);
errorDiv.innerText = `Error: ${error.message}`;
Expand Down

0 comments on commit 542ef61

Please sign in to comment.