forked from lambdaspace/ConsuelaAddOn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents.js
56 lines (50 loc) · 1.65 KB
/
events.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
function eventParser(topic) {
var event = {};
var tokens = topic.split(' ');
event.day = tokens[0];
if (!event.day.match(/^\d\d\/\d\d\/\d\d\d\d+$/)) {
throw 'Not in expected format';
}
var dateTokens = tokens[0].split('/');
event.date = new Date(dateTokens[2], dateTokens[1] - 1, dateTokens[0], 0, 0);
if (tokens[1].match(/^\d\d:\d\d+$/)) {
event.time = tokens[1];
event.title = topic.substr(17);
} else {
event.time = "";
event.title = topic.substr(11);
}
return event;
};
function parseEvents(data) {
data.topic_list.topics.forEach(function(topic) {
var event;
try {
event = eventParser(topic.title);
} catch(e) {
return;
}
if (event.date > Date.now() - 86400000) {
eventsExist = true;
var link = "https://discourse.techministry.gr/t/" + topic.id;
document.getElementById("events").innerHTML += "<tr class=\"clickable\" url=\"" + link + "\"> <td>" + event.day + "</td> <td>" + event.time + "</td> <td>" + event.title + "</td> </tr>";
}
});
};
function httpGet(theUrl) {
var xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", theUrl, false );
xmlHttp.send( null );
return xmlHttp.responseText;
};
var eventsExist = false;
parseEvents(JSON.parse(httpGet("https://discourse.techministry.gr/c/5/l/latest.json")));
if (eventsExist) {
Array.from(document.getElementsByClassName("clickable")).forEach(function(eventRow) {
eventRow.onclick = function() {
window.open(eventRow.getAttribute("url"));
};
});
} else {
document.getElementById("events").outerHTML = "<p> There are currently no upcoming events <br> Check again later and have a nice day :) </p>";
}