-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
47 lines (42 loc) · 1.86 KB
/
main.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
const rss_url = 'https://dotnet.social/@TimPurdum.rss';
let mastoDiv = document.getElementById('mastodon');
fetch(rss_url)
.then(response => response.text())
.then(str => new window.DOMParser().parseFromString(str, "text/xml"))
.then(data => {
console.log(data);
const items = data.querySelectorAll("item");
const monthFormatter = new Intl.DateTimeFormat('en', { month: 'long'});
const dayFormatter = new Intl.DateTimeFormat('en', { day: 'numeric'});
const yearFormatter = new Intl.DateTimeFormat('en', { year: 'numeric'});
items.forEach(el => {
let content = el.querySelector("description").innerHTML.trim();
if (content.includes("Content warning:")) {
return;
}
let date = new Date(el.querySelector("pubDate").innerHTML);
let article = document.createElement('article');
let month = monthFormatter.format(date);
let day = dayFormatter.format(date);
let year = yearFormatter.format(date);
article.innerHTML = `<h3>${month} ${day}, ${year}</h3>`;
article.innerHTML += decodeEntity(content);
let media = el.querySelector("content");
if (media !== null) {
let mediaUrl = media.getAttribute("url");
if (mediaUrl !== null) {
article.innerHTML += `<img src="${mediaUrl}" />`;
}
}
let link = document.createElement('a');
link.target = '_blank';
link.href = el.querySelector("link").innerHTML;
link.appendChild(article);
mastoDiv.appendChild(link);
});
});
function decodeEntity(inputStr) {
var textarea = document.createElement("textarea");
textarea.innerHTML = inputStr;
return textarea.value;
}