-
Notifications
You must be signed in to change notification settings - Fork 0
/
bsky-feed.js
66 lines (58 loc) · 2.48 KB
/
bsky-feed.js
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
async function fetchRSSFeed() {
try {
const rssUrl = 'your_bluesky_feed-url'; // Replace with your RSS feed URL
const proxyUrl = `https://a.proxy.url/rss-proxy?url=${encodeURIComponent(rssUrl)}`; // Insert proxy URL if receiving CORS errors, or remove the proxy functions
console.log(`Fetching RSS feed from: ${proxyUrl}`);
const response = await fetch(proxyUrl);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const text = await response.text();
console.log('Fetched RSS feed:', text);
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(text, 'application/xml');
const items = xmlDoc.getElementsByTagName('item');
console.log('Parsed RSS feed items:', items);
displayRSSFeed(items);
} catch (error) {
console.error('Error fetching RSS feed:', error);
}
}
function displayRSSFeed(items) {
const rssContainer = document.getElementById('rss-feed');
rssContainer.innerHTML = '<p>Bluesky Feed</p>';
const list = document.createElement('ul');
for (let i = 0; i < Math.min(items.length, 3); i++) {
const item = items[i];
const descriptionElement = item.getElementsByTagName('description')[0];
const contentElement = item.getElementsByTagName('content:encoded')[0];
const linkElement = item.getElementsByTagName('link')[0];
const pubDateElement = item.getElementsByTagName('pubDate')[0];
if (!descriptionElement && !contentElement) {
console.warn('Missing description or content in RSS feed item:', item);
continue;
}
const content = descriptionElement ? descriptionElement.textContent : contentElement.textContent;
const truncatedContent = content.length > 50 ? content.substring(0, 50) + '...' : content;
let author = 'Unknown author';
let link = '#';
if (linkElement) {
link = linkElement.textContent;
const match = link.match(/profile\/([^\/]+)/);
if (match) {
author = match[1];
}
}
const pubDate = pubDateElement ? new Date(pubDateElement.textContent).toLocaleDateString() : 'Unknown date';
const listItem = document.createElement('li');
listItem.innerHTML = `
<p><strong>${author}</strong></p>
<p><small class="rss-date">posted: ${pubDate}</small></p>
<p>${truncatedContent} <a href="${link}" target="_blank">more</a></p>
`;
list.appendChild(listItem);
}
rssContainer.appendChild(list);
}
// Call the function to fetch and display the RSS feed
fetchRSSFeed();