-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscript.js
More file actions
70 lines (59 loc) · 2.6 KB
/
script.js
File metadata and controls
70 lines (59 loc) · 2.6 KB
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
"use strict";
const RSS_URL = "https://anchor.fm/s/1d60e538/podcast/rss";
const MAX_PODCASTS = 5;
fetch(RSS_URL)
.then(res => res.text())
.catch(err => {
console.error(err);
const p = document.createElement("p");
p.textContent = "Seems like there was a problem getting the feed...";
document.getElementById("podcasts").append(p);
})
.then(rss => (new DOMParser()).parseFromString(rss, "text/xml"))
.then(rss => {
const items = rss.getElementsByTagName("item");
const length = items.length < MAX_PODCASTS ? items.length : MAX_PODCASTS;
for(let i = 0; i < length; i++) {
const item = items[i];
const titleText = item.getElementsByTagName("title")[0].textContent;
const linkText = item.getElementsByTagName("link")[0].textContent;
const dateText = item.getElementsByTagName("pubDate")[0].textContent;
const podcast = document.createElement("li");
const link = document.createElement("a");
link.href = linkText;
link.target = "blank";
const date = document.createElement("p");
date.textContent = ago(new Date(dateText));
link.append(date);
const title = document.createElement("h4");
title.textContent = titleText;
link.append(title);
podcast.append(link);
document.getElementById("podcasts").append(podcast);
}
});
// source - https://www.npmjs.com/package/s-ago
function format(diff, divisor, unit, past, future, isInTheFuture) {
const val = Math.round(Math.abs(diff) / divisor);
if (isInTheFuture)
return val <= 1 ? future : 'in ' + val + ' ' + unit + 's';
return val <= 1 ? past : val + ' ' + unit + 's ago';
}
const units = [
{ max: 2760000, value: 60000, name: 'minute', past: 'a minute ago', future: 'in a minute' },
{ max: 72000000, value: 3600000, name: 'hour', past: 'an hour ago', future: 'in an hour' },
{ max: 518400000, value: 86400000, name: 'day', past: 'yesterday', future: 'tomorrow' },
{ max: 2419200000, value: 604800000, name: 'week', past: 'last week', future: 'in a week' },
{ max: 28512000000, value: 2592000000, name: 'month', past: 'last month', future: 'in a month' } // max: 11 months
];
function ago(date, max) {
const diff = Date.now() - date.getTime();
if (Math.abs(diff) < 60000)
return 'just now';
for (let i = 0; i < units.length; i++) {
if (Math.abs(diff) < units[i].max || (max && units[i].name === max)) {
return format(diff, units[i].value, units[i].name, units[i].past, units[i].future, diff < 0);
}
}
return format(diff, 31536000000, 'year', 'last year', 'in a year', diff < 0);
};