-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
255 lines (229 loc) · 11.6 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Mis Toots</title>
<link rel="stylesheet" href="style.css"/>
<!-- Remove below link tag to remove favicon or replace with whatever image you want -->
<link rel="icon" type="image/x-icon" href="https://upload.wikimedia.org/wikipedia/commons/4/48/Mastodon_Logotype_%28Simple%29.svg">
</head>
<body data-theme="light">
<h1>Mis Toots <img id="mastodon-icon" src="https://upload.wikimedia.org/wikipedia/commons/4/48/Mastodon_Logotype_%28Simple%29.svg" alt="Mastodon Icon"></h1>
<div id="rss-content">Cargando...</div>
<div id="error-message"></div>
<button id="theme-toggle">Cambiar Tema</button>
<a href="https://github.com/nubesurrealista/Toots" target="_blank" rel="noopener noreferrer">View on GitHub</a>
<!-- Modal for viewing individual posts -->
<div id="postModal" class="modal">
<div class="modal-content">
<img class="close" src="/close.svg" alt="close">
<div id="modal-content"></div>
</div>
</div>
<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;
if (url.includes(instanceURL)) {
continue; // Skip links from the same Mastodon instance
}
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");
contentDiv.innerHTML = 'Cargando...';
errorDiv.innerText = '';
try {
const accountResponse = await fetch(`${instanceURL}/api/v1/accounts/lookup?acct=${userHandle}`);
if (!accountResponse.ok) {
throw new Error(`Error al buscar la cuenta: ${accountResponse.status} ${accountResponse.statusText}`);
}
const account = await accountResponse.json();
if (!account) {
throw new Error(`No se encontró la cuenta ${userHandle}`);
}
const userId = account.id;
const tootsResponse = await fetch(`${instanceURL}/api/v1/accounts/${userId}/statuses?limit=20&exclude_replies=false&exclude_reblogs=false`);
if (!tootsResponse.ok) {
throw new Error(`Error al cargar los toots: ${tootsResponse.status} ${tootsResponse.statusText}`);
}
const toots = await tootsResponse.json();
contentDiv.innerHTML = '';
if (toots.length > 0) {
toots.forEach(toot => {
const isReblog = toot.reblog !== null;
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;
const avatar = tootContent.account.avatar;
const emojis = tootContent.emojis;
let mediaHtml = '';
if (mediaAttachments.length > 0) {
mediaHtml = '<div class="media-gallery">';
mediaAttachments.forEach(media => {
if (media.type === 'image') {
mediaHtml += `<img src="${media.url}" alt="${media.description || 'Imagen adjunta'}">`;
}
});
mediaHtml += '</div>';
}
if (emojis.length > 0) {
emojis.forEach(emoji => {
content = content.replace(`:${emoji.shortcode}:`, `<img src="${emoji.url}" alt="${emoji.shortcode}" class="emoji">`);
});
}
const reblogNotice = isReblog ? '<p>Impulsado por el usuario</p>' : '';
const htmlContent = `
<div class="toot">
${reblogNotice}
<div class="toot-header">
<img src="${avatar}" alt="Avatar" class="avatar">
<div class="toot-info">
<h3>${tootContent.account.display_name}</h3>
<p>@${tootContent.account.acct}</p>
</div>
</div>
<p>${content}</p>
${mediaHtml}
<p>Likes: ${tootContent.favourites_count} | Impulsos: ${tootContent.reblogs_count}</p>
<p><a href="${url}" target="_blank" rel="noopener noreferrer">Publicado el ${createdAt}</a></p>
<button onclick="viewPost('${tootContent.id}')">Ver más</button>
</div>
<hr/>
`;
contentDiv.innerHTML += htmlContent;
});
// Generate link previews after toots are loaded
await generateLinkPreviews(contentDiv);
} else {
errorDiv.innerText = 'No se encontraron toots.';
}
} catch (error) {
console.error("Error:", error);
contentDiv.innerHTML = '';
errorDiv.innerText = `Error: ${error.message}`;
}
}
async function viewPost(postId) {
const modal = document.getElementById("postModal");
const modalContent = document.getElementById("modal-content");
const errorDiv = document.getElementById("error-message");
try {
const postResponse = await fetch(`${instanceURL}/api/v1/statuses/${postId}`);
if (!postResponse.ok) {
throw new Error(`Error al cargar el toot: ${postResponse.status} ${postResponse.statusText}`);
}
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;
const avatar = post.account.avatar;
const emojis = post.emojis;
let mediaHtml = '';
if (mediaAttachments.length > 0) {
mediaHtml = '<div class="media-gallery">';
mediaAttachments.forEach(media => {
if (media.type === 'image') {
mediaHtml += `<img src="${media.url}" alt="${media.description || 'Imagen adjunta'}">`;
}
});
mediaHtml += '</div>';
}
if (emojis.length > 0) {
emojis.forEach(emoji => {
content = content.replace(`:${emoji.shortcode}:`, `<img src="${emoji.url}" alt="${emoji.shortcode}" class="emoji">`);
});
}
const htmlContent = `
<div class="toot">
<div class="toot-header">
<img src="${avatar}" alt="Avatar" class="avatar">
<div class="toot-info">
<h3>${post.account.display_name}</h3>
<p>@${post.account.acct}</p>
</div>
</div>
<p>${content}</p>
${mediaHtml}
<p>Likes: ${post.favourites_count} | Impulsos: ${post.reblogs_count}</p>
<p><a href="${url}" target="_blank" rel="noopener noreferrer">Publicado el ${createdAt}</a></p>
</div>
`;
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}`;
}
}
function toggleTheme() {
const body = document.body;
const currentTheme = body.getAttribute('data-theme');
const newTheme = currentTheme === 'light' ? 'dark' : currentTheme === 'dark' ? 'blue' : 'light';
body.setAttribute('data-theme', newTheme);
document.getElementById('theme-toggle').innerText = `Tema: ${newTheme.charAt(0).toUpperCase() + newTheme.slice(1)}`;
}
document.getElementById('theme-toggle').addEventListener('click', toggleTheme);
window.onload = loadToots;
// Modal close functionality
const modal = document.getElementById("postModal");
const span = document.getElementsByClassName("close")[0];
span.onclick = function() {
modal.style.display = "none";
}
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
</script>
</body>
</html>