-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtimeline-basic-rss.html
380 lines (323 loc) · 14.2 KB
/
timeline-basic-rss.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>User Timeline via RSS Feed (JavaScript)</title>
<meta name="author" content="@[email protected]" />
<meta name="keywords"
content="mastodon, embed, timeline, rss, xml, javascript, fediverse, example" />
<meta name="description"
content="Example showing how to display a user timeline by fetching and parsing their Mastodon RSS feed using client-side JavaScript." />
<meta property="og:title" content="Mastodon User Timeline via RSS Feed (JavaScript)" />
<meta property="og:type" content="website" />
<meta property="og:url" content="https://mastodon-embeds.glitch.me/timeline-basic-rss.html" />
<meta property="og:image" content="https://mastodon-embeds.glitch.me/mastodon-logo.svg" />
<meta property="og:description"
content="Example showing how to display a user timeline by fetching and parsing their Mastodon RSS feed." />
<meta property="og:site_name" content="Mastodon Embed Examples" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/css/bulma.min.css" />
<style>
.section.has-top-controls {
position: relative;
padding-top: 4rem;
}
.theme-toggle-button {
position: absolute;
top: 1.5rem;
right: 1.5rem;
width: 2.5rem;
height: 2.5rem;
padding: 0;
border-radius: 50%;
display: inline-flex;
align-items: center;
justify-content: center;
}
.theme-toggle-button .icon svg {
width: 1.1em;
height: 1.1em;
}
#rss-timeline-output {
min-height: 200px;
margin-top: 1rem;
}
/* Styling for the rendered timeline posts */
.rss-timeline-post {
border: 1px solid #dbdbdb;
border-radius: 6px;
padding: 1rem;
margin-bottom: 1rem;
background-color: white;
}
[data-theme="dark"] .rss-timeline-post {
border-color: #4a4a4a;
background-color: #2e2e2e;
}
.rss-timeline-post .post-content {
margin-bottom: 0.75rem;
word-wrap: break-word;
font-size: 0.95em;
}
.rss-timeline-post .post-content p {
margin-bottom: 0.5em !important;
}
.rss-timeline-post .post-content a {
color: hsl(217, 71%, 53%);
}
[data-theme="dark"] .rss-timeline-post .post-content a {
color: hsl(204, 71%, 63%);
}
.rss-timeline-post .post-meta {
font-size: 0.8em;
color: #7a7a7a;
padding-top: 0.5rem;
margin-top: 0.75rem;
}
[data-theme="dark"] .rss-timeline-post .post-meta {
color: #b5b5b5;
}
.rss-timeline-post .post-meta a {
color: inherit;
text-decoration: underline;
}
/* Style for config inputs */
.config-section label {
font-weight: bold;
}
.config-section input {
margin-bottom: 0.5rem;
}
.message {
margin-top: 1rem;
margin-bottom: 1rem;
}
</style>
</head>
<body>
<section class="section has-top-controls">
<button class="button theme-toggle-button" aria-label="Toggle theme">
<span class="icon theme-icon">
</span>
</button>
<div class="container">
<h1 class="title">Mastodon User Timeline via RSS Feed</h1>
<div class="box">
<div class="content">
<h2 class="title is-4">About This Method</h2>
<p>
Mastodon instances provide a public RSS feed for each user's public posts, found at
<code>https://[instance.domain]/users/[username].rss</code>.
</p>
<p>
This example uses client-side JavaScript to attempt to fetch this RSS feed, parse the XML content using
the browser's built-in <code>DOMParser</code>, and then render the latest posts below.
</p>
<article class="message is-info">
<div class="message-header">
<p>Content Limitations</p>
</div>
<div class="message-body">
The RSS feed only includes the user's <em>public, original posts</em>. It excludes replies
and boosts.<br>
The standard RSS format lacks a specific field for <em>Content Warnings (CWs)</em>. Posts with
CWs will have their full content displayed immediately.
</div>
</article>
<article class="message is-warning">
<div class="message-header">
<p>CORS Limitation</p>
</div>
<div class="message-body">
Fetching resources (like RSS feeds) directly from a different domain using JavaScript is often blocked
by browser security policies (CORS). This example <em>may not work reliably</em> unless the Mastodon
instance's server is configured to allow requests from your website's origin, or if you use a <a
href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS/Errors/CORSMissingAllowOrigin#what_went_wrong"
target="_blank" rel="noopener noreferrer">CORS proxy</a>. It is included mainly as a demonstration of
parsing the feed format. Check the browser console for errors if the feed doesn't load.
</div>
</article>
</div>
</div>
<div class="box config-section">
<div class="content">
<h2 class="title is-4">Configuration</h2>
<div class="field">
<label class="label" for="rss-instance">Instance Domain:</label>
<div class="control">
<input class="input" type="text" id="rss-instance" value="macaw.social">
</div>
</div>
<div class="field">
<label class="label" for="rss-username">Username:</label>
<div class="control">
<input class="input" type="text" id="rss-username" value="andypiper">
</div>
</div>
<div class="field">
<label class="label" for="rss-limit">Number of Posts:</label>
<div class="control">
<input class="input" type="number" id="rss-limit" value="15" min="1" max="20">
</div>
</div>
<div class="field">
<div class="control">
<button id="fetch-rss-button" class="button is-info">Fetch & Render RSS Feed</button>
</div>
</div>
</div>
</div>
<div class="box">
<div class="content">
<h2 class="title is-4">Rendered Timeline (from RSS)</h2>
<p>Attempting to fetch and display the latest posts from the RSS feed:</p>
<div id="rss-timeline-output">
<p><i>Click button above to load feed...</i></p>
</div>
</div>
</div>
</div>
</section>
<footer class="footer">
<div class="content has-text-centered">
<p>
<strong><a href="https://mastodon-embeds.glitch.me">Mastodon Embed Examples</a></strong>.
Styled using <a href="https://bulma.io/" target="_blank" rel="noopener noreferrer">Bulma</a>.
Find me on Mastodon: <a href="https://macaw.social/@andypiper" target="_blank"
rel="noopener noreferrer">@[email protected]</a>.
</p>
</div>
</footer>
<script>
(function () {
// Simple inline SVGs for icons
const sunIcon = `<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="4"/><path d="M12 2v2"/><path d="M12 20v2"/><path d="m4.93 4.93 1.41 1.41"/><path d="m17.66 17.66 1.41 1.41"/><path d="M2 12h2"/><path d="M20 12h2"/><path d="m6.34 17.66-1.41 1.41"/><path d="m19.07 4.93-1.41 1.41"/></svg>`;
const moonIcon = `<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 3a6 6 0 0 0 9 9 9 9 0 1 1-9-9Z"/></svg>`;
// Select ALL buttons with this class
const toggleButtons = document.querySelectorAll('.theme-toggle-button');
// Select ALL icon spans within those buttons
const iconSpans = document.querySelectorAll('.theme-toggle-button .theme-icon');
const htmlElement = document.documentElement;
const storageKey = 'theme-preference';
// Function to apply theme and update ALL icons
const applyTheme = (theme) => {
htmlElement.dataset.theme = theme;
const iconHTML = theme === 'dark' ? moonIcon : sunIcon;
iconSpans.forEach(span => {
// Check if span exists before setting innerHTML
if (span) span.innerHTML = iconHTML;
});
};
// Function to get the preferred theme
const getPreferredTheme = () => {
const savedTheme = localStorage.getItem(storageKey);
if (savedTheme) return savedTheme;
// Default to light if no system preference detected or needed
return window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
};
// Function to handle the button click
const handleToggleClick = () => {
// Read the current theme directly from the html element
let currentTheme = htmlElement.dataset.theme || getPreferredTheme();
let newTheme = currentTheme === 'dark' ? 'light' : 'dark';
applyTheme(newTheme);
localStorage.setItem(storageKey, newTheme);
}
// --- Initialization ---
// Apply the initial theme when the script runs
let currentTheme = getPreferredTheme();
applyTheme(currentTheme);
// Add listeners to all toggle buttons found
if (toggleButtons.length > 0) {
toggleButtons.forEach(button => {
button.addEventListener('click', handleToggleClick);
});
} else {
// It's okay if not found on pages without the button
// console.warn("Theme toggle button(s) not found on this page.");
}
})();
</script>
<script>
const fetchButton = document.getElementById('fetch-rss-button');
const outputDiv = document.getElementById('rss-timeline-output');
const instanceInput = document.getElementById('rss-instance');
const usernameInput = document.getElementById('rss-username');
const limitInput = document.getElementById('rss-limit');
const getText = (element, tagName) => {
const node = element.querySelector(tagName);
return node ? node.textContent : '';
};
const fetchAndRenderRSS = () => {
const instance = instanceInput.value.trim();
const username = usernameInput.value.trim();
const limit = parseInt(limitInput.value, 10) || 10;
if (!instance || !username) {
outputDiv.innerHTML = '<div class="notification is-danger">Please enter both instance and username.</div>';
return;
}
const rssUrl = `https://${instance}/users/${username}.rss`;
outputDiv.innerHTML = `<p><i>Fetching ${rssUrl}...</i></p>`;
fetch(rssUrl)
.then(response => {
if (!response.ok) {
if (response.status === 0 || response.type === 'opaque') {
throw new Error(`Network error fetching RSS feed. This is likely a CORS issue preventing the browser from accessing the feed directly from a different domain. Check the browser console for details.`);
}
throw new Error(`HTTP error! status: ${response.status} ${response.statusText}`);
}
return response.text();
})
.then(xmlText => {
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(xmlText, "text/xml");
const parseError = xmlDoc.querySelector("parsererror");
if (parseError) {
console.error("XML Parsing Error:", parseError);
throw new Error(`Failed to parse RSS feed. Response might not be valid XML or the content type was incorrect. Check console.`);
}
const items = xmlDoc.querySelectorAll('item');
let allPostsHtml = '';
let count = 0;
items.forEach(item => {
if (count >= limit) return;
const link = getText(item, 'link');
const pubDateStr = getText(item, 'pubDate');
const description = getText(item, 'description');
let formattedDate = 'Unknown date';
if (pubDateStr) {
try {
formattedDate = new Date(pubDateStr).toLocaleString();
} catch (e) {
console.warn(`Could not parse date: ${pubDateStr}`);
formattedDate = pubDateStr;
}
}
// WARNING: Rendering HTML from external source directly has security risks.
// Also, this does not handle Content Warnings within the description.
allPostsHtml += `
<div class="rss-timeline-post">
<div class="content post-content">${description || '(No content)'}</div>
<div class="post-meta">
<a href="${link}" target="_blank" rel="noopener noreferrer">Published: ${formattedDate}</a>
</div>
</div>`;
count++;
});
if (allPostsHtml) {
outputDiv.innerHTML = allPostsHtml;
} else {
outputDiv.innerHTML = '<p>No posts found in the RSS feed.</p>';
}
})
.catch(error => {
console.error("Error fetching or parsing RSS feed:", error);
outputDiv.innerHTML = `<div class="notification is-danger">${error.message || 'An unknown error occurred.'}</div>`;
});
};
if (fetchButton) {
fetchButton.addEventListener('click', fetchAndRenderRSS);
}
</script>
</body>
</html>