Replace client-side JS with server-side relative timestamps
This commit is contained in:
@@ -235,7 +235,7 @@
|
||||
<td class="source">{{.Source}}</td>
|
||||
<td class="title">{{.Title}}</td>
|
||||
<td><a href="{{.Link}}" class="article-link" target="_blank">{{.Summary}}</a></td>
|
||||
<td class="timestamp" title="{{.FirstSeen.Format "2006-01-02 15:04:05 MST"}}" data-timestamp="{{.FirstSeen.Unix}}"></td>
|
||||
<td class="timestamp" title="{{.FirstSeen.Format "2006-01-02 15:04:05 MST"}}">{{.RelativeTime}}</td>
|
||||
</tr>
|
||||
{{else}}
|
||||
<tr>
|
||||
@@ -263,7 +263,7 @@
|
||||
{{range .History}}
|
||||
<tr>
|
||||
<td class="id">{{.ID}}</td>
|
||||
<td class="timestamp" title="{{.BroadcastTime.Format "2006-01-02 15:04:05 MST"}}" data-timestamp="{{.BroadcastTime.Unix}}"></td>
|
||||
<td class="timestamp" title="{{.BroadcastTime.Format "2006-01-02 15:04:05 MST"}}">{{.BroadcastRelativeTime}}</td>
|
||||
<td class="importance {{if ge .Importance 70}}high{{else if ge .Importance 40}}medium{{else}}low{{end}}">{{.Importance}}</td>
|
||||
<td class="source">{{.Source}}</td>
|
||||
<td class="title">{{.Title}}</td>
|
||||
@@ -316,58 +316,5 @@
|
||||
<div class="footer">
|
||||
<a href="https://git.eeqj.de/sneak/gomeshalerter">gomeshalerter</a> is a project by <a href="https://sneak.berlin">@sneak</a> and released under the WTFPL (<a href="https://git.eeqj.de/sneak/gomeshalerter">source</a>)
|
||||
</div>
|
||||
|
||||
<script>
|
||||
// Function to format relative time
|
||||
function formatRelativeTime(timestamp) {
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
const diff = now - timestamp;
|
||||
|
||||
// Less than a minute
|
||||
if (diff < 60) {
|
||||
return 'just now';
|
||||
}
|
||||
|
||||
// Less than an hour
|
||||
if (diff < 3600) {
|
||||
const minutes = Math.floor(diff / 60);
|
||||
return `${minutes}m ago`;
|
||||
}
|
||||
|
||||
// Less than a day
|
||||
if (diff < 86400) {
|
||||
const hours = Math.floor(diff / 3600);
|
||||
return `${hours}h ago`;
|
||||
}
|
||||
|
||||
// Less than a week
|
||||
if (diff < 604800) {
|
||||
const days = Math.floor(diff / 86400);
|
||||
return `${days}d ago`;
|
||||
}
|
||||
|
||||
// More than a week
|
||||
const weeks = Math.floor(diff / 604800);
|
||||
return `${weeks}w ago`;
|
||||
}
|
||||
|
||||
// Update all timestamp elements
|
||||
function updateRelativeTimes() {
|
||||
const timestampElements = document.querySelectorAll('.timestamp[data-timestamp]');
|
||||
|
||||
timestampElements.forEach(el => {
|
||||
const timestamp = parseInt(el.getAttribute('data-timestamp'), 10);
|
||||
if (!isNaN(timestamp)) {
|
||||
el.textContent = formatRelativeTime(timestamp);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Update times on page load
|
||||
document.addEventListener('DOMContentLoaded', updateRelativeTimes);
|
||||
|
||||
// Update times every minute
|
||||
setInterval(updateRelativeTimes, 60000);
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user